Storing number in Hex format

Q

qazmlp

I have to store the value of a 'long long' number in Hex format into a buffer.
What is the correct & best way of doing it so?

E.g.: If the number is: 374643194001883136, the stored value should be
0x0533000000000000


What I have currently is something like this:

long long Id = 374643194001883136LL ;
char IdInHexBuf[50]= {0} ;
sprintf( IdInHexBuf , "%#018llx" , Id ) ;

Any comments to improve this code?
 
J

jacob navia

qazmlp said:
I have to store the value of a 'long long' number in Hex format into a buffer.
What is the correct & best way of doing it so?

E.g.: If the number is: 374643194001883136, the stored value should be
0x0533000000000000


What I have currently is something like this:

long long Id = 374643194001883136LL ;
char IdInHexBuf[50]= {0} ;
sprintf( IdInHexBuf , "%#018llx" , Id ) ;

Any comments to improve this code?

It is OK!

"If it ain't broken do not fix it!"
 
E

Emmanuel Delahaye

qazmlp wrote on 26/07/04 :
I have to store the value of a 'long long' number in Hex format into a
buffer.
What I have currently is something like this:

long long Id = 374643194001883136LL ;
char IdInHexBuf[50]= {0} ;
sprintf( IdInHexBuf , "%#018llx" , Id ) ;

Any comments to improve this code?

- Be sure that <stdio.h> is included.
- The '= {0} ;' thing is useless but harmless.
- I'm not sure about the meaning of the '#' in the format string.
- "%llX" expects 'an unsigned long long'.
- Your spacing could be improved... (style-matter)
 
E

Erik de Castro Lopo

qazmlp said:
I have to store the value of a 'long long' number in Hex format into a buffer.
What is the correct & best way of doing it so?

E.g.: If the number is: 374643194001883136, the stored value should be
0x0533000000000000

What I have currently is something like this:

long long Id = 374643194001883136LL ;
char IdInHexBuf[50]= {0} ;
sprintf( IdInHexBuf , "%#018llx" , Id ) ;

Any comments to improve this code?

In this case, using sprintf is OK, but normally you should
use snprintf:

snprintf ( IdInHexBuf , sizeof ( IdInHexBuf ), "%#018llx" , Id ) ;

which can prevent beuffer overflows.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+
"Indeed, I am impressed that Google runs an 8,000 node Linux
cluster, 5 data centers, an extensive network, and a rapidly
evolving application all with a staff of 12."
-- http://research.microsoft.com/~gray/papers/FAAMs_HPTS.doc
 
A

Alan Balmer

qazmlp said:
I have to store the value of a 'long long' number in Hex format into a buffer.
What is the correct & best way of doing it so?

E.g.: If the number is: 374643194001883136, the stored value should be
0x0533000000000000

What I have currently is something like this:

long long Id = 374643194001883136LL ;
char IdInHexBuf[50]= {0} ;
sprintf( IdInHexBuf , "%#018llx" , Id ) ;

Any comments to improve this code?

In this case, using sprintf is OK, but normally you should
use snprintf:

snprintf ( IdInHexBuf , sizeof ( IdInHexBuf ), "%#018llx" , Id ) ;

which can prevent beuffer overflows.
Standard snprintf is new in C99, and may not be supported by the OP's
compiler.
 
E

Emmanuel Delahaye

Alan Balmer wrote on 27/07/04 :
Standard snprintf is new in C99, and may not be supported by the OP's
compiler.

Huh! If the OP's compiler supports long long, chances are that it also
supports snprintf() that belongs to the same version of the definition
of the C-language.
 
A

Alan Balmer

Alan Balmer wrote on 27/07/04 :

Huh! If the OP's compiler supports long long, chances are that it also
supports snprintf() that belongs to the same version of the definition
of the C-language.

There might be a statistical correlation, but no logical one.
 
K

Keith Thompson

Emmanuel Delahaye said:
Alan Balmer wrote on 27/07/04 :

Huh! If the OP's compiler supports long long, chances are that it also
supports snprintf() that belongs to the same version of the definition
of the C-language.

Possibly, but I wouldn't bet on it. Many pre-C99 compilers support
long long; the committee added it to C99 because it was existing
practice. The snprintf() function is almost certainly provided by the
C library, not by the compiler, and they're not necessarily in sync.
 
D

Daniel Haude

On Mon, 26 Jul 2004 15:23:21 -0700,
Alan Balmer said:
qazmlp said:
I have to store the value of a 'long long' number in Hex format into a buffer.
What is the correct & best way of doing it so?

E.g.: If the number is: 374643194001883136, the stored value should be
0x0533000000000000

What I have currently is something like this:

long long Id = 374643194001883136LL ;
char IdInHexBuf[50]= {0} ;
sprintf( IdInHexBuf , "%#018llx" , Id ) ;

Any comments to improve this code?

In this case, using sprintf is OK, but normally you should
use snprintf:

snprintf ( IdInHexBuf , sizeof ( IdInHexBuf ), "%#018llx" , Id ) ;

which can prevent beuffer overflows.
Standard snprintf is new in C99, and may not be supported by the OP's
compiler.

Supported or not; it's important to notice that that sprintf() call it a
buffer overflow waiting to happen. Sure, 50 is an awful lot of digits,
but who knows how long a "long long" will be in 20 years?

Although it's unlikely that this program will be re-compiled in 20
years, potential buffer overflows are something to be very aware of, and
sprintf calls like this one should either be replaced by snprintf, or,
if that is unavailable, by some check for the size of the number to be
sprintfed.

--Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top