Sign in to follow this  

array of hex to Number in C++

This topic is 2014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi
can any one tell why doesnt this print 9282798 which is the hexedecimal value in the buffer?

[CODE]
BYTE * buffer = new BYTE[4];
buffer[0] = 0;
buffer[1] = 0x8D;
buffer[2] = 0xA4;
buffer[3] = 0xEE;

// 9282798


DWORD *p = (DWORD*)buffer; // Convert pointer
cout << ((*p)==9282798) << " number: " << ( (DWORD) (*p) ) << endl; // print buffer as DWORD

[/CODE]

I thought the buffer (array) could be converted to DWORD with type conversion alone because the memory has same region (4 bytes) but I guess i was wrong.

what am I doing wrong?

thx Edited by heh65532

Share this post


Link to post
Share on other sites
The problem is, that your CPU lays out the bytes of integers in memory in the opposite direction (little endian) as you are used to (big endian).
So when you type the bytes in the order 00, 8D, A4, EE, you mean the hexadecimal number 8DA4EE, but your processor "thinks" you mean EEA48D00.

You can't generalize this effect though. On other CPUs, it may be just the other way around. Don't write your integers that way or use a conversion function (ntohl for example) afterwards. But then you'll have to be careful about data alignment, because some CPUs can't read integers from memory addresses that are not a multiple of four (and your new BYTE[] will likely align the new memory to 1 byte).

Also, you should not use new and if you use it, don't use it without a smart pointer like unique_ptr or shared_ptr.

Ah, and don't use C-Style casts "(DWORD*)buffer". If you really want to do something like this, use a reinterpret_cast<DWORD*>. This does not change how error-prone the cast is, but it shows that you know it is dangerous. Also you can see the cast more easily in your editor.

And I have one more advice for your coding practices, even if this one is more of a question of good taste. Don't use DWORD or BYTE unless you really need to. Use cstdint's uint32_t or uint8_t instead.

Share this post


Link to post
Share on other sites
so i need to covert to proper endian type? but also determine which type it is.

i suppose this code didn't cause anything like buffer-overflow it just resulted in wrong value. But withing the correct memory range?

thx

Share this post


Link to post
Share on other sites
[quote name='rnlf' timestamp='1342077075' post='4958281']
Also, you should not use new and if you use it, don't use it without a smart pointer like unique_ptr or shared_ptr.

And I have one more advice for your coding practices, even if this one is more of a question of good taste. Don't use DWORD or BYTE unless you really need to. Use cstdint's uint32_t or uint8_t instead.
[/quote]
Using a new without a smart pointer type is fine as long as you know what you are doing and you know who has ownership of the pointer, and DON'T forget to use delete on it, otherwise you might be better of using a smart pointer type. But just flat out saying don't use without a new is just wrong. In this case you don't even need to use "new" as "Byte buffer[4];" will achieve the same thing without dynamic memory

As for the types I would define my own typedefs like this
[code]
#ifdef __GNUC__
typedef uint8_t byte;
typedef uint16_t word;
typedef uint32_t dword;
typedef uint64_t qword;
#elseif _MSC_VER
typedef unsigned __int8 byte;
typedef unsigned __int16 word;
typedef unsigned __int32 dword;
typedef unsigned __int64 qword;
#endif
[/code]
As GCC and MSVC approach this differently, but the typedefs can now be used on both and will mean the same on both platforms. Edited by NightCreature83

Share this post


Link to post
Share on other sites
I'm sorry, but this is wrong. What happens if an exception is thrown between your new and the matching delete? Or you refactor your code months later and you insert a return statement somewhere in the middle. Your pointer will go out of scope, the delete will never be executed. Result: Memory leak. If you use new without a smart pointer, you most like do not know, what you are doing, thus must not use it. There may be a few exceptions to this, but you will have to think really hard before doing it.

Yes, you can just allocate it on the stack which is a much better idea, if it fits the rest of your program (like you can't return the memory from a function then). Edited by rnlf

Share this post


Link to post
Share on other sites
DWORD is a particularly awful name for a 32-bit integer. The machine word hasn't been 16 bits since the 80386, which came out in 1985. I understand keeping the label around in VC++ for compatibility, but I wouldn't use it.

Share this post


Link to post
Share on other sites
In masm a word is still a 16 bit entity and that is where the names come from for MSVC++. But as I showed in an earlier post to be portable throughout your code you should redefine the primitive types to names of your own choosing as that will make your code compiler independent.

Share this post


Link to post
Share on other sites
Please don't. I can tell you from my experience, that this will give you a bad time. At a company I worked for, they had done this. They used an awful mess of conditional compilation to select the right typedefs for a given compiler and target processor. I tried to port code from 32 to 64 bits. I had to redo all the old type definitions (there were some for several 8 bit processors, a few 32 processors and so on). It was a pain in the ass.

There isn't a single sensible reason to define your own MY_INT32 or something similar. We have stdint.h and cstdint. They define everything you need. Types with guaranteed sizes and signedness. Redefining those types will also require you to include your type definition headers in each and every source file, thus slowing down the development process and compilation.

In order to improve readability of your code, you could define types for your function arguments. Like a typedef double Angle; or something like this. It won't keep you from passing the wrong type to your function, but it allows you to make the code easier to understand for a reader.

Share this post


Link to post
Share on other sites
i'm just doing some windows GUIs. I wont port that so DWORD was useful definition and the win api uses it, so it should be perfect with win GUIs

Share this post


Link to post
Share on other sites
Sign in to follow this