How to concatenate string and unsigned char ?

Discussion in 'C Programming' started by Abby, Aug 13, 2003.

  1. Abby

    Abby Guest

    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.
    Abby, Aug 13, 2003
    #1
    1. Advertising

  2. Abby

    Ben Pfaff Guest

    (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
    Ben Pfaff, Aug 13, 2003
    #2
    1. Advertising

  3. Abby

    Alex Guest

    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.


    Sure, look at 'sprintf'.

    Alex
    Alex, Aug 13, 2003
    #3
  4. 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
    Martin Ambuhl, Aug 13, 2003
    #4
  5. Abby

    Jirka Klaue Guest

    Abby wrote:
    > Ben Pfaff <> wrote in message news:<>...
    >> (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);

    Jirka
    Jirka Klaue, Aug 13, 2003
    #5
  6. Abby

    James Antill Guest

    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 --
    Need an efficent and powerful string library for C?
    http://www.and.org/vstr/
    James Antill, Aug 19, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    568
    Jack Klein
    May 9, 2005
  2. Ioannis Vranos
    Replies:
    11
    Views:
    742
    Ioannis Vranos
    Mar 28, 2008
  3. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    599
    Ben Bacarisse
    Mar 29, 2008
  4. Alex Vinokur
    Replies:
    9
    Views:
    768
    James Kanze
    Oct 13, 2008
  5. Carlos

    Concatenate/De-Concatenate

    Carlos, Oct 12, 2012, in forum: VHDL
    Replies:
    10
    Views:
    853
Loading...

Share This Page