Jump to content
  • Advertisement
Sign in to follow this  
heh65532

array of hex to Number in C++

This topic is 2262 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?


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



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
Advertisement
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

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.

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

#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

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
sorry but correct me if im wrong... but what data types is being used doesn't have anything to do with the problem?

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!