LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   printing 64 bit hex number (https://www.linuxquestions.org/questions/linux-newbie-8/printing-64-bit-hex-number-475240/)

lilzz 08-18-2006 07:32 PM

printing 64 bit hex number
 
Hi
I have a variable
value1=0x5555555555555555; 64 bit
I like to printf("the value is %llx\n", value1);



the output is a decimal value not in hex.
how do I print a hex number from a hex number

%x output a decimal to hex but I need to output a hex from hex

what modifier I should use?

thanks

w3bd3vil 08-18-2006 08:10 PM

If you would do something like this
Quote:

char value1[]="0x5555555555555555";
printf("the value is %s\n", value1);

if this doesnt solve the problem
whats the type of value1?

lilzz 08-19-2006 01:14 AM

If it's static, then I could use your way of representing it as a string. But it's 64 bit variable that being constantly updated. So, I am not sure the string represenation is viable option.

The variable is declared as unsigned long long

AnanthaP 08-19-2006 03:25 AM

What you mean is the DISPLAY representation (in hex) of a hex number.

Something like this (pseudo code)

mynum = value1 ;
mystr = "" ;
b = '' ; (char type)
While mynum != 0
a = mynum modulo 16 ;
If a > 9
b = chr(a+64)
else
b = char(a+47);
EndIf
mystr = b + mystr ;
mynum = (mynum - a) / 16 ;
Endwhile
print mystr ;

w3bd3vil 08-19-2006 04:04 AM

long long value1="0x5555555555555555";
printf("the value is %s\n", value1);

your just printing here, so it wont matter if its a char or long right?

lilzz 08-20-2006 02:31 PM

Hi w3bd3vil
I try your method, %s but it still output the decimal value.

btmiller 08-20-2006 04:43 PM

That's because he assigned a string to a long long which isn't going to work particularly well. Just using %llx works fine for me. Are you sure it's not printing out on hex? It won't print out the leading 0x (you must specify that yourself), so in your example it should just print out 5555555555555555 which would look decimal unless you "knew" it was a hex number.

Also, be sure you're compiling a 64 bit executable with -m64.

lilzz 08-20-2006 06:38 PM

hi btmiller
I try again now the problem is
gcc -c -m64 test.c which would produce the test.o
then gcc test.o -o test
would complain the x64-64 of test.o (/w -m64) incompatible with i386 output.

I try gcc -m64 test.o -o test would give me an error about the ld linker and one of its library.

w3bd3vil 08-20-2006 08:16 PM

Quote:

#include <stdio.h>

void main()
{
long long value1="0x5555555555555555";
printf("the value is %llx\n",value1);
printf("the value is %s\n", value1);
}

output
the value is 80484f4
the value is 0x5555555555555555
I try your method, %s but it still output the decimal value.

value1="0x5555555555555555" --> this must be in quotes
when I dont input the quotes, this what I get
the value is 5555555555555555 using %llx, and segmentation fault with %s


All times are GMT -5. The time now is 01:27 PM.