🔒 Locked
Hex editing
Advertisement
Original Post
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]
I want to edit a value represented in hexadecimal at an offset.
To make a hex string is really easy. Watch this function:
Notice that this function starts to fill out the hex string at the terminating character and works its way to the left.
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);
Do I have too much time on my hands? Ehm, anyway here's a function for the other way around:

[edited by - CWizard on April 28, 2003 8:31:00 PM]
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]
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?
drslush how do i implement your way?
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
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement