Archived

This topic is now archived and is closed to further replies.

asdasd12345

Changing hex to int

Recommended Posts

Hello, I want to take a memory address and turn it into an integer so I can mutiply it to another int to give me more randomness in a random number generator. I figured because programs are always run in a different spot in the memory this would help. Does anyone know how you convert a hex memory address to an int? By the way Im using C++. [edited by - asdasd12345 on October 24, 2003 4:25:02 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you probably need to seed the random number generator. Try srand(GetTickCount()) at the beginning of your program

Share this post


Link to post
Share on other sites
Use rand() - that''s good enough in most cases. Just seed it with the starting time of your program.

If that''s not enough, go and by "The Art of Computer Programming" by Donald Knuth. One of them (I think Volume 2) covers all sorts of random number generators. Or search the web for "Pseudo Random Number Generators". Or go to http://www.agner.org/random/

By the way - you convert pointers to int by just casting them.

int* intptr;
int intvalue;

intvalue=(int)intptr;

works just fine. Don''t let the hex numbers you see in the debugger irritate you.

- Robert

Share this post


Link to post
Share on other sites
Try the srand() function. You can look it up on google to get all the various overloaded functions. But it will give you a different number every time no matter what. (P.S. You would call this function before using rand())

Brent

Share this post


Link to post
Share on other sites
Include if you are on Windows and seed the random time generator with the Tick time. This ensures almost truly random numbers, unless you have your own formula for lots of random numbers.

Scott Simontis
e-mail:ageofscott@comcast.net
AIM:ssimontis

Share this post


Link to post
Share on other sites
quote:
Original post by asdasd12345
I figured because programs are always run in a different spot in the memory this would help.

But they''re not. It depends on what has been running before.

Share this post


Link to post
Share on other sites
quote:
Original post by merlin9x9
...but don''t ever let yourself believe that casting pointers to integers is okay.
quote:
C++ standard, 5.2.10/4
A pointer can be explicitly converted [with reinterpret_cast] to any integral type large enough to hold it. The mapping function is implementation-defined [Note: it is intended to be unsurprising to those who know the addressing structure of the underlying machine. ]

Share this post


Link to post
Share on other sites
Hex values are NOT a datatype. Let''s make that clear. Decimal values are also not datatypes. Integers are actually stored as binary values (8 is 111 for example). Decimal and hexadecimal notation are ways of representing numbers in STRINGS. Here are some equivalences:

int D = 32;
int H = 0x20;

if( D == H ) printf( "Same\n" ); // output: Same

printf( "%d\n", D ); // output: 32
printf( "%d\n", H ); // output: 32
printf( "%x\n", D ); // output: 20
printf( "%x\n", H ); // output: 20


~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.

Share this post


Link to post
Share on other sites
quote:
Original post by CGameProgrammer
(8 is 111 for example).

um, actually 8 is 1000 , 111 bin = 7 in decimal
to quote from my favorite t-shirt "there are only 10 types of people in this world. Those that understand binary, and those that dont"

to answer your question, hex to int is very easy

hex goes 0 to F, with 0 = 0 decimal, and f = 15 decimal (16 unique characters)

basicly, instead of going ...8,9,10,11 you go ...8,9,A,B

so 0Ah (lowercase h denotes hex) = 10 decimal, FFh = 255 decimal

from there it''s simple math.


also, dont use the routine you''re talking about hoping to get random numbers. If the built in rand function isnt enough for you seed the following code with 2 rand()''s seeded from 2 seperate timeGetTime()''s and that should be more random than you ever would need


float noise(int x, int y)
{
int temp=0;
int count=0;

_asm
{
pushad
mov eax, 57
mul y
add eax, x
mov count, eax
shl eax, 13
mov temp, eax
popad
}
temp=temp^count;
_asm
{
pushad
mov eax, temp
mov ebx, eax
mul eax
mov edx, 15731d
mul edx
add eax, 789221d
mul ebx
add eax, 1376312589d
and eax, 7fffffffh

//do the division and subtraction here


mov temp, eax
popad
}
return (float)(1.0-(float)temp/1073741824.0);
}

note: unfinished assembly, i got lazy and wrote the floating point part in c code, it''s really bad assembly at that too

Share this post


Link to post
Share on other sites
quote:
Original post by Raptor85
quote:
Original post by CGameProgrammer
(8 is 111 for example).

um, actually 8 is 1000 , 111 bin = 7 in decimal
to quote from my favorite t-shirt "there are only 10 types of people in this world. Those that understand binary, and those that dont"

Sorry, I wasn''t thinking... I was counting from 0, thinking 7 is the 8th number so 8 is 111... I''ll be quiet now.

~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.

Share this post


Link to post
Share on other sites
As far as I''m concerned, what the C++ standard says on this doesn''t matter; many things in C++ are around so as not to break too much from C. In my 12 years of programming, I''ve never had a legitimate need to do pointer <--> integer conversions, and I''d be very surprised to hear of any such "needs" that couldn''t be implemented another, better way. I believe that the very concept of such conversions is a logic error in a high-level language.

Share this post


Link to post
Share on other sites
merlin9x9:

Guess you never wrote fixup-code for a dynamic loader, then. Any kind of address fixup *needs* integer<->pointer magic. Yes, you can use byte pointers instead, but that''s just hiding the conversion to integer, that''s all.

There''s plenty of other reasons why you might want to do this - but most of them are only needed if you need to get *really* close to the metal. So in a way, merlin''s correct - if you do this in high-level code, there''s a very good chance you don''t need to.

- Robert

Share this post


Link to post
Share on other sites
There are indeed uses of pointer-integer conversion. For example, my 3D engine supports both Direct3D and OpenGL, so the material structure includes a "UINT Texture". In OpenGL, it is the integer set by glGenTextures() as a handle to the texture. For Direct3D, it''s a pointer to the texture.

~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.

Share this post


Link to post
Share on other sites
quote:
Original post by merlin9x9
As far as I''m concerned, what the C++ standard says on this doesn''t matter...
Oh, then you''re not saying that it''s non-standard; just that it''s often bad style. Sorry. I misunderstood.

Share this post


Link to post
Share on other sites