473,654 Members | 3,280 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,"le n:%x listenip:%x",ht onl(tlen),liste nip);

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 1785

jt wrote:
Here is my code:

unsigned long listenip;
short tlen;

sprintf(tmp,"le n:%x listenip:%x",ht onl(tlen),liste nip); since tlen is less than 2 bytes, you should use htons() instead.

sprintf(tmp,"le n:%02x listenip:%04x", htonl(tlen),lis tenip);
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@pa n" <ba*********@gm ail.com>
wrote:

jt wrote:
Here is my code:

unsigned long listenip;
short tlen;

sprintf(tmp,"le n:%x listenip:%x",ht onl(tlen),liste nip); 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,"le n:%02x listenip:%04x", htonl(tlen),lis tenip);
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.ne t
Nov 14 '05 #3
Dave Thompson <da************ *@worldnet.att. net> writes:
On 17 May 2005 18:03:40 -0700, "baumann@pa n" <ba*********@gm ail.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_Keit h) 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
2594
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 PowerC, a 16-bit DOS compiler); if there are nonstandard or "accidentally-works" aspects, please let me know. {This is the sort of situation where if I knew *what* to Google for, I wouldn't *need* to... <grin>}
6
2494
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 the character array(alerts.msg) with sprintf doesn't work for the obvious reasons in my first sentence with tmp having those control characters. Is there another way to do this? To accomplish the same thing that sprintf does but able to do having...
26
4799
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: (void)sprintf(ptr_testing, "This is my string"); This in itself isn't where the actual error occurs. The error occurs at any later point, in the same function where the sprintf() call is made, where I try to assign a value to one of the...
1
3519
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 others are asked below: 1. #include <stdio.h> int main(){ char buffer;
7
3301
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 buffer into the character pointer. The code looks like the following: #include <stdio.h> #include <stdlib.h> #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
3
4566
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 *fileHandle; const char * filePath = "C:\\output.txt"; const char delimiter = {';'}; char power_string;
4
15675
by: Mandragon03 | last post by:
Anyone know if the standard sprintf supports utf8 characters that extend beyond the normal ascii characters? Thanks!
4
1570
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 filename to a function, and have it pass back an array containing the database contents, and some parameters telling me the dimensions of the array. I've succeeded in getting my function to read in the dbf file, and it returns the dimensions of...
173
13882
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 filetype to invoke the C compiler. Here's a link http://www.microsoft.com/express/download/#webInstall The download is 2.6 megs, which is near a reasonable size for a compiler, but then setup.exe wants to download 87 megs of dot net framework...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7307
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.