Re: int array to string?

From: Leor Zolman (leor_at_bdsoft.com)
Date: 04/01/04


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


Relevant Pages

  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: releasing memory
    ... > the memory management for you. ... Thanks for the tip about calculating array sizes, ... willing to spend their time helping out struggling beginner programmers. ... look at the libraries that standard C++ has to offer. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Substring
    ... A common scenario is in fact taking an initial string and chopping it into smaller pieces. ... It's more efficient to do this using a single common source array and just maintaining indices into the array, both in terms of memory usage and in terms of speed. ... Worrying about the shared array is a premature optimization at best, and ignores an important "common case" optimization at worst. ...
    (comp.lang.java.programmer)
  • Re: fscanf issues, please help.
    ... those pointers point to strings of whatever length. ... An array of pointers is an array of pointer, ... they pont to some random places in memory. ... be an integer, the next one a string, followed by a hash etc., ...
    (comp.lang.c)
  • Re: Concat some string not ended...
    ... > i try to concat some string not ended ... What you're dealing here with are simple char arrays ... 'size' isn't compile-time constant and thus the length of the array ... a char pointer and than allocate enough memory. ...
    (comp.unix.programmer)