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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-18-2006, 07:32 PM
|
#1
|
|
LQ Newbie
Registered: Aug 2006
Posts: 23
Rep:
|
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
|
|
|
|
08-18-2006, 08:10 PM
|
#2
|
|
Senior Member
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,189
Rep:
|
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.
|
|
|
|
08-19-2006, 01:14 AM
|
#3
|
|
LQ Newbie
Registered: Aug 2006
Posts: 23
Original Poster
Rep:
|
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.
|
|
|
|
08-19-2006, 03:25 AM
|
#4
|
|
Member
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 543
Rep:
|
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 ;
|
|
|
|
08-19-2006, 04:04 AM
|
#5
|
|
Senior Member
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,189
Rep:
|
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?
|
|
|
|
08-20-2006, 02:31 PM
|
#6
|
|
LQ Newbie
Registered: Aug 2006
Posts: 23
Original Poster
Rep:
|
Hi w3bd3vil
I try your method, %s but it still output the decimal value.
|
|
|
|
08-20-2006, 04:43 PM
|
#7
|
|
Senior Member
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 3,824
Rep: 
|
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.
|
|
|
|
08-20-2006, 06:38 PM
|
#8
|
|
LQ Newbie
Registered: Aug 2006
Posts: 23
Original Poster
Rep:
|
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.
|
|
|
|
08-20-2006, 08:16 PM
|
#9
|
|
Senior Member
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,189
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:12 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|