Ok, I have asked this b4, and I don't think I have gotten a straight forward answer, I really need this answered, because what I am doing is "legal", but i am 100% it does not make logical sense.

This it the original C# code

var version = reader.ReadUInt32();
    if (version != 0x4e302e32)
        return data;

As you can see, they use version with Hex coding. ReadUInt32() is meant to read 4 bytes and they use var to create an "auto" storage to hold it. I need to read the same 4 bytes in C++ into a variable and be able to implement the same thing.

Up to this point I have been doing

unsigned long int temp = 0;
inFileDrs >> temp; // inFileDrs is reading the file just as reader is, opened in binary mode

Now that I look closer, I don't think this is going to work for me. I came up with a possible solution (along the idea of one), but this is illegal because auto's have to me initialized. So my question to the community is, what should I try? Thank you :)

auto tableCount2;
	inFileDrs.read(tableCount2, 4);

Recommended Answers

All 7 Replies

Why don't you think your first piece of c++ code wont work?

Because it is a long int value, and is not in hex value? I am not sure if it will or will not work. Thats why I am trying to get opinions on it.

Or can C++ automatically compare a hex value to its long int equivalent?

Because it is a long int value, and is not in hex value? I am not sure if it will or will not work. Thats why I am trying to get opinions on it.

Or can C++ automatically compare a hex value to its long int equivalent?

I don't think that the program cares, the base is just a representation, it's all the same number. You can do something like:

int value = 0xff;
std::cout << value << std::endl;

It will correctly print out the 255 . Where you have to be careful is with big numbers, since

int value = 0xffffffff;

will give -1 , because the hex and octal values are unsigned, so the int has "wrapped around".

yes you can compare a int to a hex number. this should work

UINT32 foo = 20;
if (foo == 0x14)
    cout << "woot";
else
    cout << "dooh";

UINT32 = Unsigned long int correct? With that answered I think I am good.

UINT32 = Unsigned long int correct?

I think that UINT32 is not an official type, I reckon it will be defined in a header file somewhere that may or may not be specific to your compiler.

I did a bit of reading about this and there appears to be a kind of unofficial convention that

unsigned char      -> 1 byte
unsigned short     -> 2 bytes
unsigned int       -> 4 bytes
unsigned long      -> 4 bytes
unsigned long long -> 8 bytes

Apparently, this is a pretty well-held convention across compilers. However, in the C99 standard and the proposed C++0x standard there is a header file called stdint.h (C) or cstdint (C++) that defines integers with known, fixed widths in a platform independent manner. These are int8_t , int16_t , int32_t , int64_t (or uint32_t etc.) and apparently should be used to absolutely ensure that you're getting the number of bytes that you expect in your int .

Currently, when I include this header, I get a warning from the compiler (gcc 4.3) that the feature is experimental and to get it to compile I have to use the "-std=c++0x" flag. If I do that the everything appears to work as expected though.

Anyway, I just though that might be useful to your binary-reading project :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.