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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DBuss
    New Member
    • Mar 2007
    • 12

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

    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.

    Code:
    unsigned int int_x1 = buffer[2]<<24 | buffer[3]<<16 | buffer[4]<<8 | buffer[5];
    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).

    Code:
    memcpy (&int_x1, &buffer[2], 4);
    memcpy (&int_x2, &buffer[6], 1);
    And the following code also produces garbage.

    Code:
    unsigned int int_x1 = *(int *)&buffer[2];
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    How is buffer defined??

    I assume it is a char array and the sizeof(char) on ypur system, is 1. Yes?

    Comment

    • DBuss
      New Member
      • Mar 2007
      • 12

      #3
      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].
      Code:
      char buffer[1536];
      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.
      Code:
      int xxMsgType;
      memcpy (&xxMsgType, &buffer[6], 1);
      cout << "  memcpy MsgType " << xxMsgType;
      
      int xSeqNo4 = buffer[2]<<24 | buffer[3]<<16 | buffer[4]<<8 | buffer[5];
      cout << "  SEQ T4= " << xSeqNo4;
      cout << endl;
      
      cout << "  memcpy MsgType " << xxMsgType;
      cout << endl;

      Comment

      • DBuss
        New Member
        • Mar 2007
        • 12

        #4
        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.

        Comment

        Working...