develooper Front page | perl.perl6.users | Postings from June 2022

Re: Dual Buf and Str question

Thread Previous | Thread Next
From:
Elizabeth Mattijsen
Date:
June 10, 2022 09:36
Subject:
Re: Dual Buf and Str question
Message ID:
49F417F5-AD4D-4AF1-BDC4-7301DA914E52@dijkmat.nl
> On 10 Jun 2022, at 11:20, ToddAndMargo via perl6-users <perl6-users@perl.org> wrote:
> 
>>> On 10 Jun 2022, at 07:54, ToddAndMargo via perl6-users <perl6-users@perl.org> 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 of ways to do this, but it would
>>> require separate variable and conversions routines
>>> back and forth.
>>> 
>>> Any words of Raku wisdom?
>>> 
>>> Many thanks,
>>> -T
> 
> On 6/10/22 01:23, Elizabeth Mattijsen wrote:
>> 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 string again.
> 
> I want 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 "abc".encode.does(Blob);  # True
Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About