Velocity Reviews > Converting Character Array to String

Converting Character Array to String

Charles L
Guest
Posts: n/a

 12-03-2003
I don't know if this is a stupid quesiton or not.

I would like to know how to convert an array of characters generated from a
previous operation to a string ie how do I append a null character at the
end? I haven't been able to do it so far. Is there a string function I can
use?

Can anyone help?

Charles L

lallous
Guest
Posts: n/a

 12-03-2003
"Charles L" <(E-Mail Removed)> wrote in message
news:3fcdfe8e\$(E-Mail Removed)...
> I don't know if this is a stupid quesiton or not.
>
> I would like to know how to convert an array of characters generated from

a
> previous operation to a string ie how do I append a null character at the
> end? I haven't been able to do it so far. Is there a string function I can
> use?
>
> Can anyone help?
>
> Charles L
>

What is a string?
Are you talking about STL's string ?

an array of characters is a string.

If you mean that array of characters doesn't have to be null terminated and
string has to be, then the answer to your question would be:
Try to determine a size out of your character array then do this:
string[length] = 0;

Regards
Elias

Richard Bos
Guest
Posts: n/a

 12-03-2003
"Charles L" <(E-Mail Removed)> wrote:

> I don't know if this is a stupid quesiton or not.
>
> I would like to know how to convert an array of characters generated from a
> previous operation to a string ie how do I append a null character at the
> end? I haven't been able to do it so far. Is there a string function I can
> use?

If you know how long the string is supposed to be, it's simple:
array[length]='\0' will do the trick. If you don't know how long it's
supposed to be, there's no feasible way to find out from the array
alone, since the characters past the required length can be filled with
anything, including seemingly normal text.

Richard

Eric
Guest
Posts: n/a

 12-03-2003
Charles L <(E-Mail Removed)> wrote:

> I would like to know how to convert an array of characters generated from a
> previous operation to a string ie how do I append a null character at the
> end? I haven't been able to do it so far. Is there a string function I can
> use?

Do you track how many characters you have?

If you know this, what you want to do is easy.

long nCharacters; /*should be set to the number of characters you*/
/*have*/

yourString[nCharacters] = '\0';

This does assume 'yourString' has the space to store the null character.
If you do not know that it does, you will need to dynamically allocate a
string that will.

long nCharacters; /*should be set to the number of characters you*/
/*have*/
char *newString = NULL;

newString = malloc( nCharacters + 1 ); /*should check for NULL*/
strncpy( newString, yourString, nCharacters );
newString[nCharacters] = '\0';

If you do not know how many characters you have or cannot determine it,
what you want to do is impossible.

--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===

Richard Bos
Guest
Posts: n/a

 12-03-2003
"lallous" <(E-Mail Removed)> wrote:

> "Charles L" <(E-Mail Removed)> wrote in message
> news:3fcdfe8e\$(E-Mail Removed)...
> > I don't know if this is a stupid quesiton or not.
> >
> > I would like to know how to convert an array of characters generated from

> a
> > previous operation to a string ie how do I append a null character at the
> > end? I haven't been able to do it so far. Is there a string function I can
> > use?

>
> What is a string?

A string is, and I quote the Standard, "a contiguous sequence of
characters terminated by and including the first null character".

> Are you talking about STL's string ?

Who is STL?

> an array of characters is a string.

Only if terminated by a null character.

> If you mean that array of characters doesn't have to be null terminated and
> string has to be,

Of course he does.

> Try to determine a size out of your character array

How?

Richard

Lew Pitcher
Guest
Posts: n/a

 12-03-2003
lallous wrote:
> "Charles L" <(E-Mail Removed)> wrote in message
> news:3fcdfe8e\$(E-Mail Removed)...
>
>>I don't know if this is a stupid quesiton or not.
>>
>>I would like to know how to convert an array of characters generated from

>
> a
>
>>previous operation to a string ie how do I append a null character at the
>>end? I haven't been able to do it so far. Is there a string function I can
>>use?
>>
>>Can anyone help?
>>
>>Charles L
>>

>
>
> What is a string?

7.1.1 Definitions of terms
1 A string is a contiguous sequence of characters terminated by and
including the first null character. The term multibyte string is sometimes
used instead to emphasize special processing given to multibyte characters
contained in the string or to avoid confusion with a wide string. A pointer
to a string is a pointer to its initial (lowest addressed) character. The
length of a string is the number of bytes preceding the null character and
the value of a string is the sequence of the values of the contained
characters, in order.

> Are you talking about STL's string ?

He'd better not be, given that this is comp.lang.c and there's no such thing
as STL here.

> an array of characters is a string.

Without the terminating null character, an array of characters is /not/ a
string.

> If you mean that array of characters doesn't have to be null terminated and
> string has to be, then the answer to your question would be:
> Try to determine a size out of your character array then do this:
> string[length] = 0;

Correct.

> Regards
> Elias
>
>

--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Eric
Guest
Posts: n/a

 12-03-2003
Richard Bos <(E-Mail Removed)> wrote:

> "Charles L" <(E-Mail Removed)> wrote:
>
> > I don't know if this is a stupid quesiton or not.
> >
> > I would like to know how to convert an array of characters generated from a
> > previous operation to a string ie how do I append a null character at the
> > end? I haven't been able to do it so far. Is there a string function I can
> > use?

>
> If you know how long the string is supposed to be, it's simple:
> array[length]='\0' will do the trick.

Only if 'array' is big enough the hold the NULL character.

--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===

those who know me have no need of my name
Guest
Posts: n/a

 12-04-2003
in comp.lang.c i read:

>I would like to know how to convert an array of characters generated from a
>previous operation to a string ie how do I append a null character at the
>end? I haven't been able to do it so far. Is there a string function I can
>use?

there is no string function you can use, because until there is a null byte
at the end it's not a string, merely a sequence of characters.

--
a signature

Capstar
Guest
Posts: n/a

 12-04-2003
Richard Bos wrote:
> "lallous" <(E-Mail Removed)> wrote:
>
>
>>"Charles L" <(E-Mail Removed)> wrote in message
>>news:3fcdfe8e\$(E-Mail Removed)...
>>
>>>I don't know if this is a stupid quesiton or not.
>>>
>>>I would like to know how to convert an array of characters generated from

>>
>>a
>>
>>>previous operation to a string ie how do I append a null character at the
>>>end? I haven't been able to do it so far. Is there a string function I can
>>>use?

>>

<snip>
>
>
>>Try to determine a size out of your character array

>
>
> How?

By keeping track in the "previous operation"

Mark

Richard Bos
Guest
Posts: n/a

 12-04-2003
Capstar <(E-Mail Removed)> wrote:

> Richard Bos wrote:
> > "lallous" <(E-Mail Removed)> wrote:
> >
> >>Try to determine a size out of your character array

> >
> > How?

>
> By keeping track in the "previous operation"

Well, quite. But that's not determining it "out of" the array.

Richard