Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to concatenate string and unsigned char ?

Reply
Thread Tools

How to concatenate string and unsigned char ?

 
 
Abby
Guest
Posts: n/a
 
      08-12-2003
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.
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      08-12-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Alex
Guest
Posts: n/a
 
      08-12-2003
Abby <(E-Mail Removed)> 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
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      08-13-2003
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

 
Reply With Quote
 
Jirka Klaue
Guest
Posts: n/a
 
      08-13-2003
Abby wrote:
> Ben Pfaff <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
>>(E-Mail Removed) (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

 
Reply With Quote
 
James Antill
Guest
Posts: n/a
 
      08-19-2003
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) + < 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 -- (E-Mail Removed)
Need an efficent and powerful string library for C?
http://www.and.org/vstr/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
Padding bits and char, unsigned char, signed char Ioannis Vranos C Programming 6 03-29-2008 10:55 AM
Padding bits and char, unsigned char, signed char Ioannis Vranos C++ 11 03-28-2008 10:47 PM
void*, char*, unsigned char*, signed char* Steffen Fiksdal C Programming 1 05-09-2005 02:33 AM



Advertisments