Jump to content

  • Log In with Google      Sign In   
  • Create Account


array of hex to Number in C++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 heh65532   Members   -  Reputation: 150

Like
0Likes
Like

Posted 11 July 2012 - 02:09 PM

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, 11 July 2012 - 02:10 PM.


Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 11709

Like
4Likes
Like

Posted 11 July 2012 - 02:14 PM

What does it print? 4003761408?

#3 Bregma   Crossbones+   -  Reputation: 4741

Like
4Likes
Like

Posted 11 July 2012 - 02:47 PM

clue
Stephen M. Webb
Professional Free Software Developer

#4 heh65532   Members   -  Reputation: 150

Like
0Likes
Like

Posted 11 July 2012 - 11:41 PM

What does it print? 4003761408?


yes

#5 rnlf   Members   -  Reputation: 1057

Like
0Likes
Like

Posted 12 July 2012 - 01:11 AM

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.

my blog (German)


#6 heh65532   Members   -  Reputation: 150

Like
0Likes
Like

Posted 12 July 2012 - 01:49 AM

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

#7 NightCreature83   Crossbones+   -  Reputation: 2654

Like
0Likes
Like

Posted 12 July 2012 - 02:17 AM

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, 12 July 2012 - 02:22 AM.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#8 rnlf   Members   -  Reputation: 1057

Like
0Likes
Like

Posted 12 July 2012 - 02:47 AM

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, 12 July 2012 - 02:50 AM.

my blog (German)


#9 heh65532   Members   -  Reputation: 150

Like
0Likes
Like

Posted 12 July 2012 - 03:49 AM

sorry but correct me if im wrong... but what data types is being used doesn't have anything to do with the problem?

#10 rnlf   Members   -  Reputation: 1057

Like
0Likes
Like

Posted 12 July 2012 - 03:56 AM

No, it doesn't. It's just a stylistic advice.

my blog (German)


#11 Álvaro   Crossbones+   -  Reputation: 11709

Like
1Likes
Like

Posted 12 July 2012 - 06:09 AM

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.

#12 NightCreature83   Crossbones+   -  Reputation: 2654

Like
0Likes
Like

Posted 12 July 2012 - 08:02 AM

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.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#13 rnlf   Members   -  Reputation: 1057

Like
1Likes
Like

Posted 12 July 2012 - 09:22 AM

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.

my blog (German)


#14 heh65532   Members   -  Reputation: 150

Like
0Likes
Like

Posted 13 July 2012 - 07:58 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS