By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,481 Members | 3,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,481 IT Pros & Developers. It's quick & easy.

Converting a Multi-Byte Binary Stream Data into Human Readable

P: 12
OK, I'm reading a multicast socket. It attaches fine, reads fine, all of that.

The problem is that while some of the data I get is normal text (ASCII String), some of it is Binary Integer.

The binary data is how they send numbers (they call it "Big Endian"). I only know at run time whether a byte is going to be text or binary (one of the fields I decode tells me which the latter fields are).

The code I have converts a single byte of Binary into something more readable, but when I try to do the same with multiple bytes I trip over the conversion.

For example binary "1100101" is "101" in dec but it's also how string stores the character "e". So when I say "string X", instead of trying to convert "1100101" I end up trying to convert "e".

I use "#ifdef not_using" to remove code that isn't working. It show cases what I've tried and had fail.

Any suggestions?

Expand|Select|Wrap|Line Numbers
  1. int Read_message(int sockfd)
  2. {
  3.     char buffer[BUFF_SIZE];  // Holds the message we read
  4.     char temp_buffer[10];    // Holds the message we read/senting.
  5.     int num_char;            // Return value for read/write, # chars processed.
  6.  
  7.     bzero(buffer,sizeof(buffer));
  8.  
  9.     // Store the received info in "buffer"
  10.     num_char = recvfrom(sockfd,buffer,BUFF_SIZE-1, 0, (struct sockaddr *) &serv_addr, &sizeof_from_addr);
  11.  
  12.     // The first byte is always a number.
  13.     // This line converts it into a "1" or "2" or whatever.
  14.     cout << "ID:" << bitset<100>(buffer[0]).to_ulong() << ":" << endl;
  15.  
  16.     // Ditto for the second byte.
  17.     cout << "Var2:" << bitset<100>(buffer[1]).to_ulong() << ":" << endl;
  18.  
  19. #ifdef not_using
  20.     //This is an example of what isn't working.
  21.     //There are 4 bytes of data that need to be converted into one number.
  22.     bzero(temp_buffer,sizeof(temp_buffer));
  23.     temp_buffer[0] = buffer[2];
  24.     temp_buffer[1] = buffer[3];
  25.     temp_buffer[2] = buffer[4];
  26.     temp_buffer[3] = buffer[5];
  27.     temp_buffer[4] = '\0';
  28.     cout << "4 Byte Number:" << bitset<100>(string(temp_buffer)).to_ulong() << ":" << endl;
  29. #endif
  30.  
  31.     // Both of these are single byte numbers.
  32.     cout << "MsgType:" << bitset<100>(buffer[6]).to_ulong() << ":" << endl;
  33.     cout << "MsgFlag:" << bitset<100>(buffer[7]).to_ulong() << ":" << endl;
  34.  
  35.     // This "TimeStamp" is one long ASCII string.
  36.     cout << "TimeStamp:" << buffer[8] << buffer[9] << buffer[10] << buffer[11] << buffer[12] << buffer[13] << buffer[14] << buffer[15] << buffer[16] << buffer[17] << buffer[18] << buffer[19] << buffer[20] << buffer[21] << buffer[22] << buffer[23] << buffer[24] << buffer[25] << ":" << endl;
  37.  
  38.     //(And we go on but the problem remains the same)
  39.     return(1);
  40. }
Mar 6 '07 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
if the information in the 4 bytes is numeric you could turn it into a 32 bit integer so
Expand|Select|Wrap|Line Numbers
  1.     unsigned char ch[4];
  2.     ch[0] = 0;
  3.     ch[1] = 0x1;
  4.     ch[2] = 0x86;
  5.     ch[3] = 0xA0;
  6.     unsigned int x=ch[0]<<24 | ch[1] << 16 | ch[2] << 8 | ch[3];
  7.  
in this case the number is 100000 decimal, 11000011010100000binary or 186A0 hexadecimal. That is assuming you know which way around the bytes are, i.e. least significant first or most significant first
Mar 6 '07 #2

P: 12
Thank you!!!

That worked, and looks to be quicker than the bitset solution to boot.

:Thumbsup:
Mar 7 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.