convert an array of bytes into an integer

127 views Asked by At

I'm working on IJVM project using C language. First, I imported a binary file into my program, which should be parsed as words. Now I made a variable (text) which contains the binary and I need to convert it into an array of words. I got a hint

    int result = ((bytes[0] & 0xFF) << 24) |
    ((bytes[1] & 0xFF) <<16) |
    ((bytes[2] & 0xFF) << 8) | (bytes[3] & 0xFF);

This worked only for the first 4-bytes, What I need is an array of result.

I tried something like this, but it never worked .. :(

void type_converter(byte_t* text)
{
  for (int i = 0; i < sizeof(text)/4; i++) {
    for (int j = 0; i < sizeof(text)/4; i++) {

      int result[i] = ((text[j+0] & 0xFF) << 24) |
      ((text[j+1] & 0xFF) << 16) |
      ((text[j+2] & 0xFF) << 8) | (text[j+3] & 0xFF);
      instruction[i] = result;
      j =+ 4;
    }
  }
1

There are 1 answers

1
Scheff's Cat On BEST ANSWER

About OP's code sample:

  • The inner loop doesn't make sense (or I didn't understand the intention).

  • The outer loop, I would change to: for (int i = 0; i + 3 < size; i += 4). Please, note the i + 3. It makes the loop safe against a partial amount of bytes at end of buffer. The i += 4 makes the appropriate step in 4 byte jumps.

  • The size is something you have to provide. As Martin already mentioned: sizeof text provides the size of the pointer. There is no chance to determine this by an operator (if you don't have direct access to original array). You should make a parameter size for this. (This is a very common solution in C programs.)

  • Btw. there is no declaration for result or instruction. Hence, I would make one of them as another parameter of function.

So, your change function could look like this:

void type_converter(uint32_t *result, byte_t *text, size_t size)
{
  for (size_t i = 0; i + 3 < size; i += 4) {
    result[i / 4]
      = ((text[i+0] & 0xFF) << 24)
      | ((text[i+1] & 0xFF) << 16)
      | ((text[i+2] & 0xFF) << 8)
      | (text[i+3] & 0xFF);
    }
  }
}

And, please, don't forget that result has to provide enough storage for size / 4 uint32_t words.


As I just remembered how I did similar things in the past, this can be a little bit changed to prevent the i + 3 in loop condition:

void type_converter(uint32_t *result, byte_t *text, size_t size)
{
  for (size_t i = 3; i < size; i += 4) {
    result[i / 4]
      = ((text[i-3] & 0xFF) << 24)
      | ((text[i-2] & 0xFF) << 16)
      | ((text[i-1] & 0xFF) << 8)
      | (text[i] & 0xFF);
    }
  }
}

Live Demo on wandbox

Note:

I just realized that this kind of byte combination is for big-endian numbers (highest byte in first position). For little-endian the indices had to be re-ordered.