sprintf

This is a discussion on sprintf within the C++ Programming forums, part of the General Programming Boards category; I have a char with 0xFA in it, and i want the program to print -$06 (funny hexadecimal syntax) i ...

  1. #1
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    sprintf

    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

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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;
       }
    }

  3. #3
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154
    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
    Last edited by krappykoder; 11-18-2004 at 01:15 PM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Sure, but you'll get the same assembly code. Looks fancy though.

  5. #5
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154
    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..

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >sprintf(,"+%02X",) prints +FA..i want -06
    Ever heard of %d ?

  7. #7
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154
    It *needs* to be in hex, it'd look out of place, several other hundered numbers are in hex :P

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by krappykoder
    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.

    If you try printf or sprintf using this:
    Code:
    ("+$%02X", x)
    you get +$FA. (Not +$FFFA or +$FFFFFFFA, right?)

    What if you print using this:
    Code:
    ("-$%02X", -x)
    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 like
    Code:
    z = -x;
    You had to say something like
    Code:
    z = 0-x;

    So, what if you try your sprintf with this:

    Code:
    ("-$%02X", 0-x)
    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???

    Regards,

    Dave
    Last edited by Dave Evans; 11-18-2004 at 03:29 PM.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    As Dave suggested, one idea is:
    Code:
    if (num < 0)
       sprintf(,"-$%02X",-num);
    else
       sprintf(,"+$%02X",num);
    I'll let you or Dave convert it to use the ternary operator.

    EDIT: added $ and +
    Last edited by swoopy; 11-18-2004 at 04:43 PM.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154
    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

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by krappykoder
    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.

    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

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A quick hack:
    Code:
     sprintf(dst, "%c$%02X", "-+" [ value >= 0 || value < -127 ],
               (unsigned char)abs(value));
    Last edited by Dave_Sinkula; 11-19-2004 at 08:13 AM. Reason: [1] Swapped order of evaluation. [2] Changed 'value == -128' to 'value < -127'.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 09:21 AM
  3. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 03:14 AM
  4. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 12:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21