Re: from array of ints to string and vice versa

From: Willem (willem_at_stack.nl)
Date: 10/19/04


Date: Tue, 19 Oct 2004 16:39:38 +0000 (UTC)

Martijn wrote:
) Hi,
)
) I have the following issue: A user is allowed a enter a sequence of
) numbers, separated by comma's. I want to convert this string to an array,
) using the C language, taking "abundant" whitespace into account. I
) currently do this by running over the string in two passes, one to validate
) the string - trap on letters and invalid things such as "8,,9" and "8,9
) 8,9" - and to calculate the required size of the integer array, and another
) to extract the actual numbers.
)
) It works like a charm (code on request), but I was wondering if there is a
) more straightforward way to accomplish this? For the reversed process I do
) something similar - one pass to calculate the size of the string, and the
) other to generate it, separating them with ", ".

Well, the two-pass method to first calculate the size, and then fill the
array, is IMO the most straightforward way of doing things, in C that is.
Otherwise you'd need some way to dynamically grow the thing.

One way that may or may not be more elegant is to have a function that
takes a buffer and returns a size, which you first call with a NULL pointer
to have it calculate the size, then you allocate the buffer, and finally
you call the same code again with the buffer allocated to the required
size. Then, insize the code, you just conditional(*) on the pointer to see
if it's the dry run, or the actual run.

*) You can verb any noun. :-)

SaSW, Willem

-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


Relevant Pages

  • Re: Data type byte
    ... attempt to change the input buffer to a string (although not by changing the ... With multi dimensional array that can hold ... Also the receiving ... dimensional Byte Array is just one long string of memory. ...
    (microsoft.public.vb.general.discussion)
  • Re: String parsing in VB.net
    ... refactor your code so that myDataBuffer is an array of Byte, ... than a string. ... receives a data buffer. ... encoding - it is only defined for 0-127. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Help With Arrays
    ... > The string to be copied to the buffer is of varying size, ... making the size of the string the same size as the array. ... The way round this is to use malloc(). ...
    (comp.lang.c)
  • Re: pls help: very odd behaviour when concatenating strings
    ... More then likely you are having a problem because your byte array is ... array to a string. ... // Put some stuff in the buffer. ... > concatenate them some values are missing (I canīt see them in the debugger). ...
    (microsoft.public.dotnet.languages.csharp)
  • split not returning an array?
    ... I start out with a string containing at least 1 '+' separating ... I'm trying to get an array using str.split, ... '1' is only outputted once, and if I just print out @cmds, I get ...
    (comp.lang.ruby)