Login  Register

Converting an Array of Characters to a String

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options Options
Embed post
Permalink
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Converting an Array of Characters to a String

kilon.alios
2862 posts
I feel like stupid but I cannot find a way to convert an Array of Characters to a String , I can do with a do: and join characters converted to strings to a single string but it feels too many steps. 

Is there a simpler way ? 
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Converting an Array of Characters to a String

Nicolai Hess-3-2
935 posts


2016-11-08 14:31 GMT+01:00 Dimitris Chloupis <[hidden email]>:
I feel like stupid but I cannot find a way to convert an Array of Characters to a String , I can do with a do: and join characters converted to strings to a single string but it feels too many steps. 

Is there a simpler way ? 

String newFrom:{ $a . $b . $c }.
{ $a . $b . $c } as:String.

Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Converting an Array of Characters to a String

EstebanLM
6187 posts
In reply to this post by kilon.alios
(always with Char100 example in mind):

s := MyStructure fromHandle: blah.
string := s data readString.

should work.

Esteban


> On 8 Nov 2016, at 14:31, Dimitris Chloupis <[hidden email]> wrote:
>
> I feel like stupid but I cannot find a way to convert an Array of Characters to a String , I can do with a do: and join characters converted to strings to a single string but it feels too many steps.
>
> Is there a simpler way ?


Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Converting an Array of Characters to a String

EstebanLM
6187 posts
In reply to this post by Nicolai Hess-3-2
ah yes, that will work (I was thinking on UFFI and I answered that) :)

On 8 Nov 2016, at 14:41, Nicolai Hess <[hidden email]> wrote:



2016-11-08 14:31 GMT+01:00 Dimitris Chloupis <[hidden email]>:
I feel like stupid but I cannot find a way to convert an Array of Characters to a String , I can do with a do: and join characters converted to strings to a single string but it feels too many steps. 

Is there a simpler way ? 

String newFrom:{ $a . $b . $c }.
{ $a . $b . $c } as:String.


Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Converting an Array of Characters to a String

kilon.alios
2862 posts
In reply to this post by Nicolai Hess-3-2


On Tue, Nov 8, 2016 at 3:42 PM Nicolai Hess <[hidden email]> wrote:
2016-11-08 14:31 GMT+01:00 Dimitris Chloupis <[hidden email]>:
I feel like stupid but I cannot find a way to convert an Array of Characters to a String , I can do with a do: and join characters converted to strings to a single string but it feels too many steps. 

Is there a simpler way ? 

String newFrom:{ $a . $b . $c }.
{ $a . $b . $c } as:String.


Its so weird that

{ $a . $b . $c } as:String. 

works but 

{ $a . $b . $c } asString.

does not 

the latter returns a string that contains something like

{$S. $e. $d. Character space. $o. $r. $n. $a. $r. $e. C

In any case Thanks Nicolai it works :)    
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Converting an Array of Characters to a String

vonbecmann
296 posts
something similar

String withAll: { $a. Character space. $b }.



On Tue, Nov 8, 2016 at 10:52 AM, Dimitris Chloupis <[hidden email]> wrote:


On Tue, Nov 8, 2016 at 3:42 PM Nicolai Hess <[hidden email]> wrote:
2016-11-08 14:31 GMT+01:00 Dimitris Chloupis <[hidden email]>:
I feel like stupid but I cannot find a way to convert an Array of Characters to a String , I can do with a do: and join characters converted to strings to a single string but it feels too many steps. 

Is there a simpler way ? 

String newFrom:{ $a . $b . $c }.
{ $a . $b . $c } as:String.


Its so weird that

{ $a . $b . $c } as:String. 

works but 

{ $a . $b . $c } asString.

does not 

the latter returns a string that contains something like

{$S. $e. $d. Character space. $o. $r. $n. $a. $r. $e. C

In any case Thanks Nicolai it works :)    



--
Bernardo E.C.

Sent from a cheap desktop computer in South America.
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Converting an Array of Characters to a String

Igor Stasenko
9533 posts
In reply to this post by EstebanLM


On 8 November 2016 at 14:42, Esteban Lorenzano <[hidden email]> wrote:
(always with Char100 example in mind):

s := MyStructure fromHandle: blah.
string := s data readString.

should work.


IIRC, #readString works correctly only  for correctly null-terminated strings. If not, it will read beyond the structure size , until it find a zero byte somewhere in memory,
and thus, results may vary :)
 
Esteban


> On 8 Nov 2016, at 14:31, Dimitris Chloupis <[hidden email]> wrote:
>
> I feel like stupid but I cannot find a way to convert an Array of Characters to a String , I can do with a do: and join characters converted to strings to a single string but it feels too many steps.
>
> Is there a simpler way ?





--
Best regards,
Igor Stasenko.
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Converting an Array of Characters to a String

Igor Stasenko
9533 posts
What i meant, i wanted to warn Dimitris that
char[100] 
are just array of 100 characters (bytes in C).. and has nothing to do with strings.
Do not confuse fixed-length C arrays with strings. There's no 'string' data type in C, and instead they use null-terminated character sequence as a convention. But it is not a fixed-size data.

On 9 November 2016 at 02:38, Igor Stasenko <[hidden email]> wrote:


On 8 November 2016 at 14:42, Esteban Lorenzano <[hidden email]> wrote:
(always with Char100 example in mind):

s := MyStructure fromHandle: blah.
string := s data readString.

should work.


IIRC, #readString works correctly only  for correctly null-terminated strings. If not, it will read beyond the structure size , until it find a zero byte somewhere in memory,
and thus, results may vary :)
 
Esteban


> On 8 Nov 2016, at 14:31, Dimitris Chloupis <[hidden email]> wrote:
>
> I feel like stupid but I cannot find a way to convert an Array of Characters to a String , I can do with a do: and join characters converted to strings to a single string but it feels too many steps.
>
> Is there a simpler way ?





--
Best regards,
Igor Stasenko.



--
Best regards,
Igor Stasenko.
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Converting an Array of Characters to a String

kilon.alios
2862 posts
Not in C but there std::string in C++ similar to our String object.

C char indeed is more a byte array than a string. Actually C won't allow to change the string value mainly because it thinks you try to change the size which something that is not allowed for arrays anyway, so the only way to change a char string aka char array is character by character.

Null termination is why I filled the shared memory with zeros to be on the safe side and then added the string and the number on top

On Wed, 9 Nov 2016 at 03:41, Igor Stasenko <[hidden email]> wrote:
What i meant, i wanted to warn Dimitris that
char[100] 
are just array of 100 characters (bytes in C).. and has nothing to do with strings.
Do not confuse fixed-length C arrays with strings. There's no 'string' data type in C, and instead they use null-terminated character sequence as a convention. But it is not a fixed-size data.

On 9 November 2016 at 02:38, Igor Stasenko <[hidden email]> wrote:


On 8 November 2016 at 14:42, Esteban Lorenzano <[hidden email]> wrote:
(always with Char100 example in mind):

s := MyStructure fromHandle: blah.
string := s data readString.

should work.


IIRC, #readString works correctly only  for correctly null-terminated strings. If not, it will read beyond the structure size , until it find a zero byte somewhere in memory,
and thus, results may vary :)
 
Esteban


> On 8 Nov 2016, at 14:31, Dimitris Chloupis <[hidden email]> wrote:
>
> I feel like stupid but I cannot find a way to convert an Array of Characters to a String , I can do with a do: and join characters converted to strings to a single string but it feels too many steps.
>
> Is there a simpler way ?





--
Best regards,
Igor Stasenko.



--
Best regards,
Igor Stasenko.
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Converting an Array of Characters to a String

Igor Stasenko
9533 posts


On 9 November 2016 at 07:49, Dimitris Chloupis <[hidden email]> wrote:
Not in C but there std::string in C++ similar to our String object.

Strictly speaking , std::string is again not part of C++ per se, it comes from library that implements such class.
 
C char indeed is more a byte array than a string. Actually C won't allow to change the string value mainly because it thinks you try to change the size which something that is not allowed for arrays anyway, so the only way to change a char string aka char array is character by character.

Null termination is why I filled the shared memory with zeros to be on the safe side and then added the string and the number on top

just wanted to warn you, to make sure you understand that char[100] is not string, not in C , not in C++. And, of course, it should not autoconvert to smalltalk String object(s) in FFI, because many C coders use 'char' as a default data type to operate with buffers of certain length and to count their size in bytes.
 
On Wed, 9 Nov 2016 at 03:41, Igor Stasenko <[hidden email]> wrote:
What i meant, i wanted to warn Dimitris that
char[100] 
are just array of 100 characters (bytes in C).. and has nothing to do with strings.
Do not confuse fixed-length C arrays with strings. There's no 'string' data type in C, and instead they use null-terminated character sequence as a convention. But it is not a fixed-size data.

On 9 November 2016 at 02:38, Igor Stasenko <[hidden email]> wrote:


On 8 November 2016 at 14:42, Esteban Lorenzano <[hidden email]> wrote:
(always with Char100 example in mind):

s := MyStructure fromHandle: blah.
string := s data readString.

should work.


IIRC, #readString works correctly only  for correctly null-terminated strings. If not, it will read beyond the structure size , until it find a zero byte somewhere in memory,
and thus, results may vary :)
 
Esteban


> On 8 Nov 2016, at 14:31, Dimitris Chloupis <[hidden email]> wrote:
>
> I feel like stupid but I cannot find a way to convert an Array of Characters to a String , I can do with a do: and join characters converted to strings to a single string but it feels too many steps.
>
> Is there a simpler way ?





--
Best regards,
Igor Stasenko.



--
Best regards,
Igor Stasenko.



--
Best regards,
Igor Stasenko.