gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > print character with hex values

#108483 - chrissieboy - Thu Nov 09, 2006 11:13 am

Im busy with loading a 3d model binary file.

So right now i read out the hex values from the binary.

In the header of the binary file there is the name of the file, wich i want to read out.

I know a character is 4 bytes, so i must read 4 bytes from the binary i think?

In my hex viewer i see this hex values : F5 1B 00 00 4F 62 6A 65 63 74
these value i want to translate to chars.

I do this :
Code:

iprintf("%04x  ", ((u16*)hovercraft_bin)[i]);


to print the hex value on the screen of the ds,
then it prints :
F5 1B 00 00 4F 62 6A 65 63 74
on my ds screen, but now i want it to print it as characters to display the name.


I did this but it crashes :
Code:

iprintf("%s  ", ((u16*)hovercraft_bin)[i]);


Code:

iprintf("%c  ", ((u16*)hovercraft_bin)[i]);


but all it did not work, does anyone know how to print characters out of an hex value??

(sorry for my bad english im from the netherlands)

#108496 - Lick - Thu Nov 09, 2006 4:19 pm

You'd need to zero the string to signalize to iprintf that the string has ended. So in memory it would look like this 'string\0'.

Simply do this:
Code:
// char array to hold 10 characters and a zero.
char buffer[10 +1] = {0};
// copy 10 characters to the array
memcpy(buffer, hovercraft_bin, 10);
// print the array
iprintf("%s ", buffer);

_________________
http://licklick.wordpress.com

#108498 - chrissieboy - Thu Nov 09, 2006 4:37 pm

THanx works perfect !!

#108500 - Cearn - Thu Nov 09, 2006 4:43 pm

chrissieboy wrote:
I'm busy with loading a 3d model binary file.

So right now i read out the hex values from the binary. In the header of the binary file there is the name of the file, wich i want to read out.

I know a character is 4 bytes, so i must read 4 bytes from the binary i think?

A character is not 4 bytes, a character is one byte (at least in C).
Code:
 C type  | ARM name | typedef | size
char     | byte     | s8/u8   | 1 byte
short    | halfword | s16/u16 | 2 bytes
int/long | word     | s32/u32 | 4 bytes


chrissieboy wrote:
In my hex viewer i see this hex values : F5 1B 00 00 4F 62 6A 65 63 74
these value i want to translate to chars.

Technically you don't 'translate' binary to chars, you just interpret them as such. Hex is also just an interpretation. The file format of the binary is just another interpretation as well of data, which I assume you have the format specs somewhere.To interpret the header, just impose part of the file as such by pointing to it with the appropriate header struct:
Code:
// Assumed header structure, actual header is probably different
typedef struct HEADER
{
    u32 magic;   // possibly size or so
    char name[NAMESIZE]; // name of file
    ... more
};

// In code:
HEADER *hdr= (HEADER*)hovercraft_bin;

Now you can use hdr and its members for interpreting the header's contents. For example, hdr->name would point to the byte 4 of the data and interpret it as a string. I'm assuming this is where the name starts because the bytes "4F 62 6A 65 63 74" are ascii for "Object". Of course, I'm just guessing at the string-size, or whether it's even zero-terminated.
If it is zero-terminated, you can print out the name like this:
Code:
iprintf("%s", hdr->name);


About why these didn't work:
Code:
iprintf("%s  ", ((u16*)hovercraft_bin)[i]);

iprintf("%c  ", ((u16*)hovercraft_bin)[i]);

((u16*)hovercraft_bin) interprets hovercraft_bin as a halfword array, from which you read the ith entry. "%s" expects a pointer, not a halfword. As halfwords make poor substitutes for pointers, it'll fail.
"%c" expects a character (i.e., an 8bit (signed) integer). In this case it'll only use the lower byte of the halfword, so using it in a loop will only give every other character of a string. Another problem is that the name doesn't start at the beginning of the data, which means you'll get strange results at first.

If you're still unfamiliar with datatypes, pointers and how the printf() routines work, I'd suggest trying them out on a PC first, where you have better debugging facilities.

chrissieboy wrote:
(sorry for my bad english im from the netherlands)
Hah! With the pervasiveness of English over here, one could say that English classes start at age 9. Sorry dude, that excuse doesn't work this time :P
(Text is mostly fine, anyway; mostly. Proper capitalization and apostrophes might be nice, though, but meh, this isn't English Lit, so don't sweat it.)

#108502 - chrissieboy - Thu Nov 09, 2006 4:58 pm

thanx Cearn!!

Im going to read your answer tonight again, because its a lot information at once for me ;)

But it al starts to make more sense to me!!

Thanx for spending your time to teach me something!