Thread: Formatting Hex Output

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    2

    Formatting Hex Output

    SOLVED - Sort of, see edit

    I'm trying to build a simple assembler and have now managed to get everything working except something rather trivial... the correct output format...

    The format must be:

    Address followed by a space followed by Operand and Opcode.

    These must be two 32 bit values (as 8 hex characters). For the Operand/Opcode the first six bits are the Operand and the final two bits are the Opcode. EG:

    00000001 0009D0A1

    To generate this I am using:
    Code:
    printf ("%8.8X %6.6X%2.2X\n", printout, operand, opcode);
    -----------

    Solution involved a slightly roundabout and less than ideal way of doing it but am now using:

    Code:
    char test[7];
    sprintf(test, "%X",-1);
    printf ("%c%c%c%c%c%c\n",test[2],test[3],test[4],test[5],test[6],test[7]);
    Which works fine. Will simply do a check to see if operand < 0 and if so use this style.

    Which is all fine and lovely until the operand is a negative value at which point it will promptly ignore any attempts at following the format and print out 8 characters such as:

    00000001 FFFFFFFF00

    Any idea as for how to fix this?
    Last edited by mvtaylor; 09-05-2010 at 06:02 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    man printf:

    "The unsigned int argument is converted to ... unsigned hexadecimal (x and X) notation."

    Unfortunately, I am not aware of any way to get printf to format a signed hexadecimal number, which, when you think about wouldn't make a lot of sense anyway.

    One way to achieve what you are trying to do, would be to negate the operand and manually prefix a hyphen. Something along the lines of:

    Code:
    if(operand < 0)
        printf("-%X", -operand);
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    35
    How exactly do you want it formatted? Just a hex value with a negative sign?

    Code:
    printf("%8.8X %s%6.6X%2.2X\n", printout, (operand < 0) ? "-" : "", abs(operand), opcode);

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    2
    Basically for -1 it is required to print out:

    FFFFFF

    May not make much sense but just following the specification!


    -----

    Found a solution to the problem via a slightly roundabout way of doing it...


    Code:
    char test[7];
    sprintf(test, "%X",-1);
    printf ("%c%c%c%c%c%c\n",test[2],test[3],test[4],test[5],test[6],test[7]);
    Last edited by mvtaylor; 09-05-2010 at 06:00 AM.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by mvtaylor View Post
    Basically for -1 it is required to print out:

    FFFFFF

    May not make much sense but just following the specification!
    No can do. Printf only accepts length modifiers for the built-in types, none of which are 6 bytes. You'd have to write your own alternative.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 11:20 AM
  2. Replies: 14
    Last Post: 04-06-2006, 12:18 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Formatting output into aligned columns
    By Kheila in forum C++ Programming
    Replies: 8
    Last Post: 11-14-2005, 09:47 PM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 06:50 PM