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 gkittelson 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
 
What language?

For VB try:

Dim byArray(1) As Byte
byArray(0) = Integer16 And &HFF
byArray(1) = (Integer16 And &HFF00) / 16

Send (byArray)

I think that this work, haven't tried it myself. This would reverse the order as well, sending the higher order byte after the lower order byte.

pansophic
 
i'm using visual c++

the problem if found (and i hope i'm mistaken)is that using the 'send' function i can only send chars, but want to send a string or array of 16-bit intergers.

thanks, bigdaz
 
OK, I think that you will get a better answer posting this to one of the C++ forums here, but this MAY work.

byte [1] bArray = new byte [];
bArray[1] = (byte)int16 && 0xff;
bArray[0] = (byte)((int16 && oxff00)/16);

Send((char)bArray);

pansophic
 
cheers in the end i used

on the sending side
bArray[0] = 'unsigned int' & 0xff;
bArray[1] = ('unsigned int' >> 8) & 0xff;

on the receiving side
'unsigned int' = bArray[0] + (bArray[1] << 8);

it was easier than i thought.

cheers, bigdaz.
 
Forgot about the shift operator. Works much better than dividing, expecially with the posibility that the byte may end up being null.

Glad you worked it out!

pansophic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top