473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Storing number in Hex format

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?
Nov 14 '05 #1
10 2815

"qazmlp" <qa********@rediffmail.com> a écrit dans le message de
news:db**************************@posting.google.c om...
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!"
Nov 14 '05 #2
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)

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #3
qazmlp wrote:

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 no****@mega-nerd.com (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/...FAAMs_HPTS.doc
Nov 14 '05 #4
On Tue, 27 Jul 2004 07:51:24 +1000, Erik de Castro Lopo
<no****@mega-nerd.com> wrote:
qazmlp wrote:

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.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #5
Alan Balmer wrote on 27/07/04 :
long long Id = 374643194001883136LL ;
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.


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.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #6
On Tue, 27 Jul 2004 00:57:31 +0200, Emmanuel Delahaye
<em***@YOURBRAnoos.fr> wrote:
Alan Balmer wrote on 27/07/04 :
long long Id = 374643194001883136LL ; 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.


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.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #7
Emmanuel Delahaye <em***@YOURBRAnoos.fr> writes:
Alan Balmer wrote on 27/07/04 :
long long Id = 374643194001883136LL ; 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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
Alan Balmer wrote on 27/07/04 :
On Tue, 27 Jul 2004 00:57:31 +0200, Emmanuel Delahaye
<em***@YOURBRAnoos.fr> wrote:
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.


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


Come on...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #9
Emmanuel Delahaye <em***@YOURBRAnoos.fr> writes:
Alan Balmer wrote on 27/07/04 :
On Tue, 27 Jul 2004 00:57:31 +0200, Emmanuel Delahaye
<em***@YOURBRAnoos.fr> wrote:
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.


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


Come on...


Perhaps you could expand on that. (See my response elsethread.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10
On Mon, 26 Jul 2004 15:23:21 -0700,
Alan Balmer <al******@att.net> wrote
in Msg. <o0********************************@4ax.com>
On Tue, 27 Jul 2004 07:51:24 +1000, Erik de Castro Lopo
<no****@mega-nerd.com> wrote:
qazmlp wrote:

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

--
"With me is nothing wrong! And with you?" (from r.a.m.p)
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Steven | last post by:
Hi there, I am wanting to store price data as n.nn format, so if the user enters "1" the data that gets stored is "1.00" Is there a way to do this. Cheers Steven
3
by: TP | last post by:
Hi, In SQL Server 2000, if I have to create a table to store a very large number of 10 digit telephone numbers, would I be better off to have a bigint field or just use a varchar field? If I do...
7
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I...
2
by: JP SIngh | last post by:
Hi All I am writing an ASP application that deals with the TV tapes and two of the fields is what I am having issues with I need to store the duration of the program i.e. DURATION field only...
3
by: hamvil79 | last post by:
I'm implementig a java web application using MySQL as database. The main function of the application is basically to redistribuite documents. Those documents (PDF, DOC with an average size around...
14
by: Luiz Antonio Gomes Pican?o | last post by:
How i can store a variable length data in file ? I want to do it using pure C, without existing databases. I'm thinking to use pages to store data. Anyone has idea for the file format ? I...
1
by: Mark Smith | last post by:
Hi Group, Are there any examples of class for storing fixed width number strings such as phone number and social security numbers. This class would do thing like valid that the number is all...
5
by: netcoder77 | last post by:
Has anyone tried this in VB .NET or via VBScript? Can it be done? How do we handle retrieving a binary data format (the photo) using ADSI or VB .NET? All my searching on the net yielded no useful...
2
by: pavanip | last post by:
Hi, I am developing one application in that i have to develop inbox and outbox functionality. I have to display message like the following format "Dear member, This is a notice to let...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.