How to concatenate string and unsigned char ?

A

Abby

Hi,

I've got the following ...

char user[10] = "root";
unsigned long session = 0x0012453b;
char result[20];

How can I concatenate user and session to result? Thank you.
 
B

Ben Pfaff

char user[10] = "root";
unsigned long session = 0x0012453b;
char result[20];

How can I concatenate user and session to result? Thank you.

sprintf(result, "%s%lx", user, session);

Be sure to allocate enough space in the result string.
 
M

Martin Ambuhl

Abby said:
Hi,

I've got the following ...

char user[10] = "root";
unsigned long session = 0x0012453b;
char result[20];

How can I concatenate user and session to result? Thank you.

#include <stdio.h>

int main(void)
{
char user[10] = "root";
unsigned long session = 0x0012453b;
char result[20];
sprintf(result, "%s%#.8lx", user, session);
printf("\"%s\" and %#.8lx combined to yield\n"
" \"%s\"\n", user, session, result);
return 0;
}

[output]
"root" and 0x0012453b combined to yield
"root0x0012453b"
 
J

Jirka Klaue

Abby said:
Ben Pfaff said:
[email protected] (Abby) said:
char user[10] = "root";
unsigned long session = 0x0012453b;
char result[20];

How can I concatenate user and session to result? Thank you.

sprintf(result, "%s%lx", user, session);

Be sure to allocate enough space in the result string.

What if I got

unsigned char session[4];
session[0] = 0x00;
session[1] = 0x12;
session[2] = 0x45;
session[3] = 0x3B;

instead of unsigned long session = 0x0012453b

even worse, how about if I have a very long array like session[100],
which I need to concatenate all of them to "result". Do I need to
write the sprintf for all session[0] - session[99] manually?? Please
advise. Thank you so much!!

char s[42] = "root0x", *p = s + strlen(s);

unsigned char session[4];
session[0] = 0x00;
session[1] = 0x12;
session[2] = 0x45;
session[3] = 0x3B;

for (i=0; i<4; i++, p+=2) sprintf(p, "%02x", session);

Jirka
 
J

James Antill

Sorry to ask another silly question. After above, I need to
concatenate another string characters to "p". How can I do that? I
tried to use strncpy, but it didn't work. I think I really miss
concept about data type. Please help me figure this thing out.

What I need to do is
[snip ... ]

As you should have been able to work our from what was said to you
before, you can use something like...

char buf[128]; /* This _needs_ to be big enough */

/* so assume we test it with the largest s possible */
assert(((strlen(s) * 2) + 8) < sizeof(buf));

sprintf(buf,
"%s"
"%02x%02x%02x%02x"
"%02x%02x%02x%02x"
"%s",
s,
session[0], session[1], session[2], session[3],
id[0], id[1], id[2], id[3],
s);

....which will do what you want, however I'd recommend you take a deeper
look into how pointers and memory work in C ... and also look at using a
real dynamically allocated string type. See...

http://www.and.org/vstr/security.html
http://www.and.org/vstr/comparison.html
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top