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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

formatting of data

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
 
Your example was 12345. This is 3039 in hexa. So you can send first 30H and after that 39H(the order is irelevant)
......
__int16 yourNumber=12345;
BYTE firstByte, secondByte;
firstByte=BYTE(yourNumber>>8);//now firstByte is 30H
yourNumber=yourNumber<<8;// yourNumber become 3900H
yourNumber=yourNumber>>8;// yourNumber become 0039H
secondByte=BYTE(yourNumber);//now secondByte is 39H
// send firstByte and secondByte
.......

on the other side build the number
__int16 yourNumber;
BYTE firstByte, secondByte;
//...receive the firstByte, secondByte
yourNumber=firstByte;
yourNumber=yourNumber<<8;
yourNumber+=secondByte;

// now yourNumber=12345
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top