Re: Dual Buf and Str question

2022-06-11 Thread ToddAndMargo via perl6-users
On 6/11/22 02:28, ToddAndMargo via perl6-users wrote: On 6/9/22 22:54, ToddAndMargo via perl6-users wrote: Hi All, I can easily  get away with this in Modula2, but how can I do this with Raku? I wish to create a single variable that can be manipulated in two ways: 1) as a fixed length string

Re: Dual Buf and Str question

2022-06-11 Thread ToddAndMargo via perl6-users
On 6/9/22 22:54, ToddAndMargo via perl6-users wrote: Hi All, I can easily  get away with this in Modula2, but how can I do this with Raku? I wish to create a single variable that can be manipulated in two ways: 1) as a fixed length string (Str) 2) as a fixed length buffer (Buf) I can think

Re: Dual Buf and Str question

2022-06-10 Thread ToddAndMargo via perl6-users
the variable to act as both an array of characters and a binary array of bytes. Convert a string to a Buf: say "abc".encode.Buf; # Buf:0x<61 62 63> Convert a Buf to a Str: say Buf.new(97,98,99).decode; # abc Technically, I think the .encode is enough for what you want: say "

Re: Dual Buf and Str question

2022-06-10 Thread Elizabeth Mattijsen
gt; Perhaps https://raku.land/zef:raku-community-modules/Pythonic::Str is what >> you're after? > > No really. > > Maybe if I was to tell you what I am trying to do. > > I am trying to do a bitwise XOR on each byte > against another Buf. Then I want it to act > like a s

Re: Dual Buf and Str question

2022-06-10 Thread ToddAndMargo via perl6-users
On 10 Jun 2022, at 07:54, ToddAndMargo via perl6-users wrote: Hi All, I can easily get away with this in Modula2, but how can I do this with Raku? I wish to create a single variable that can be manipulated in two ways: 1) as a fixed length string (Str) 2) as a fixed length buffer (Buf) I

Re: Dual Buf and Str question

2022-06-10 Thread Elizabeth Mattijsen
Perhaps https://raku.land/zef:raku-community-modules/Pythonic::Str is what you're after? > On 10 Jun 2022, at 07:54, ToddAndMargo via perl6-users > wrote: > > Hi All, > > I can easily get away with this in Modula2, but > how can I do this with Raku? > > I wish to create a single variable

Dual Buf and Str question

2022-06-09 Thread ToddAndMargo via perl6-users
Hi All, I can easily get away with this in Modula2, but how can I do this with Raku? I wish to create a single variable that can be manipulated in two ways: 1) as a fixed length string (Str) 2) as a fixed length buffer (Buf) I can think of ways to do this, but it would require separate

Re: Buf to Str

2021-06-09 Thread ToddAndMargo via perl6-users
On 6/8/21 9:34 PM, Paul Procacci wrote: Hopefully a pretty quick question GIven the following: my Buf $b .= new([72, 105, 0, 32, 97, 103, 97, 105, 110, 0]); say $b.decode; I would expect this to print 'Hi'. Instead it prints 'Hi again'. https://docs.raku.org/type/Buf#(Blob)_method_decode

Re: Buf to Str

2021-06-09 Thread Matthew Stuckwisch
Another way to do this could be to just create your own method. my method decode-c(Blob:) { self.subbuf(^self.first(0,:kv)[0]).decode } my $string-with-zero = Buf.new: 72, 101, 108, 108, 111, 0, 32, 119, 111, 114, 108, 100, 33; say $string-with-zero.decode; say $string-with-zero. The output of

Re: Buf to Str

2021-06-09 Thread Paul Procacci
>> That C null is an int pointer, longer than a single byte. Yep, no arguments there. ;) On Wed, Jun 9, 2021 at 11:06 AM yary wrote: > That C null is an int pointer, longer than a single byte. > > On Wed, Jun 9, 2021 at 11:04 AM Paul Procacci wrote: > >> Not sure about the 80's, my

Re: Buf to Str

2021-06-09 Thread Paul Procacci
That does help some Daniel. I do in fact need to work with C style null terminated strings because when passing a structure to an underlying OS via nativecall, you have no control over what the underlying libraries want. An Example: class myStruct is repr('CStruct') { HAS int8 @.Path[MAX_PATH]

Re: Buf to Str

2021-06-09 Thread yary
That C null is an int pointer, longer than a single byte. On Wed, Jun 9, 2021 at 11:04 AM Paul Procacci wrote: > Not sure about the 80's, my programming endeavors started in the 90's. > NUL doesn't exist in the C standard so I have no comment on it. > The C standard defines that 0 cast to the

Re: Buf to Str

2021-06-09 Thread Paul Procacci
Not sure about the 80's, my programming endeavors started in the 90's. NUL doesn't exist in the C standard so I have no comment on it. The C standard defines that 0 cast to the type void * is both a null pointer and a null pointer constant. I always have and most likely will continue using 0 over

Re: Buf to Str

2021-06-09 Thread Daniel Sockwell
Hi Paul, If you _do_ want/need to work with C-style null-terminated strings, you can use the (core) NativeCall library. So, given your example: > my Buf $b .= new([72, 105, 0, 32, 97, 103, 97, 105, 110, 0]); > say $b.decode; > I would expect this to print 'Hi'. > > Instead it prints 'Hi

Re: Buf to Str

2021-06-09 Thread yary
>From my early 1980s days learning programming, ASCII CHR 0 is not null, it is NUL (can type it as ctrl-@) it's a control character much like the others. Ctrl-G BEL, it was fun putting you in file names... On Wed, Jun 9, 2021, 9:56 AM Paul Procacci wrote: > >> But yeah, the Str class in Raku

Re: Buf to Str

2021-06-09 Thread Paul Procacci
>> But yeah, the Str class in Raku is much more than a C-string. Got it. Thanks Elizabeth. On Wed, Jun 9, 2021 at 6:45 AM Elizabeth Mattijsen wrote: > > On 9 Jun 2021, at 06:34, Paul Procacci wrote: > > > > Hopefully a pretty quick question > > > > GIven the following: > > > > my Buf $b

Re: Buf to Str

2021-06-09 Thread Elizabeth Mattijsen
> On 9 Jun 2021, at 06:34, Paul Procacci wrote: > > Hopefully a pretty quick question > > GIven the following: > > my Buf $b .= new([72, 105, 0, 32, 97, 103, 97, 105, 110, 0]); > say $b.decode; > > I would expect this to print 'Hi'. > Instead it prints 'Hi again'. > >

Re: Buf to Str

2021-06-09 Thread ToddAndMargo via perl6-users
On 6/8/21 11:32 PM, ToddAndMargo via perl6-users wrote: Hi Paul, In "C", a chr(0), also called a "null" is a string terminator.  In Raku, the terminator is in the accompanying structure (hidden from you), when says long the string is. My guess as to what is happening is that the decode

Re: Buf to Str

2021-06-09 Thread ToddAndMargo via perl6-users
On 6/8/21 9:34 PM, Paul Procacci wrote: Hopefully a pretty quick question GIven the following: my Buf $b .= new([72, 105, 0, 32, 97, 103, 97, 105, 110, 0]); say $b.decode; I would expect this to print 'Hi'. Instead it prints 'Hi again'. https://docs.raku.org/type/Buf#(Blob)_method_decode

Buf to Str

2021-06-08 Thread Paul Procacci
Hopefully a pretty quick question GIven the following: my Buf $b .= new([72, 105, 0, 32, 97, 103, 97, 105, 110, 0]); say $b.decode; I would expect this to print 'Hi'. Instead it prints 'Hi again'. https://docs.raku.org/type/Buf#(Blob)_method_decode The decode documentation for Buf only