# sending a hex string to a remote via netcat?

## afabco

Hey folks,

I've got some binary commands (which I'm representing as hex) that I need to send to a remote device (it's an embedded data collection device) and observe the reply.  

It's easy to connect to it with netcat

```
nc -v 192.168.200.34 19000
```

and it sits there happy as a clam.

The hex string I need to send is something like:

 *Quote:*   

> 05:64:10:D3:FD:00:00:00:E7:30:F5:C6:01:3C:02:07:1E:3C:03:07:1E:E7:59
> 
> 

 

But when I type this into my netcat window (with or without spaces , with or without the colons) netcat transmits it as ascii.

All the literature is happy to tell me how to capture a hexdump from the remote device, but not how to transmit binary/hex data to the remote device.  

Is this an easy thing to do, or am I missing something?

thanks!

----------

## Hu

Netcat will forward its input unmodified.  Your best bet would be to find or make a tool that reads hexadecimal numbers and prints the corresponding bytes onto its standard output.  Then do helper-cmd | nc 192.168.200.34 19000.

If you could write the data in base64 instead, then the coreutils program /usr/bin/base64 could transform your data into raw bytes for the wire.

----------

## coolsnowmen

perhaps something like this (compile with g++ filename.c)

```
#include <cstdio>

#include <string>

//Written by Coolsnowmen for free

int hex2int(char &hex)

{

   const static std::string key("0123456789abcdef");

   return key.find( hex | 32 );

}

int main(int argnum,char ** argin)

{

  if (argnum==2) {

   int length=strlen(argin[1]);

   if (length%2==0) {

      for (int idx =0; idx<length; idx+=2)

         printf("%c",hex2int(argin[1][idx])*16 + hex2int(argin[1][idx+1]));

      return 0;

   }

   else   {

      fprintf(stderr,"non-even length string");

   }

  } else printf("1 argument expexted, of hexpairs, eg: %s 4a4f4e --> JON",argin[0]);

  return 1;

}
```

testing:

```
./a.out 000102030405 | wc

      0       0       6
```

----------

