• Advertisement

Archived

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

Hex programming

This topic is 6559 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

Man, i have a major question. I am a beginner programmer and I use a tutorial book. I am having major problems, evryprogram the book shows has hex in it. I would like to know do I need to know hex in order to program games. Im only 13 so learning hex is a hard thing for me. I''m already having enough trouble learning C

Share this post


Link to post
Share on other sites
Advertisement
Learning Hex shouldn''t be any harder then learning decimal, it''s just another base. decimal = base 10, hex= base 16.
In hex, instead of counting to 9 before you add another digit, you count to 15. Then, every digit after that represents 16 more. EX:
HEX DECIMAL
0 = 0
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 7
8 = 8
9 = 9
a = 10
b = 11
c = 12
d = 13
e = 14
f = 15
f0 = 16

note, the a through f can be capitalized. It makes no difference. The last example I showed, f0, is just a combination of f and 0. So, ff wouldbe 255. Get it?

Share this post


Link to post
Share on other sites
Why should learning hex be any harder just because you''re 13? Almost everything is hard in the beginning

You don''t need to learn hex to program games, but it might come in handy when you come across stuff that has hex in it.

A way to easily convert between hex and decimal is to use this nifty little program. You type in a number and it will show up in decimal and hexadecimal.

int main(void)
{
int no;
scanf("%d", &no);
printf("Decimal: %d\nHex: %x", no, no);
}

/. Muzzafarath

Share this post


Link to post
Share on other sites
Except that F0 is 240 decimal. Once you get to F, it starts over at 10, just like at 9 in decimal it starts over at 10.

Basically Assasin, hex is just a representation of the same number. 24 in decimal is the same as 18 hex. The computer has no idea about the different representations, because its all the same actual number in binary. Its just a different, and often times easier, way for us to represent numbers. If you want, use a calculator to convert the hex to decimal for a while until it makes sense.

Rock

Share this post


Link to post
Share on other sites
when the number is for example 255 in decimal, it means
_2_*10^2 + _5_*10^1 + _5_*10^0 (2*100+5*10+5*1).
there are powers of 10

same way for 16base, but there are powers of 16
for example 0x255 is
_2_*16^2 + _5_*16^1 + _5_*16^0 (2*256 + 5*16 +5*1)
so 0x255 hexadecimal is 597 decimal

clear now?

Share this post


Link to post
Share on other sites
man im only taking seq math1 you guys are confusing me. Whats the point of using hex anyways. Why cant we stick to our normal Base 10 numbering system. Hex is confusing a little. BTW i made a program like that that converts regular characters into ASCII characters.
And a better code for that program you made would be

#include
#include

main()
{
int x;
printf("type in the character you would like to convert into hex\n");
printf("You typed: %d in decimal\n You typed: %x in hex\n", x, x);
system("Pause");
return 0;
}


works the same way just that it doesnt close the program in a blink of the eye.

Share this post


Link to post
Share on other sites
umm the two header files you should include are stdio.h and stdlib.h ok, sorry bout that, and what does teh Ox stand for

Share this post


Link to post
Share on other sites
0x tells the C compiler to treat the number as hex and not decimal. Otherwise the compiler wouldn't know what you meant if set a variable equal to 22 (could be 22 decimal, or 34 decimal if treated as hex).

Hex is easier to use for doing things like bit masking (it is easy to read 0x48 and to understand what bits are on and which ones are off). It is very hard to tell at a glance what bits are on for the number 72 decimal. You can use decimal numbers for everything, but there are places where hex is easier to read and understand.

Rock

Edited by - Rock2000 on 3/11/00 12:43:44 PM

Share this post


Link to post
Share on other sites
Expanding on Rock2000''s post...

Each digit in hex corresponds exactly to 4 bits in a binary sequence. i.e.
8 hex corresponds to 0100 binary.
F hex corresponds to 1111 binary
therefore
8F hex corresponds to 01001111 binary.

In decimal, there is no direct correspondance like that. Hex is useful for compressing long binary sequences.
I would rather read F801h than 1111010000000001b.

Mike

Share this post


Link to post
Share on other sites

  • Advertisement