2

I want to send the payload which is 6 bytes of hex : "8000000004d2" using udp package provided by tcl. I am able to send it and on the receiver pc i am able to read the same data. But in the wireshark capture it shows 12 bytes of payload, because it sends this data in ascii format like "38 30 30 30 30 30 30 30 31 32 33 34". Can anybody please tell me is the data really getting transmitted as 12 bytes or only Wireshark is interpreting it false. If the data is getting transmitted as 12 bytes, can anybody help me out to send it only in 6 bytes using tcl udp package only. For reference i am giving the udp sender code

UDP sender code :

namespace eval soc {

variable s
}

set soc::s [udp_open]
udp_conf $soc::s $IP_ADDR_RX  $UDP_PORT
fconfigure $soc::s -buffering none -translation binary

set data 1234
append hex [ format %04X [ expr $i | 0x8000 ] ]
append hex [ format %08X [ expr $data ] ]
puts -nonewline $soc::s $hex
user1497818
  • 375
  • 1
  • 7
  • 16

1 Answers1

3

If you have a payload in hex, decode it into byte array before sending:

# For tcl 8.5 and below
puts -nonewline $soc::s [binary format H* $hex]
# For tcl 8.6 and above
puts -nonewline $soc::s [binary decode hex $hex]

But there is no reason to have intermediate hexadecimal representation: you can create byte array from the very start.

set bytes [binary format SI [expr {$i|0x8000}] $data]
puts -nonewline $soc::s $bytes

(Note: expr without {,} produces an additional round of substitution before parsing, which is usually wrong semantically and always bad for performance).

Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69