2

i want to read some Bytes of my File as an Integer Value. For Example the file contains the value 0x000003BB which equals 955.

I could read the data this way:

ifstream input ("C:/Desktop/myFile", ios::binary);
uint8_t buffer[4] = {0};
input.read((char*)buffer, sizeof(buffer));

But how can i convert the buffer-array to the corresponding int-value 955? And if possible in a endianness independent way, because this and some other Files in Big Endian Byte Order but my system are on Little Endian Byte Order. Thanks :)

Opa114
  • 518
  • 4
  • 12
  • 28

3 Answers3

3

I am not sure that this is the best solution, but it works.

uint8_t buffer[4] = {0};
input.read((char*)buffer, sizeof(buffer));
unsigned int result = buffer[0];
result = (result << 8) | buffer[1];
result = (result << 8) | buffer[2];
result = (result << 8) | buffer[3];
eerorika
  • 232,697
  • 12
  • 197
  • 326
petrmikheev
  • 342
  • 1
  • 6
  • it did not consider the endianness. If i print out the data-var i got the value ´3137536000´ which equals ´0xBB030000´but i need to get ´0x000003BB´ – Opa114 Jun 15 '16 at 18:25
  • @Opa114 actually, this code does *consider endianness* in the sense that it works on a machine of any endianness. There is simply a mistake, and this converts *little* endian, rather than big endian which is what you wanted. Change the buffer indices to go from 0 to 3, and it'll work. – eerorika Jun 15 '16 at 18:38
2

If your OS supports POSIX or is windows, then you can use ntohl:

int data;
input >> data;
data = ntohl(data);
eerorika
  • 232,697
  • 12
  • 197
  • 326
1

There is no need to use an array of uint8_t, you can direcly read into a 4 byte integer:

uint32_t data;
input.read((char*)&data, sizeof(data));

EDIT:

This code works on my VC++ compiler:

#include<stdint.h>
#include<intrin.h>
#include<iostream>

int main()
{
    uint32_t data;
    data = 0xBB030000;
    data = _byteswap_ulong(data);

    std::cout << data;
}

If you are using GCC, you need to replace _byteswap_ulong with __builtin_bswap32. If you need independent code, you need to craft your own (probably on top of these compiler intrinsics).

Ajay
  • 18,086
  • 12
  • 59
  • 105
  • 1
    this works, but it did not consider the endianness. If i print out the data-var i got the value `3137536000` which equals ´0xBB030000´ but i need to get `0x000003BB` – Opa114 Jun 15 '16 at 18:23
  • Still falls into the same trap, Ajay. OP doesn't know if the file is big or little and they want generic code to handle both cases. Any file protocol that doesn't specify endian is flawed and should go back to the lab for a redesign, but.... – user4581301 Jun 15 '16 at 18:34
  • 1
    Numbers don't keep endian-ness with them! – Ajay Jun 15 '16 at 18:35
  • Exactly. The question resolves down to "How do you safely determine the endian of a file?" Without more information, you can't. The file MUST have some sort of tag or a value that can be tested for validity and will only be valid in one endian. OP hasn't specified any of this. – user4581301 Jun 15 '16 at 18:41