'unsigned long' to 'char[]' array

Discussion in 'C Programming' started by Mark, Aug 2, 2010.

  1. Mark

    Mark Guest

    Hello,

    I need to convert values from unsigned long array into char buffer, and
    include one space symbol between the elements, i.e.

    char buf[55];
    unsigned long p[NUM] = {1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2,2, 2, 2, 2, 2, 0};

    should get buf={1 0 0 0 0 1 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0}

    but tricks with sprintf() don't seem to work, the target buffer doesn't
    include spaces. What am I doing wrong ? Thanks.

    int i;
    char buf[55] = {'0'};
    unsigned long p[NUM] = {1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2,2, 2, 2, 2, 2, 0};

    for (i = 0; i < NUM; i++) {
    int n;
    n = sprintf(&buf, "%lu", p);
    sprintf(&buf + n, " ");
    }

    --
    Mark
     
    Mark, Aug 2, 2010
    #1
    1. Advertising

  2. "Mark" <> writes:

    > I need to convert values from unsigned long array into char buffer,
    > and include one space symbol between the elements, i.e.
    >
    > char buf[55];
    > unsigned long p[NUM] = {1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 2, 2, 2, 2, 2,
    > 2, 2, 2, 2,2, 2, 2, 2, 2, 0};
    >
    > should get buf={1 0 0 0 0 1 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0}
    >
    > but tricks with sprintf() don't seem to work, the target buffer
    > doesn't include spaces. What am I doing wrong ? Thanks.
    >
    > int i;
    > char buf[55] = {'0'};
    > unsigned long p[NUM] = {1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 2, 2, 2, 2, 2,
    > 2, 2, 2, 2,2, 2, 2, 2, 2, 0};
    >
    > for (i = 0; i < NUM; i++) {
    > int n;
    > n = sprintf(&buf, "%lu", p);
    > sprintf(&buf + n, " ");
    > }


    Just "play computer" (or use a debugger). Where does the first unsigned
    long go when i is 0? What about the second one?

    --
    Ben.
     
    Ben Bacarisse, Aug 2, 2010
    #2
    1. Advertising

  3. Mark

    Mark Guest

    Ben Bacarisse wrote:
    >> int i;
    >> char buf[55] = {'0'};
    >> unsigned long p[NUM] = {1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 2, 2, 2, 2, 2,
    >> 2, 2, 2, 2,2, 2, 2, 2, 2, 0};
    >>
    >> for (i = 0; i < NUM; i++) {
    >> int n;
    >> n = sprintf(&buf, "%lu", p);
    >> sprintf(&buf + n, " ");
    >> }

    >
    > Just "play computer" (or use a debugger). Where does the first
    > unsigned long go when i is 0? What about the second one?


    p[0] = 1 -> buf[0]
    p[0] + n = ' '
    p[1] - I see, here I overwrite the space set above.

    --
    Mark
     
    Mark, Aug 2, 2010
    #3
  4. Mark

    Guest

    Mark <> wrote:
    >
    > but tricks with sprintf() don't seem to work, the target buffer doesn't
    > include spaces. What am I doing wrong ?


    Your sprintf is hard-coded to go into a certain position in the buffer
    which overwrites the space you carefully (although very inefficiently)
    added.

    > int i;
    > char buf[55] = {'0'};
    > unsigned long p[NUM] = {1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    > 2, 2,2, 2, 2, 2, 2, 0};
    >
    > for (i = 0; i < NUM; i++) {
    > int n;
    > n = sprintf(&buf, "%lu", p);
    > sprintf(&buf + n, " ");
    > }


    You need to walk a pointer through the buffer so you don't overwrite
    things. Something like:

    char p = buf;
    for (i = 0; i < NUM; i++) {
    p += sprintf(p, "%lu ", p);
    }
    if (p > buf) p[-1] = '\0';

    Note that you need to ensure that buf is big enough to hold the output,
    which may be non-trivial.
    --
    Larry Jones

    I've got to start listening to those quiet, nagging doubts. -- Calvin
     
    , Aug 2, 2010
    #4
  5. On Sun, 1 Aug 2010 23:19:08 -0400, wrote:

    > Mark <> wrote:
    > >
    > > but tricks with sprintf() don't seem to work, the target buffer doesn't
    > > include spaces. What am I doing wrong ?

    >
    > Your sprintf is hard-coded to go into a certain position in the buffer
    > which overwrites the space you carefully (although very inefficiently)
    > added.
    >
    > > int i;
    > > char buf[55] = {'0'};


    It's odd to put a single digit zero in a buffer like this, and then
    overwrite it. You might have meant to initially put a null terminator
    at the beginning of the buffer, which is a common and usually good
    practice, but that's '\0' or 0, which are NOT the same as '0'.

    Also, FYI, this actually initializes the whole array: the first char
    to '0' and all remaining chars to 0 aka '\0' aka NUL. That has
    advantages and disadvantages, but if it isn't what you want, do an
    executable statement (after all declarations for C<=89) buf[0] = 0.

    > > unsigned long p[NUM] = {1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    > > 2, 2,2, 2, 2, 2, 2, 0};
    > >
    > > for (i = 0; i < NUM; i++) {
    > > int n;
    > > n = sprintf(&buf, "%lu", p);
    > > sprintf(&buf + n, " ");
    > > }

    >
    > You need to walk a pointer through the buffer so you don't overwrite
    > things. Something like:
    >

    Pointer or offset; pointer is more popular/idiomatic, but I've worked
    on a system where offset was (usually) preferable.

    > char p = buf;
    > for (i = 0; i < NUM; i++) {
    > p += sprintf(p, "%lu ", p);
    > }
    > if (p > buf) p[-1] = '\0';
    >

    But not a pointer named p if the data is also named p as here.
    Although giving the data a more descriptive name is even better.

    That is the general solution, and thus the one to remember if you
    (can) remember only one. If you know each numeric value will be only
    one digit as in the example posted, it could make sense to do:
    for( i = 0; i < NUM; i++ ) sprintf (&buf[i*2], "%c ", '0'+data);
    or even
    for( i = 0; i < NUM; i++ ){ buf[i*2]='0'+data; buf[i*2+1]=' '; }

    > Note that you need to ensure that buf is big enough to hold the output,
    > which may be non-trivial.


    Indeed. But not all that hard either.
     
    David Thompson, Aug 11, 2010
    #5
    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. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    750
    Eric Sosman
    Jul 8, 2003
  2. Replies:
    1
    Views:
    472
    Diez B. Roggisch
    Jun 1, 2005
  3. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,248
    Peter Shaggy Haywood
    Sep 20, 2005
  4. Alex Vinokur
    Replies:
    9
    Views:
    832
    James Kanze
    Oct 13, 2008
  5. Gabriel Genellina
    Replies:
    0
    Views:
    524
    Gabriel Genellina
    Aug 13, 2009
Loading...

Share This Page