TCP Socket Sending HEX instead of text

I’ve written a number of apps that use the TCPSocket connect to send plain text to third party servers and it works great. However, I now have an application where I want to send some HEX data (01 4E 30 54 49 09 30 30 30 46 09 30 30 30 43 32 41 04) and just putting that in the write method doesnt work.

So what do I need to do instead of TCPSocket1.Write(“01 4E 30 54 49 09 30 30 30 46 09 30 30 30 43 32 41 04”) ?

Many thanks!


What you’re sending is text. So you’ll have to explain what you mean by “doesn’t work”.

Have you tried using EncodeHex to encode the data you intend to send?

Var s As String s = EncodeHex("The cake is a lie.", False) // s is "5468652063616B652069732061206C69652E"

And I’m not sure without more information on the service and text you’re using, but text encodings are something to be wary of in nearly all aspects of text communication.

I have the HEX data i need to send already, this is the data I put in the post. I do not need to convert text to hex, i already have the hex. But I need to send it as HEX and not as text.

Are you supposed to send a HEX string, or the data that the HEX string represents? We don’t really have enough information to help you.

Hex is text or rather it it is numbers represented by teh code points that teh numbers reprinted as text would be.

What I am saying is that you are sending binary no matter what, be they code points that could be interested as text (and Hex numbers are numbers represented as text in a certain way) or binary representations of numbers directly as in the IEEE standards.

So what are you trying to do?

  • Karen

So what you actually have is some binary that you want to send. I don’t see why you can’t just send that to the socket. But you could encode it using

I need to send a HEX string, not the data that the HEX string represents.

When I put the hex string into the HEX field in packet sender (a standalone app i downloaded and run on my mac), and send it, the hardware responds to the comand as I would want it to. All I know, is that I am sending it as HEX and not as ASCII. If I switch to ASCII in packet sender, it doesnt work. So I know I need to send it as HEX.

As for the HEX string itself, it is structured as follows:

01 4E 30 54 49 09 30 30 30 46 09 30 30 30 43 32 41 04

0x01 – SOH (start of heading)
0x4E – N (protocol identification)
0x30 – 0 (sequence number, normally 0 in this application)
0x54, 0x49 – TI (requested command, use ‘TI’ for ‘Take Index’)
0x09 – TAB (horizontal tab)
0x30 0x30 0x30 0x46 – Destination (selects destination “0016”, four hex numbers, )
0x09 – TAB (horizontal tab)
0x30 0x30 0x30 0x43 – Source (selects source “0013”, four hex numbers, see note below)
0x32 – Checksum byte0
0x41 – Checksum byte1
0x04 – EOT (end of transmission)

You are trying to send binary, which will allow you to include non-text characters like SOH and EOT.
DecodeHex will convert your HEX into binary characters to transmit:


Thanks John! That worked perfectly.