Skip to main content
gamedev.net
🔒 Locked

Hex editing

Started by Ultra The Vampire Apr 26, 2003 at 5:34 PM 9 replies 600+ views
Original Post
Ultra The Vampire
Ultra The Vampire
How do i go about making some functions to get, read, and edit hex (hexadecimal) offsets in C++. I am working on a rom editor and this would greatly help me. [edited by - Ultra The Vampire on April 26, 2003 6:38:56 PM]
CWizard
CWizard
I cannot parse that. You want functions to edit offsets??? Or, do you mean edit a value (represented in hexadeciaml) at an offset?

Explain better what you want to achieve, and I''m sure someone will be able to help you out.


drslush
drslush
So basically, you''re writing a hex editor? If all you want is to be able to display a number as hex, then parse a hex number (stored in a string), use sprintf to convert to a string, then use strtol to parse the hex number. Just set the base to 16.

Does this help?
CWizard
CWizard
To make a hex string is really easy. Watch this function:

void long_to_hex_ascii(unsigned long val, char *buffer, int digits)
{
buffer += digits; // buffer points to after our new string
*--buffer = ''\0''; // Terminate it

int digit;
while(digits--) {
digit = val & 0x0000000f; // Get rightmost 4 bits
val >>= 4; // Discard it
if(digit <= 9) digit += ''0''; // Make ascii digits 0-9
else digit += ''A'' - 10; // Make ascii A-F
*--buffer = digit;
}
}

// Although untested, it should work
char buffer[9]; // can store 8 digits
long_to_hex_ascii(65537, buffer, 8);
Notice that this function starts to fill out the hex string at the terminating character and works its way to the left.


CWizard
CWizard
Do I have too much time on my hands? Ehm, anyway here's a function for the other way around:

unsigned long hex_ascii_to_long(const char *buffer, int digits)
{
unsigned long val = 0;

int digit;
while(digits-- && (digit = *buffer)) {
val <<= 4; // start on next digit
if(digit >= '0' && digit <= '9)
digit -= '0'; // numeric value of hex ascii 0-9
else if(digit >= 'A' && digit <= 'F')
digit -= 'A' + 10; // numeric value of hex ascii A-F
else {
// error in string
}
val |= digit; // add this digit to the right of value
}

return val;
}

char hexstring[] = "1337DEAD";
unsigned long = hex_ascii_to_long(hexstring, strlen(hexstring));



[edited by - CWizard on April 28, 2003 8:31:00 PM]
drslush
drslush
Wouldn''t it just be easier to use:

sprintf(buffer, "%X", myInt);

to convert to asciii hex and:

myInt = strtol(buffer, 0, 16);

to convert back? "Work smarter, not harder" ;-)
Ultra The Vampire
Ultra The Vampire
I am making a level editor for a rom, and most of the variables and data, i want edit are in hex. I wanted to read the hex data and then use it to display the levels as they are in the game. i know where all the data is already, i just needed to get the functions, to read, edit, and get the hex values.
drslush how do i implement your way?
CaptainJester
CaptainJester
quote:
Original post by Ultra The Vampire
I am making a level editor for a rom, and most of the variables and data, i want edit are in hex. I wanted to read the hex data and then use it to display the levels as they are in the game. i know where all the data is already, i just needed to get the functions, to read, edit, and get the hex values.
drslush how do i implement your way?


Values are not stored as hex, they are stored as binary. They may then be displayed as hex or decimal or octal etc... Just load the number into a byte sized field and manipulate it.

If the hex numbers are represented as strings and then saved as strings, just load then and parse the hex to a value that is easy for you to use.



First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
antareus
antareus
Ultra The Vampire? Any relation to Trevor The Vampire?
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.