Search code examples
matlabunixudpnetcat

Send number via netcat


I want to send a single number via netcat. I don't want to send the ASCII representation of the number, but the binary version of the number (uint8, int32, etc.). I have a UDP port open in Matlab that is waiting to receive the number. Matlab's dsp.UDPReceiver can only accept ['uint8' (default) | 'double' | 'single' | 'int8' | 'int16' | 'uint16' | 'int32' | 'uint32' | 'logical']. FYI, I am sending integers via UDP to a process to control some action therein.

I originally tried

echo 5 | netcat -u localhost 12345

but the receiver (in Matlab) prints out ans = uint8 53 because the output of echo is the string not the binary representation of the number 5. I tried using bc like this:

echo "obase=2;5" | bc -l|netcat -u localhost 12345

but get the result ans = uint8 49 because bc is returning the ASCII version of the binary rather than the bits themselves.

How can I send a single number via netcat?


Solution

  • With the option -e echo supports given the bytes as hexadecimal or octal. With -n the final newline gets supressed:

    echo -n -e '\x05' | netcat ...