473,394 Members | 1,219 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Converting a Multi-Byte Binary Stream Data into Human Readable

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.
  7.     bzero(buffer,sizeof(buffer));
  9.     // Store the received info in "buffer"
  10.     num_char = recvfrom(sockfd,buffer,BUFF_SIZE-1, 0, (struct sockaddr *) &serv_addr, &sizeof_from_addr);
  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;
  16.     // Ditto for the second byte.
  17.     cout << "Var2:" << bitset<100>(buffer[1]).to_ulong() << ":" << endl;
  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
  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;
  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;
  38.     //(And we go on but the problem remains the same)
  39.     return(1);
  40. }
Mar 6 '07 #1
2 4337
1,510 Expert 1GB
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];
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
Thank you!!!

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

Mar 7 '07 #3

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

Similar topics

by: Keith MacDonald | last post by:
Hello, Is there a portable (at least for VC.Net and g++) method to convert text between wchar_t and char, using the standard library? I may have missed something obvious, but the section on...
by: Al Moritz | last post by:
Hi all, I was always told that the conversion of Word files to HTML as done by Word itself sucks - you get a lot of unnecessary code that can influence the design on web browsers other than...
by: Toronto Web Designer | last post by:
Heya, I'm curious to know if there are any programs that convert HTML tables to a CSS layout. I already have a good handle on CSS but I'm always on the lookout for other ways of doing things. ...
by: David Goldsmith | last post by:
Any reference material recommendations discussing issues arising in the reengineering of a legacy, structured-style C program to an OO C++ program? DG
by: Coleen | last post by:
Hi All :-) I found the way to get my column sum (Thanks Cor I did it a little different, but the result is what I wanted) I used: dt_stat_report_3b.Columns.Add(New DataColumn("Sum",...
by: Man-wai Chang | last post by:
Must I use CSS to layout the pages? -- .~. Might, Courage, Vision, SINCERITY. http://www.linux-sxs.org / v \ Simplicity is Beauty! May the Force and Farce be with you! /( _ )\ (Ubuntu 6.06) ...
by: Lame Duck | last post by:
Hi Group! I have a vector<floatvariable that I need to pass to a function, but the function takes a float * arguement. That's OK, I can convert by doing &MyVector.front(), but when I get back a...
by: Razii | last post by:
What you need, instead of doubles and floats, is Multi-Precision Library. Too bad, as usual, it's not part of standard C++ library. Use GNU Bignum Library. All problems solved easily.
by: Robert Bravery | last post by:
HI all, Can someone point me in the right direction. I am looking for the fastet and simplest way of converting a vb6 project, written by someone else (I am takeing over the project) to VB.net,...
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.