Re: int array to string?
From: Leor Zolman (leor_at_bdsoft.com)
Date: 04/01/04
- Next message: Mike Wahler: "Re: int array to string?"
- Previous message: Amittai Aviram: "int array to string?"
- In reply to: Amittai Aviram: "int array to string?"
- Next in thread: Amittai Aviram: "Re: int array to string?"
- Reply: Amittai Aviram: "Re: int array to string?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 01 Apr 2004 03:46:03 GMT
On Wed, 31 Mar 2004 22:20:10 -0500, "Amittai Aviram" <amittai@amittai.com>
wrote:
>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
>
>Amittai Aviram
>
>
-- Leor Zolman --- BD Software --- www.bdsoft.com On-Site Training in C/C++, Java, Perl and Unix C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
- Next message: Mike Wahler: "Re: int array to string?"
- Previous message: Amittai Aviram: "int array to string?"
- In reply to: Amittai Aviram: "int array to string?"
- Next in thread: Amittai Aviram: "Re: int array to string?"
- Reply: Amittai Aviram: "Re: int array to string?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|