Hey guys,

Can you help me with printing char values as Hex (0x00) on LCD. I tried sprintf but seems it is not complete.
Indeed,, Tari et alia are still working on them for the Python project. I think something like the following would work:


Code:
#define PREHEXCHAR(x) ('0'+(((x)>9)?((x)+'a'-'9'+1)):x)

function dispHex(unsigned char c, int x, int y) {
  char tmp[7] = "XX0x00";
  tmp[4] = PREHEXCHAR(c/16);
  tmp[5] = PREHEXCHAR(c%16);
  PrintXY(x,y,tmp,0,TEXT_COLOR_BLACK);
}
Caveat: Untested, just wrote this off the top of my head.
I usually find a LUT to be a bit cleaner in this situation and prefer to do the shift and mask explicitly, but Kerm's code should work too, aside from the part where it seems he's got Javascript on the brain.

Code:
void dispHex(unsigned char c, int x, int y) {
    char map[] = "0123456789ABCDEF";
    char out[] = "XX0x00";
    out[4] = map[(c >> 4) & 0xF];
    out[5] = map[c & 0xF];
    PrintXY(x,y,out,0,TEXT_COLOR_BLACK);
}

When you say that sprintf "seems incomplete", do you mean you're using the 0.3 SDK release and there's no sprintf at all, or did you get the libc implementation from git and it didn't work?
Thank you very much both of you, I ended up using Tari's snippet. When I say incomplete, I saw mentions of it under stdio.h but no implementation.
Couldn't you just use sprintf? Or does that not exist on the Prizm?
blue_bear_94 wrote:
Couldn't you just use sprintf? Or does that not exist on the Prizm?
Didn't you read the rest of the thread? Up until Tari (and Merth?) started putting a stdio together for the Python project, we didn't have any sort of stdio for the Prizm. Now we will.
Then...

Code:

void hex(char* dest,char src)
{
  const char str[]="0123456789ABCDEF";
  strcpy(dest,"0x");
  dest[2]=str[src/16];
  dest[3]=str[src%16];
}
KermMartian wrote:
Indeed,, Tari et alia are still working on them for the Python project. I think something like the following would work:


Code:
#define PREHEXCHAR(x) ('0'+(((x)>9)?((x)+'a'-'9'+1)):x)

function dispHex(unsigned char c, int x, int y) {
  char tmp[7] = "XX0x00";
  tmp[4] = PREHEXCHAR(c/16);
  tmp[5] = PREHEXCHAR(c%16);
  PrintXY(x,y,tmp,0,TEXT_COLOR_BLACK);
}
Caveat: Untested, just wrote this off the top of my head.

Tari wrote:
I usually find a LUT to be a bit cleaner in this situation and prefer to do the shift and mask explicitly, but Kerm's code should work too, aside from the part where it seems he's got Javascript on the brain.

Code:
void dispHex(unsigned char c, int x, int y) {
    char map[] = "0123456789ABCDEF";
    char out[] = "XX0x00";
    out[4] = map[(c >> 4) & 0xF];
    out[5] = map[c & 0xF];
    PrintXY(x,y,out,0,TEXT_COLOR_BLACK);
}

When you say that sprintf "seems incomplete", do you mean you're using the 0.3 SDK release and there's no sprintf at all, or did you get the libc implementation from git and it didn't work?


Way to read, blue_bear. Your code should work, since it's pretty much what everyone else has written, you'd just need to make sure that it's a 5 character string, "0x[digit][digit]\0" if you wanted to print it out.
Here's the one with the terminating null in.

Code:

void hex(char* dest,char src)
{
  const char str[]="0123456789ABCDEF";
  strcpy(dest,"0x");
  dest[2]=str[src/16];
  dest[3]=str[src%16];
  dest[4]=0;
}
Or you can go bleeding-edge and use sprintf again. I've tested basic printf functionality, so this should work fine:

Code:
 char buf[7];
 sprintf(buf, "XX0x%02x", x);
 PrintXY(...);

I haven't yet implemented field width modifiers in printf so it'll be a little funny with small values, but what it won't do is something completely wrong.
that's great news Tari, it's always best to have standard C functions implemented.
hedehede81 wrote:
that's great news Tari, it's always best to have standard C functions implemented.
Definitely. Tari is doing great work, both in terms of helping with libfxcg/stdio and with expanding WikiPrizm. Merthsoft is of course also the other half of the libfxcg/stdio team, and doing his usual brilliant job.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement