Sonic and Sega Retro Message Board: Introduction to Hex - Sonic and Sega Retro Message Board

Jump to content

Hey there, Guest!  (Log In · Register) Help
Loading News Feed...
 

Introduction to Hex

#1 User is offline Tweaker 

Posted 17 January 2006 - 09:41 PM

  • Posts: 12389
  • Joined: 27-June 04
  • Gender:Male
Hey there, Tweaker here. This is a rewrite of a tutorial I wrote at Acmlm's board, fit to work here.

Anyway, first off, you must realize that Hex stands for Hexadecimal, which is a base 16 number system. This is similar to decimal, which is a base 10 number system. So let's learn the numbers, shall we?

Decimal: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Hex: 0 1 2 3 4 5 6 7 8 9 A B C D E F 10

As you can see, counting to 16 in decimal is equvelant to counting to 10 in hex. Hex is pretty much decimal with six extra values tacked on to it.

Now, how do we hack Sonic with hex?

Well, as you may or may not know (hopefully may), everything on a computer is represented by binary code (AKA 001010101110, etc). Of course, editing and viewing ROMs -- or ANYTHING -- in binary alone is extremely impractical (unless you're dealing with bit-based compression algorthms and opcodes...). So as us humans oh-so-hate things being complicated when they don't have to be, we found a way to simplify binary; Using hexadecimal.

Hex is dealt with by the byte in most operations. In case you don't know your terminoligy, let's fix that right now:

Nybble - Half a byte, or a single digit. "A" would be a good example.

Byte - Full hexadecimal value, equal to 8 bits, and two digits. Something like "01" or "1C".

Word - Two bytes, or 16 bits. Need I really explain this more? "010A" or "1550" would be good examples.

Long(word) - Two words, or 4 bytes, or 32 bits. "00012345' would be a good example.

FYI, byte types are advanced in powers of 1,024. 1,024 bytes makes a kilobyte, 1,024 kilobytes makes a megabyte... etc etc down the whole damn chain.


Now that you know your basics, we can start editing and move on to pointers and such.

So first off, what is a pointer? Well, a pointer is pretty much what it's called -- a pointer. It's a hexadecimal string that lists an offset in ROM in which to locate data. (BTW, an offset is just the current byte in ROM that you're at. $012345 [out of $200000, though that bt's redundant] would be an offset.) There are little endian and big endian processors, and the Megadrive happens to use a big endian processor. This means addresses are read as is, so things are a bit easier to deal with. For example, if my pointer read 00123456, first I'd split that into bytes. This becomes 00 12 34 56, which is the address at which the pointer wants to locate data.

Let's make a practical example. Let's say you wanted to edit Sonic's art to add more frames to some animations (which you should edt mappings for later, but :P). You can't add them to the current location because there is vital data after Sonic's art. So what we should do is expand our ROM and add Sonic's art to the end so we can add as many frames as we want.

We'll say (it isn't really) that the location for Sonic's art is at offset $55439. However, we don't know where the pointer to his art is! So guess what? We're gonna find it. Remember how I said pointers are byte seperated offsets in ROM? Well, take the offset of Sonic's art and extend it to a longword. Originally, $55439, it becomes 00 05 54 39. Run a search for this in the ROM and you should get at least one result. We found our pointer!

Now, let's say the end of the ROM is at offset $100000. First we take Sonic's art and stick it at the end of the ROM. Now all we have left to do is to change our pointer to locate our new art set, rather than the old one. So we take our pointer -- currently 00 05 54 39 -- and change it to the correct offset, $100000. So take our offset and extend to a longword and seperate into bytes. This gives us 00 10 00 00. We then replace our pointer with this new value, and viola! Our new art is fully working and intact. You can even delete the old art and it'll still work!

Hope this helps some people understand how to use a hex editor better. I may add more examples if requested, but this should be enough to get you going on your own as a better hacker overall. :)

#2 User is offline LocalH 

Posted 17 January 2006 - 09:45 PM

  • roxoring your soxors
  • Posts: 3119
  • Joined: 11-January 03
  • Gender:Male
  • Location:wouldn't you like to know
  • Project:MDEM - Genesis programming stufz
  • Wiki edits:3
Everybody's got to write at least one hex tutorial in their life. Looks like you finally did yours. =P

#3 User is offline Hivebrain 

Posted 17 January 2006 - 10:41 PM

  • Posts: 2371
  • Joined: 15-January 03
  • Gender:Male
  • Location:53.4N, 1.5W
  • Project:HivePal 2.0
  • Wiki edits:6,176
Here's my tutorial:

Quote

Decimal: 0-9
Hex: 0-F


Any more information is superfluous and likely to confuse beginners.

#4 User is offline Tweaker 

Posted 17 January 2006 - 11:08 PM

  • Posts: 12389
  • Joined: 27-June 04
  • Gender:Male
Obviously there's a difference between "knowing hex" and knowing how to use it in a practical sense. That's what I was trying to clarify with this.

#5 User is offline ThunderPX 

Posted 18 January 2006 - 10:24 AM

  • Posts: 1449
  • Joined: 17-May 05
  • Gender:Female
  • Location:The Netherlands
  • Project:Being stumped by the beginnings of ROM hacking in an attempt to translate a game
  • Wiki edits:20
It's "voila".

#6 User is offline jman2050 

Posted 18 January 2006 - 11:42 AM

  • Teh Sonik Haker
  • Posts: 614
  • Joined: 10-December 05
  • Wiki edits:4
Hex for Beginners:

Start->Programs->Accessories->Calculator

Switch to Scientific mode, then to hex mode, and fire away.

#7 User is offline Tweaker 

Posted 18 January 2006 - 12:37 PM

  • Posts: 12389
  • Joined: 27-June 04
  • Gender:Male

ThunderPX, on Jan 18 2006, 10:24 AM, said:

It's "voila".

Oh STFU. You know what I mean.

#8 User is offline Shade 

Posted 18 January 2006 - 12:37 PM

  • Posts: 2990
  • Joined: 02-November 05
  • Gender:Male
  • Location:California
  • Wiki edits:137
Well, either way, this is nice Tweaker. I have been educating myself on the finer points of Hex whenever I have the time or find the resources, and every little bit helps.

Very nice little guide Tweaker :)

#9 User is offline Aurochs 

Posted 18 January 2006 - 04:43 PM

  • Единый, могучий Советский Союз!
  • Posts: 2343
  • Joined: 09-January 05
  • Gender:Male
  • Project:Whatever catches my fancy
  • Wiki edits:325

Tweaker, on Jan 18 2006, 01:37 PM, said:

ThunderPX, on Jan 18 2006, 10:24 AM, said:

It's "voila".

Oh STFU. You know what I mean.

Come on, you know how defensive Frenchmen get about their language. ;)

I agree with Hivebrain - it should only need to be taught as a different number base. Most of what you have there is about programming, not hexadecimal, and adding it in a hex guide could confuse people.

(I realize that the preceeding paragraph was completely disjointed. I don't care to fix it right now.)

#10 User is offline jman2050 

Posted 18 January 2006 - 04:51 PM

  • Teh Sonik Haker
  • Posts: 614
  • Joined: 10-December 05
  • Wiki edits:4
Well, considering this is a forum mostly about modifying binary programming, data or otherwise, I think teaching Hexadecimal in the context of programming is appropriate enough. Though perhaps Tweaker should disjoin the actual discussion of hexadecimal numbering from the rest of his post.

#11 User is offline LocalH 

Posted 18 January 2006 - 06:56 PM

  • roxoring your soxors
  • Posts: 3119
  • Joined: 11-January 03
  • Gender:Male
  • Location:wouldn't you like to know
  • Project:MDEM - Genesis programming stufz
  • Wiki edits:3
Also, from the programming perspective, it'll be good to cover the simple fact that binary directly translates to hex, 4 bits to a hex digit. I don't think there should be a huge amount of detail on this point, as it will eventually come naturally when working with lots of hex, but it should be mentioned to bring it to the attention of people who aren't as binary-inclined.

In other words, the way Tweaker mentioned it in his discussion of the relationship between bits, nybbles, bytes, and words, is quite good as it is.
This post has been edited by LocalH: 18 January 2006 - 06:57 PM

#12 User is offline Lostgame 

Posted 18 January 2006 - 10:47 PM

  • 'There are feathers everywhere, but it's fine...'
  • Posts: 3911
  • Joined: 02-December 03
  • Gender:Male
  • Location:Toronto, ON
  • Project:LSD-25
  • Wiki edits:2
This info needs to be pinned.

Great job as always, Dustin. :thumbsup:

#13 User is offline Zweih 

Posted 18 January 2006 - 11:10 PM

  • Open the glove box
  • Posts: 387
  • Joined: 12-November 05
  • Gender:Male
  • Project:Calyce Creations
  • Wiki edits:4
Thanks for this Tweaker, this'll help me further understand the basis of ROM hacking =)

#14 User is offline muteKi 

Posted 19 January 2006 - 12:07 AM

  • Fuck it
  • Posts: 7359
  • Joined: 03-March 05
  • Gender:Male
  • Wiki edits:91
I was just thinking today that if Greek philosopher Democritus wanted to discuss programming, he'd probably say something like this:

"Nothing exists except offs and ons [zeroes and ones]. The rest is just commentary."

Meh.

#15 User is offline drx 

Posted 22 January 2006 - 10:06 AM

  • <Shade> fuck MJ
  • Posts: 2100
  • Joined: 02-March 04
  • Gender:Male
  • Project::rolleyes:
  • Wiki edits:8
I wrote like, 3, hex tutorials in my life. LocalH is right XD

  • 2 Pages +
  • 1
  • 2
    Locked
    Locked Forum

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users