Emmanuel Morales
Emmanuel Morales

Reputation: 11

negative integer/float decimal number to signed hex and octal in standard C code

How do you turn a negative decimal number to a signed octal/hex number.

This is a portion of my code and its output:

#include <stdio.h>
#include <float.h>
#include <limits.h>


int main()
{

signed short int sui1 = -127;

printf("+------------+-----+-----+------------+------------+------------+");
printf("------------+------------+\n");
printf("%s %11s %6s %5s %12s" , "Type", "Var", "Size", "Dec.", "Hex."  );
printf("%13s %10s %14s", "Oct.", "FP", "Exp\n");


printf("%s %10s %4i %6i %#14x" , "char", "c1", sizeof(c1), c1, c1); 
printf("%#13o %10f %22e\n", c1, c1, c1);

return 0;
}

the output for my octal and hexadecimal do not have a negative number like I was hoping, any suggestions?

decimal= -128

octal=0177601

hex=0xff81

Upvotes: 1

Views: 4776

Answers (2)

chux
chux

Reputation: 153557

The "%o" and "%x" format specifiers expect unsigned matching arguments. So even if code passes an signed short int with a negative value, that value is eventual converted to unsigned before printing. Thus only positive values (and 0) are printed.

To print a negative number in base-16 (or base-8 with "%o"), code could use;

signed short int sui1 = -127;
printf("%c%x", sui1 < 0 ? '-' : '+', (unsigned) abs(sui1));

Upvotes: 0

Let's take the example below for the binary number in 8-bits to make things more clear :

11111111

if it is a considered as a signed number then the decimal value will be -1

11111111
||||||||---> +1*2^0
|||||||----> +1*2^1
||||||-----> +1*2^2
|||||------> +1*2^3
||||-------> +1*2^4
|||--------> +1*2^5
||---------> +1*2^6
|----------> -1*2^7
             -------
               -1

because the left most bit will be considered as a sign bit and taken as negative, but the rest (first 7 bits) will stay always positive, when getting the value in base 10 as it is shown above

if it is an unsigned bit than all the bits will be positive to get the value in base 10 whic is 255

11111111
||||||||---> +1*2^0
|||||||----> +1*2^1
||||||-----> +1*2^2
|||||------> +1*2^3
||||-------> +1*2^4
|||--------> +1*2^5
||---------> +1*2^6
|----------> +1*2^7
             -------
               255

and as you can see the binary number 11111111 is the same in binary notation for signed and unsigned, (even in octal notation (0377) and hexadecimal notation (0xFF)) but different in decimal notation which depends on what you consider it as a signed number or unsigned number

Here is a program that converts signed decimal to hexadecimal and octal notation

#include <stdio.h>

int main()
{
    int nb;
    printf("please enter the decimal number: ");
    scanf("%d",&nb);
    char octal[100]="",hex[100]="";
    //octal will hold the octal notation 
    //hex will hold the hexadecimal notation
    sprintf(octal,"%#o",nb); //convert decimal to octal 
    sprintf(hex,"%#X",nb); // convert decimal to hexadecimal 

    // show the result 
    printf("converting %d to hexadecimal notation %s\n",nb,hex);
    printf("converting %d to octal notation %s\n",nb,octal);

    return 0;
}

Upvotes: 2

Related Questions