Convert raw HEX to char

codehead

Joined Nov 28, 2011
57
Sure you can define it as "static char" you get the improvement in instruction count, but pay a penalty in RAM as for some oddball reason C18 seems to put the static string into RAM. So bottom line is stash the string into ROM for the best efficiency.
I mean add "static", not instead. That is, "static const char...". I'm shooting for two things here: 1) to keep the string inside the subroutine instead of splitting it off to a global somewhere else even though it only matters to the subroutine (yuck), and 2) to state it in a way that it would work for your pic and pretty much any C program in general. However, #2 is not such a big deal—it's just because this is a general programming forum and I was shooting for one statement that was generic C and would work for everyone. If it doesn't work for you, add "rom" as well ("static const rom char..."). I did download a lite IDE and compiler for C18 and it did seem to be happy with that. I couldn't figure out how to get a disassembly listing, though, to verify. It seemed to put it in rom for "static const char" as well, but the address was different and I couldn't figure out how to confirm it was in ROM or get an asm listing.

Now before you go thinking I did all this just to prove you wrong, what started me on this was looking to remove 1 or 2 bytes in the string definition: char foo[] = "1234"; is not 4 bytes but 5 as the trailing zero is strapped on the string. As in our case we don't need that zero I was looking at making an array: char foo[4] = {'1','2','3','4'};
I think C is happy with

char foo[4] = "1234";

if you want to not store the trailing null. That is, I know it works, but I don't know offhand if that behavior is mandated in the standard.

Edit:

OK, I did figure out how to get a disassembly during debugging, though I couldn't figure out how to get the whole listing (just the current routine). It looks like it gets loaded into RAM as a static array (because I see the subroutine offset by 16 in memory) unless you add the "rom" qualifier (static const rom char hexchars[16] = "0123456789abcdef";). The code itself gets one byte longer (TBLRD*). Anyway, the bottom line is that "static" saves you from making it global, and I don't think you give up anything else.
 
Last edited:
Top