By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,362 Members | 3,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,362 IT Pros & Developers. It's quick & easy.

Problems with sprintf, need help on this one

P: n/a
jt
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 control characters in tmp?

sprintf( alerts.msg , "%04x%06x%s", htonl(alerts.len), listenip, tmp);

Thanks!
Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"jt" <jt****@hotmail.com> wrote
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 control characters in tmp?

sprintf( alerts.msg , "%04x%06x%s", htonl(alerts.len), listenip, tmp);

You've told sprintf to treat alerts.msg as a pointer to a buffer containing
space for your string. It then treats the return from htonl() as an integer,
listenip as an integer, and tmp as a pointer to a NUL-terminated string.
Bascially it will concatenate them, and put the result in altert.msg.

If tmp points to a concatenated list of NUL-terminate strings, you must have
some way of telling the computer how many strings are in the list. (Is it
terminated with a double NUL, or is the count held somewhere?)

If the list is of variable length sprintf() is no longer suitable because it
is not possible to build an argument list of the form

sprintf("%s%s%s ... ", atr1, str2, str3 ... etc)
at runtime for an arbitrary number of strings.

What you can do is call sprintf() to convert the integers to strings, and
then run through the list calling strcat().

eg

char *ptr;
int Nintemp = /* number of strings contained in tmp */

sprintf(alerts.msg, "%4x%6x", htonl(alerts.len), listenip);

ptr = tmp;
for(i=0;i<Nintemp;i++)
{
strcat(alerts.msg, ptr);
ptr += strlen(ptr);
ptr++;
}

(If you tmp is terminated by a double NUL or there is some other rule you
will have to modify this code slightly).

Nov 14 '05 #2

P: n/a
On Sun, 22 May 2005 00:05:27 GMT, in comp.lang.c , "jt"
<jt****@hotmail.com> wrote:
I need to produce 1 character array from 3 others.
strcat?
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.
good.

Please provide the definitions of alerts.len, listenip and tmp.
sprintf( alerts.msg , "%04x%06x%s", htonl(alerts.len), listenip, tmp);


why are you using %x? Thats for printing unsigned ints in hexadecimal
format. Passing a string to that would be an error.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #3

P: n/a
jt

"Malcolm" <re*******@btinternet.com> wrote in message
news:d6**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...

"jt" <jt****@hotmail.com> wrote
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 control characters in tmp?

sprintf( alerts.msg , "%04x%06x%s", htonl(alerts.len), listenip, tmp);

You've told sprintf to treat alerts.msg as a pointer to a buffer
containing space for your string. It then treats the return from htonl()
as an integer, listenip as an integer, and tmp as a pointer to a
NUL-terminated string.
Bascially it will concatenate them, and put the result in altert.msg.

If tmp points to a concatenated list of NUL-terminate strings, you must
have some way of telling the computer how many strings are in the list.
(Is it terminated with a double NUL, or is the count held somewhere?)

If the list is of variable length sprintf() is no longer suitable because
it is not possible to build an argument list of the form

sprintf("%s%s%s ... ", atr1, str2, str3 ... etc)
at runtime for an arbitrary number of strings.

What you can do is call sprintf() to convert the integers to strings, and
then run through the list calling strcat().

eg

char *ptr;
int Nintemp = /* number of strings contained in tmp */

sprintf(alerts.msg, "%4x%6x", htonl(alerts.len), listenip);

ptr = tmp;
for(i=0;i<Nintemp;i++)
{
strcat(alerts.msg, ptr);
ptr += strlen(ptr);
ptr++;
}

(If you tmp is terminated by a double NUL or there is some other rule you
will have to modify this code slightly).


The sprintf(alerts.msg, "%4x%6x", htonl(alerts.len), listenip); work fine,
but after that it just all nulls with trying to append the tmp array.
Remember, tmp array contains, NUL's in it.
jt

Nov 14 '05 #4

P: n/a
jt wrote:

The sprintf(alerts.msg, "%4x%6x", htonl(alerts.len), listenip);
work fine, but after that it just all nulls with trying to append
the tmp array. Remember, tmp array contains, NUL's in it.


sprintf (and indeed, all the string functions in C) stop working
when they encounter a NUL. So you need to reconsider what function
you are using. I would suggest 'memcpy' or 'memset' .

Nov 14 '05 #5

P: n/a
jt

"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
jt wrote:

The sprintf(alerts.msg, "%4x%6x", htonl(alerts.len), listenip);
work fine, but after that it just all nulls with trying to append
the tmp array. Remember, tmp array contains, NUL's in it.


sprintf (and indeed, all the string functions in C) stop working
when they encounter a NUL. So you need to reconsider what function
you are using. I would suggest 'memcpy' or 'memset' .


I can't use memcpy or memset either because I would have to specify where to
start the copy.
Nov 14 '05 #6

P: n/a
jt <jt****@hotmail.com> wrote:
"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
jt wrote:

The sprintf(alerts.msg, "%4x%6x", htonl(alerts.len), listenip);
work fine, but after that it just all nulls with trying to append
the tmp array. Remember, tmp array contains, NUL's in it.
sprintf (and indeed, all the string functions in C) stop working
when they encounter a NUL. So you need to reconsider what function
you are using. I would suggest 'memcpy' or 'memset' .

I can't use memcpy or memset either because I would have to specify where to
start the copy.


As you need to know when you use sprintf(). You append to the end of
the string 'alerts.msg' and you copy from what 'tmp' is pointing to.
All you extra knowledge you need is how many bytes are stored in the
buffer 'tmp' points to.

And be aware that resulting char array (I don't use the word string
because what you will get out of this isn't a string since it may
have embedded '\0' characters) can't be used with functions like
printf() etc. since they all expect a '\0'- terminated string. But
I have the suspicion that you actually want to try just that to be
able to print out what you received. If that is the case then you
will have to convert all the contents of the buffer 'tmp' points to
into some kind of printable characters. Since you seem to like hex
perhaps something like

int tmp_len; /* must be set to the length of 'tmp' */
int i;
char *dest;

sprintf( alerts.msg, "%4x%6x", htonl( alerts.len ), listenip );
dest = alerts.msg + strlen( alerts.msg );
for ( i = 0; i < tmp_len, dest += 2, i++ )
sprintf( dest, "%02x", ( unsigned char ) tmp[ i ] );

will do. Make sure that the 'alerts.msg' string is long enough or
you'll get in trouble.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.