Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data formatting

Status
Not open for further replies.

BigDaz

Technical User
Jan 14, 2002
39
GB
if i have a 16-bit integer (i.e. 12345) and i want to send it via tcp/ip how can i send it as two 8-bit bytes and not as 5 char bytes (i.e. '1', '2', '3', '4', '5')?

how can i format the integer before sending so that it takes up as little space as possible?

thanks, bigdaz
 
Try this :
create union containing two chars, then send the two chars.

 
fheyn is right, but you must care, that on both sides representation of integers are the same - depending on processor type order of lower and higher bytes can differ.
 
branyesz in
thread116-352512 more correct - better approach is to send
value & 0xFF
first and then
value >> 8
Received bytes must be collected as
first_byte | (second_byte << 8)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top