'unsigned long' to 'char[]' array

M

Mark

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, " ");
}
 
B

Ben Bacarisse

Mark said:
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?
 
M

Mark

Ben said:
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.
 
L

lawrence.jones

Mark said:
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.
 
D

David Thompson

Mark said:
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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top