473,757 Members | 10,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting a Multi-Byte Binary Stream Data into Human Readable

12 New Member
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
2 4380
horace1
1,510 Recognized Expert Top Contributor
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, 110000110101000 00binary 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
DBuss
12 New Member
Thank you!!!

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

:Thumbsup:
Mar 7 '07 #3

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

Similar topics

22
5494
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 codecvt, in Josuttis' "The Standard C++ Library", did not help, and I'm still awaiting delivery of Langer's "Standard C++ IOStreams and Locales". Thanks,
20
7354
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 Internet Explorer. Our computer expert in my company had told me already a while ago that I should learn HTML and encode myself. I was never inclined to do so (I am no computer expert), and when upon his suggestion I looked how my pages (converted to...
14
6687
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. :-) Cheers.
5
1489
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
9
2094
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", GetType(Double), "sum(Column_10_ld_act_125_gtr_fy_fy_hh_avg)")) where dt_stat_report_3b is my datatable, Sum is my column name and sum(Column_10_ld_act_125_gtr_fy_fy_hh_avg) is the sum of the column I needed. This Does return me the correct value in the...
4
2114
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) Linux 2.6.17.1 ^ ^ 16:57:01 up 2 days 6:55 0 users load average: 1.18 1.09 1.02 news://news.3home.net news://news.hkpcug.org news://news.newsgroup.com.hk
156
5882
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 float * from the function, how to convert that back to a vector? Thanks in advance!
6
247
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.
5
1475
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, posibly VB9 Thanks Robert
9
4502
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 function: x = new int;
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.