Using bitwise operators in C++ to change 4 chars to int

18,348

Solution 1

You want the bitwise left shift operator:

typedef unsigned char u8;  // in case char is signed by default on your platform
unsigned num = ((u8)chars[0] << 24) | ((u8)chars[1] << 16) | ((u8)chars[2] << 8) | (u8)chars[3];

What it does is shift the left argument a specified number of bits to the left, adding zeros from the right as stuffing. For example, 2 << 1 is 4, since 2 is 10 in binary and shifting one to the left gives 100, which is 4.

This can be more written in a more general loop form:

unsigned num = 0;
for (int i = 0; i != 4; ++i) {
    num |= (u8)chars[i] << (24 - i * 8);    // += could have also been used
}

The endianness of your system doesn't matter here; you know the endianness of the representation in the file, which is constant (and therefore portable), so when you read in the bytes you know what to do with them. The internal representation of the integer in your CPU/memory may be different from that of the file, but the logical bitwise manipulation of it in code is independent of your system's endianness; the least significant bits are always at the right, and the most at the left (in code). That's why shifting is cross-platform -- it operates at the logical bit level :-)

Solution 2

Have you thought of using Boost.Spirit to make a binary parser? You might hit a bit of a learning curve when you start, but if you want to expand your program later to read floats and structured types, you'll have an excellent base to start from.

Spirit is very well-documented and is part of Boost. Once you get around to understanding its ins and outs, it's really mind-boggling what you can do with it, so if you have a bit of time to play around with it, I'd really recommend taking a look.

Otherwise, if you want your binary to be "portable" - i.e. you want to be able to read it on a big-endian and a little-endian machine, you'll need some sort of byte-order mark (BOM). That would be the first thing you'd read, after which you can simply read your integers byte by byte. Simplest thing would probably be to read them into a union (if you know the size of the integer you're going to read), like this:

union U
{
    unsigned char uc_[4];
    unsigned long ui_;
};

read the data into the uc_ member, swap the bytes around if you need to change endianness and read the value from the ui_ member. There's no shifting etc. to be done - except for the swapping if you want to change endianness..

HTH

rlc

Share:
18,348
contrapsych
Author by

contrapsych

CS major at Kansas State University, class of 2016. I am mostly interested in low level languages, around the areas of C/C++, but I enjoy tinkering in anything, so I have messed with HTML and C#. I also have experience writing Android applications with Java. My interests include AI and parallelism. I just like to know how things work down to the nitty gritty.

Updated on July 01, 2022

Comments

  • contrapsych
    contrapsych almost 2 years

    What I must do is open a file in binary mode that contains stored data that is intended to be interpreted as integers. I have seen other examples such as Stackoverflow-Reading “integer” size bytes from a char* array. but I want to try taking a different approach (I may just be stubborn, or stupid :/). I first created a simple binary file in a hex editor that reads as follows.

    00 00 00 47 00 00 00 17 00 00 00 41
    This (should) equal 71, 23, and 65 if the 12 bytes were divided into 3 integers.

    After opening this file in binary mode and reading 4 bytes into an array of chars, how can I use bitwise operations to make char[0] bits be the first 8 bits of an int and so on until the bits of each char are part of the int.

     
    My integer = 00        00        00        00  
     +           ^         ^         ^         ^
    Chars      Char[0]  Char[1]   Char[2]   Char[3]
                 00        00        00        47
    
    
    So my integer(hex) = 00 00 00 47 = numerical value of 71
    

    Also, I don't know how the endianness of my system comes into play here, so is there anything that I need to keep in mind?

    Here is a code snippet of what I have so far, I just don't know the next steps to take.

    
    std::fstream myfile;
        myfile.open("C:\\Users\\Jacob\\Desktop\\hextest.txt", std::ios::in | std::ios::out | std::ios::binary);
        if(myfile.is_open() == false)
        {
            std::cout &lt&lt "Error" &lt&lt std::endl;
        }
        char* mychar;
        std::cout &lt&lt myfile.is_open() &lt&lt std::endl;
        mychar = new char[4];
        myfile.read(mychar, 4);
    

    I eventually plan on dealing with reading floats from a file and maybe a custom data type eventually, but first I just need to get more familiar with using bitwise operations. Thanks.