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

How to concatenate string and unsigned char ?

P: n/a
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.
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ab*****@yahoo.com (Abby) writes:
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.
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons
Nov 13 '05 #2

P: n/a
Abby <ab*****@yahoo.com> wrote:
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.


Sure, look at 'sprintf'.

Alex
Nov 13 '05 #3

P: n/a
Abby wrote:
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"
--
Martin Ambuhl

Nov 13 '05 #4

P: n/a
Abby wrote:
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@pfaff.stanford.edu>...
ab*****@yahoo.com (Abby) writes:
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[i]);

Jirka

Nov 13 '05 #5

P: n/a
On Mon, 18 Aug 2003 12:22:27 -0700, Abby wrote:
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

--
James Antill -- ja***@and.org
Need an efficent and powerful string library for C?
http://www.and.org/vstr/

Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.