int array to string?

A

Amittai Aviram

Hi! Is there a convenient way to create a null-terminated string out of an
array of type int? Is there a standard library function I can use? If so,
how? This seems elementary, but I can't seem to find the right thing -- the
only thing I can think of is using sprintf repeatedly to push a copy of the
next integer from the array onto the growing string, moving the null
terminator in the process out to the new end, until all the integers have
been converted into characters in the string (provided that I begin with
adequate memory set aside for this task). But this seems clunky and
inefficient. Thanks!

Amittai Aviram
 
L

Leor Zolman

Hi! Is there a convenient way to create a null-terminated string out of an
array of type int? Is there a standard library function I can use? If so,
how? This seems elementary, but I can't seem to find the right thing -- the
only thing I can think of is using sprintf repeatedly to push a copy of the
next integer from the array onto the growing string, moving the null
terminator in the process out to the new end, until all the integers have
been converted into characters in the string (provided that I begin with
adequate memory set aside for this task). But this seems clunky and
inefficient. Thanks!

There's nothing standard for converting an unbounded quantity of binary
data into text; to me, it doesn't sound like something there'd be a
terrible demand for.

First and foremost, there's the question of how much memory to allocate for
the string. I assume you mean if you have an array such as this:
int a[] = {1, -2033449, 304050, 40};
then you'd want the result to be a string that looks something like this:
"1 -2033449 304050 40"
But how do you determine up front how much memory will be needed? You can
loop through the entire array first, figuring out how long each item plus
seperators will be in text form, add up all the lengths, allocate the
memory, then go back through and do the conversions...of course the lengths
you compute will be based upon how /you/ want the numbers formatted, not
some fixed "standard" format.

If the arrays are short enough that you can get away with a big
fixed-length buffer, you save the first loop and the allocation, but you
still have to control the formatting to suit you.

I think you'll just have to code it as best you can. Look at the bright
side: since you're producing text, it'll probably be destined for some
display or storage device and your program will be mostly I/O bound anyway,
so don't sweat the computation time for the conversion.
-leor
 
M

Mike Wahler

Amittai Aviram said:
Hi! Is there a convenient way to create a null-terminated string out of an
array of type int?

I'm not sure what that means, but I'll take a guess.
Is there a standard library function I can use? If so,
how? This seems elementary, but I can't seem to find the right thing -- the
only thing I can think of is using sprintf repeatedly to push a copy of the
next integer from the array onto the growing string, moving the null
terminator in the process out to the new end,

There's no need to 'move' the terminator, just write over it.
And the terminator indicating the 'new end' will be written
for you by 'sprintf()'.
until all the integers have
been converted into characters in the string (provided that I begin with
adequate memory set aside for this task). But this seems clunky and
inefficient. Thanks!

#include <stdio.h>
#include <string.h>

int main()
{
int array[] = {1, 2, 3, 4, 5};
char text[100] = {0};
char *p = text;
size_t i = 0;

for(i = 0; i < sizeof array / sizeof *array; ++i)
sprintf(p += strlen(p), "%d", array);

printf("%s\n", text);

return 0;
}

Output:

12345


You may or may not find my code 'clunky and inefficient',
but that's the way it's done. If it doesn't do what you
want, please clarify.


-Mike
 
A

Amittai Aviram

First and foremost, there's the question of how much memory to allocate for
the string. I assume you mean if you have an array such as this:
int a[] = {1, -2033449, 304050, 40};
then you'd want the result to be a string that looks something like this:
"1 -2033449 304050 40"

Yes, that's exactly right. And you're right -- if you don't know from the
outset what your integers will be, you either have to calculate the number
of digits (quite possible but a bit laborious) -- or else you have to assume
an outer bound for decimal digits per element and fudge accordingly, which
is what I've just done.
I think you'll just have to code it as best you can.

I've gone ahead and made a loop using sprintf. Thanks a lot for your
comments.

Amittai
 
A

Amittai Aviram

You may or may not find my code 'clunky and inefficient',
but that's the way it's done. If it doesn't do what you
want, please clarify.

Thank you very much. That was what I meant. I've gone ahead and created
something similar, though yours is, indeed, more elegant.

Amittai
 
M

Mac

First and foremost, there's the question of how much memory to allocate for
the string. I assume you mean if you have an array such as this:
int a[] = {1, -2033449, 304050, 40};
then you'd want the result to be a string that looks something like this:
"1 -2033449 304050 40"

Yes, that's exactly right. And you're right -- if you don't know from the
outset what your integers will be, you either have to calculate the number
of digits (quite possible but a bit laborious) -- or else you have to assume
an outer bound for decimal digits per element and fudge accordingly, which
is what I've just done.
I think you'll just have to code it as best you can.

I've gone ahead and made a loop using sprintf. Thanks a lot for your
comments.

Amittai

I just want to warn you that most exploits nowadays come from buffer
overflows. Without seeing your exact code, it is hard to say how
vulnerable it might be, but if you intend to distribute it, you should
make sure that a malicious user can't overflow a string buffer by
carefully crafting a strange file or input line.

Just my $0.02

--Mac
 
A

Alex

Mike Wahler said:
#include <stdio.h>
#include <string.h>

int main()
{
int array[] = {1, 2, 3, 4, 5};
char text[100] = {0};
char *p = text;
size_t i = 0;

for(i = 0; i < sizeof array / sizeof *array; ++i)
sprintf(p += strlen(p), "%d", array);


p += sprintf(p, "%d", array);
 
C

CBFalconer

Amittai said:
Hi! Is there a convenient way to create a null-terminated string
out of an array of type int? Is there a standard library
function I can use? If so, how? This seems elementary, but I
can't seem to find the right thing -- the only thing I can think
of is using sprintf repeatedly to push a copy of the next integer
from the array onto the growing string, moving the null terminator
in the process out to the new end, until all the integers have
been converted into characters in the string (provided that I
begin with adequate memory set aside for this task). But this
seems clunky and inefficient. Thanks!

Try roughly:

char buf[INFINITESIZE];
char *p;

p = buf;
while (whatevercritierionyouneed) {
p += sprintf(p, "format string", item);
}
 

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

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top