LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 08-18-2006, 07:32 PM   #1
lilzz
LQ Newbie
 
Registered: Aug 2006
Posts: 23

Rep: Reputation: 15
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
 
Old 08-18-2006, 08:10 PM   #2
w3bd3vil
Senior Member
 
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,189

Rep: Reputation: 49
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?

Last edited by w3bd3vil; 08-18-2006 at 08:16 PM.
 
Old 08-19-2006, 01:14 AM   #3
lilzz
LQ Newbie
 
Registered: Aug 2006
Posts: 23

Original Poster
Rep: Reputation: 15
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

Last edited by lilzz; 08-19-2006 at 01:16 AM.
 
Old 08-19-2006, 03:25 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 621

Rep: Reputation: 136Reputation: 136
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 ;
 
Old 08-19-2006, 04:04 AM   #5
w3bd3vil
Senior Member
 
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,189

Rep: Reputation: 49
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?
 
Old 08-20-2006, 02:31 PM   #6
lilzz
LQ Newbie
 
Registered: Aug 2006
Posts: 23

Original Poster
Rep: Reputation: 15
Hi w3bd3vil
I try your method, %s but it still output the decimal value.
 
Old 08-20-2006, 04:43 PM   #7
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,078

Rep: Reputation: 297Reputation: 297Reputation: 297
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.
 
Old 08-20-2006, 06:38 PM   #8
lilzz
LQ Newbie
 
Registered: Aug 2006
Posts: 23

Original Poster
Rep: Reputation: 15
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.
 
Old 08-20-2006, 08:16 PM   #9
w3bd3vil
Senior Member
 
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,189

Rep: Reputation: 49
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
32 bit or 64 bit install - is 32 bit easier for a newbie? dms05 Linux - Newbie 3 05-19-2006 03:05 PM
Good distro for 64-bit number munching + desktopping? Entropius Linux - Distributions 3 09-08-2004 12:24 PM
how do you edit your virtual console number? (or VT number 3) jjorloff1 Linux - General 2 04-03-2004 07:21 PM
why there is a need for minor number and major number for monitor/keyboard in pc? tripathi Solaris / OpenSolaris 1 11-07-2003 09:36 AM
Printing a line number gummimann Programming 2 11-06-2003 06:09 AM


All times are GMT -5. The time now is 11:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration