I have a char with 0xFA in it, and i want the program to print -$06 (funny hexadecimal syntax) i was wondering if this can be achieved in sprintf or if i'll need other crap, and if it can be done in sprintf what would be the "%" part
Printable View
I have a char with 0xFA in it, and i want the program to print -$06 (funny hexadecimal syntax) i was wondering if this can be achieved in sprintf or if i'll need other crap, and if it can be done in sprintf what would be the "%" part
printf() or cout, take your pick.
Code:#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;
int main()
{
char a = 0xfa;
if (static_cast<signed char> (a) < 0)
{
printf("-$%02d\n",-a);
cout << "-$" << setw(2) << setfill('0') << -a << endl;
}
else
{
printf("$%02d\n",a);
cout << "$" << setw(2) << setfill('0') << a << endl;
}
}
Isn't there a way to do it using those odd ? things where you have 2 outcomes like an If
[edit] I found how how to do the + and - using sprintf, now i just need to let it handle signed hex numbers, i'm using X atm which is only for unsigned hex numbers
Sure, but you'll get the same assembly code. Looks fancy though. :D
sprintf(,"+%02X",) prints +FA..i want -06 any way to convert it or add to the sprintf?
It's a step in the right direction though. Stupid signed numbers..
>sprintf(,"+%02X",) prints +FA..i want -06
Ever heard of %d ?
It *needs* to be in hex, it'd look out of place, several other hundered numbers are in hex :P
I'm trying to understand what you are really working with.Quote:
Originally Posted by krappykoder
If you try printf or sprintf using this:
you get +$FA. (Not +$FFFA or +$FFFFFFFA, right?)Code:("+$%02X", x)
What if you print using this:
I once worked on a small (nonstandard c-compiler) system that didn't like the unary '-', and the thing that I just showed wouldn't work. You couldn't even say something likeCode:("-$%02X", -x)
You had to say something likeCode:z = -x;
Code:z = 0-x;
So, what if you try your sprintf with this:
Just a few things for you to try. I suggest that you may have to get to the bottom of things yourself, since you are obviously working on some kind of system that is not like what I have. Maybe you could even give us a clue as to what the heck it is???Code:("-$%02X", 0-x)
Regards,
Dave
As Dave suggested, one idea is:
I'll let you or Dave convert it to use the ternary operator.Code:if (num < 0)
sprintf(,"-$%02X",-num);
else
sprintf(,"+$%02X",num);
EDIT: added $ and +
How about something like this?Code:#include <stdio.h>
#include <stdlib.h>
char *foo ( char *dst, char value )
{
sprintf ( dst, "%c$%02X", "-+" [ value >= 0 ], (unsigned char)abs(value) );
return dst;
}
If you really wanted to know what i was working on, it's a gameoy decompiler which decodes 0x78FA into:
ld hl, sp-$06
with a range of:
ld hl, sp-$7F to ld hl, sp+$80
which means i need a signed hex number, which sprintf doesn't seem to have
OK! Cool.Quote:
Originally Posted by krappykoder
I thought you meant that your C compiler was some subset non-standard microthingie (been there... done that).
Well, you are correct: %x is unsigned by its very nature.
Use any of the suggestions given previously to print sign and magnitude. It's not really that ugly is it?
Regards,
Dave
A quick hack:Code:sprintf(dst, "%c$%02X", "-+" [ value >= 0 || value < -127 ],
(unsigned char)abs(value));