• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
heh65532

array of hex to Number in C++

13 posts in this topic

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
0

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1342037648' post='4958147']
What does it print? 4003761408?
[/quote]

yes
0

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.
0

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
0

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
0

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
0

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?
0

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.
1

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.
0

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.
1

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0