Problems with sprintf, need help on this one

J

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!
 
M

Malcolm

jt said:
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).
 
M

Mark McIntyre

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.
 
J

jt

Malcolm said:
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
 
O

Old Wolf

jt said:
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' .
 
J

jt

Old Wolf said:
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.
 
J

Jens.Toerring

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
 

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

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top