473,387 Members | 1,435 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,387 software developers and data experts.

Conversion of Binary Data into Int stuffs Ones into left most bits

12
I'm reading a stream of binary data off of a UDP socket. I've got 2 integers to read, one is four bytes long and is in buffer[2] to buffer[5], the other is length 1 and in buffer[6]. Doing the following works for some values.

Expand|Select|Wrap|Line Numbers
  1. unsigned int int_x1 = buffer[2]<<24 | buffer[3]<<16 | buffer[4]<<8 | buffer[5];
  2. unsigned int int_x2 = buffer[6];
But often the left most bits get stuffed with 1's. For example int_x2 can read 0 through 127 fine, but the next 128 numbers result in very large numbers and it doesn't work again until it hits 256. What's happending is all the bits left of the first "1" are also being set to "1". I know this is a known issue, but I don't know how to code around it.

When I do the following I get garbage data in both int_x's (although oddly int_x2 by itself seems to work for at least some values).

Expand|Select|Wrap|Line Numbers
  1. memcpy (&int_x1, &buffer[2], 4);
  2. memcpy (&int_x2, &buffer[6], 1);
And the following code also produces garbage.

Expand|Select|Wrap|Line Numbers
  1. unsigned int int_x1 = *(int *)&buffer[2];
Dec 21 '07 #1
3 1704
weaknessforcats
9,208 Expert Mod 8TB
How is buffer defined??

I assume it is a char array and the sizeof(char) on ypur system, is 1. Yes?
Dec 22 '07 #2
DBuss
12
Buffer definition and read statements follow. All the messages I'm reading are at least 27 characters so I shouldn't be trying to read past the buffer with a length of 4 at &buffer[2].
Expand|Select|Wrap|Line Numbers
  1. char buffer[1536];
  2. num_char = recvfrom(sockfd,buffer,1535, 0, (struct sockaddr *) &serv_addr_cqs, &sizeof_from_addr_cqs);
Very oddly, and perhaps related to my problem(s), the other memcpy statement (below) works if the middle 3 lines are commented out. I.e. both cout statements result in a "2" (which is correct) if the middle three lines are commented out... and BOTH couts of xxMsgType output a "1076224258" if the middle three are in there. xSeqNo4 is 304 which I think is correct.
Expand|Select|Wrap|Line Numbers
  1. int xxMsgType;
  2. memcpy (&xxMsgType, &buffer[6], 1);
  3. cout << "  memcpy MsgType " << xxMsgType;
  4.  
  5. int xSeqNo4 = buffer[2]<<24 | buffer[3]<<16 | buffer[4]<<8 | buffer[5];
  6. cout << "  SEQ T4= " << xSeqNo4;
  7. cout << endl;
  8.  
  9. cout << "  memcpy MsgType " << xxMsgType;
  10. cout << endl;
Dec 24 '07 #3
DBuss
12
This is solved now.

The solution(s).

First, memcpy was displaying undefined behavior if the destination variable was not initialized.

Second, the "left bit stuffing" issue goes away if I don't splice them directly from the buffer.

Third, memcpy worked correctly for length = 1, so I just copied every byte seperately and spliced them together.

I'm sure there's a less ugly solution out there and equally sure I'm missing something about the root cause of this, but I've spent enough time on this so I'm going to drop it.

Thank you.
Dec 28 '07 #4

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

Similar topics

5
by: john | last post by:
Here is the short story of what i'm trying to do. I have a 4 sided case labeling printer setting out on one of our production lines. Now then i have a vb.net application that sends data to this...
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
8
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
4
by: Ken Tough | last post by:
Seems like a simple thing to find out, but I'm struggling. I have googled, but everything I find is about implicit conversion, not explicit. Is this implementation-specific, or does ANSI/ISO...
9
by: bowsayge | last post by:
Inspired by fb, Bowsayge decided to write a decimal integer to binary string converter. Perhaps some of the experienced C programmers here can critique it. It allocates probably way too much...
8
by: brainflakes.org | last post by:
Hi guys, I need to manipulate binary data (8 bit) stored in a 2 dimensional array. I've tried various methods (arrays, using a string filled with chr(0), using gd lib) and so far the fastest...
1
by: John Wright | last post by:
I have a device hooked up to my COM port that I read data from the serial port into my program. If the device is in its double mode the information that comes back is an 8 bit binary field for...
8
by: sara77 | last post by:
I need to convert any integer value greate than 127 to binary value using this example: Represent the value in binary (e.g 137 => 1000 1001) 2) Break in up in groups of 7 bits from the lowest...
4
by: dondigitech | last post by:
I want to convert hex to binary without losing bits. I want to preserve the 8-bits because I ultimately need a 24-bit string to grab information from. I am just using this line of code for the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
1
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...
0
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...
0
Oralloy
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,...
0
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...
0
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...

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.