'Z' and 'A' String#unpack question

hi folks!

i wondered that ‘Z’ or ‘A’ parameters in String#unpack to return a
string with characters that follow the first null character removed .

i think this is useful for extracting C null-terminated string that has
not been filled with null characters.

$ ./ruby -ve 'p “this is my test\000please remove this”.unpack(“Z*”)'
ruby 1.8.0 (2003-04-11) [i686-linux]
[“this is my test\000please remove this”]
$

i expect to have “this is my test” instead.

thanks a lot

···


/***********************************************************************/
/
Guillaume Pierronnet guillaume.pierronnet@ratp.fr /
/
***********************************************************************/

Hi,

i wondered that ‘Z’ or ‘A’ parameters in String#unpack to return a
string with characters that follow the first null character removed .

‘Z’ removes trailing NUL characters.

$ ruby -e ‘p “this is my test\000please remove this\0\0\0”.unpack(“Z*”)’
[“this is my test\000please remove this”]

i think this is useful for extracting C null-terminated string that has
not been filled with null characters.

string.sub(/\0.*/m, ‘’)

or

string[/\A[^\0]*/]

···

At Fri, 11 Apr 2003 21:51:58 +0900, guillaume.pierronnet@ratp.fr wrote:


Nobu Nakada