473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help using sprintf, wanting to make 2 byte lengths and 4 byte lengths to hex string?

jt
Here is my code:

unsigned long listenip;
short tlen;

sprintf(tmp,"len:%x listenip:%x",htonl(tlen),listenip);

If the value on "tlen" is less than 2 bytes and the value on "listenip" is
less than 4 bytes, how can I make it fill with preciding 0 zeros?

Thanks,
JT
Nov 14 '05 #1
3 1772

jt wrote:
Here is my code:

unsigned long listenip;
short tlen;

sprintf(tmp,"len:%x listenip:%x",htonl(tlen),listenip); since tlen is less than 2 bytes, you should use htons() instead.

sprintf(tmp,"len:%02x listenip:%04x",htonl(tlen),listenip);
above should work for your purpose.


If the value on "tlen" is less than 2 bytes and the value on "listenip" is less than 4 bytes, how can I make it fill with preciding 0 zeros?

Thanks,
JT


Nov 14 '05 #2
On 17 May 2005 18:03:40 -0700, "baumann@pan" <ba*********@gmail.com>
wrote:

jt wrote:
Here is my code:

unsigned long listenip;
short tlen;

sprintf(tmp,"len:%x listenip:%x",htonl(tlen),listenip); since tlen is less than 2 bytes, you should use htons() instead.

is _exactly_ 2 bytes, on "normal" systems with 8-bit byte and 16-bit
short, which is not absolutely everywhere but does include the places
sockets functions like these (and maybe Unix I/O in general) are
likely to be available and work as expected.
sprintf(tmp,"len:%02x listenip:%04x",htonl(tlen),listenip);
above should work for your purpose.

htons, as you said; and %04lx (ell ecks) for the listenip; x with no
modifier expects and prints unsigned int which can be as little as 16
bits (2 bytes) and is on some systems, at least one of them socketed.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #3
Dave Thompson <da*************@worldnet.att.net> writes:
On 17 May 2005 18:03:40 -0700, "baumann@pan" <ba*********@gmail.com>
wrote:

[...]
since tlen is less than 2 bytes, you should use htons() instead.

is _exactly_ 2 bytes, on "normal" systems with 8-bit byte and 16-bit
short, which is not absolutely everywhere but does include the placesn
sockets functions like these (and maybe Unix I/O in general) are
likely to be available and work as expected.


I've used Unix systems where short is 32 bits, and others where it's
64 bits (both Crays). Along with CHAR_BIT==8 and the lack of extended
integer types, this implies that there is no 16-bit integer type,
which makes some of the network stuff interesting (it uses bit
fields).

--
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 #4

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

Similar topics

39
2557
by: gtippery | last post by:
Newbie-ish questions - I've been away from C for a _long_ time. It seems to me that there ought to be easier (or at least shorter) ways to do what this does. It does compile & run for me (with...
6
2477
by: jt | last post by:
I need to produce 1 character array from 3 others. I tried sprintf and it terminates on the first 0, null, 0x00 it sees in tmp data. All 3 args print out nice by themselves. By trying to make...
26
4777
by: steve | last post by:
Well I've been working all morning and have finally found the source of my "bus error (signal 10)" errors. The source is odd. The error occurs in any function where I make the function call: ...
1
3503
by: jimjim | last post by:
Hello, I was wondering about the implications of giving as an argument to sprintf a different data type from the one specified in the format argument. This type of question along with some...
7
3287
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
3
4559
by: ern | last post by:
I'm writing to a file called "output.txt" It *should* contain floating point numbers delimited by semi-colons. Instead, it contains Chinese characters. /* HERE IS MY CODE */ FILE...
4
15639
by: Mandragon03 | last post by:
Anyone know if the standard sprintf supports utf8 characters that extend beyond the normal ascii characters? Thanks!
4
1563
by: Paul David Buchan | last post by:
Hello, I'm attempting to write a program to read in database files (.dbf). When I do it all as a single procedure in main, everything works. However, what I really want, is to pass the database...
173
13761
by: Ron Ford | last post by:
I'm looking for a freeware c99 compiler for windows. I had intended to use MS's Visual C++ Express and use its C capability. In the past with my MS products, I've simply needed to make .c the...
0
7199
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
7074
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
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5572
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,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.