On 6/19/22 03:06, ToddAndMargo via perl6-users wrote:
On 6/19/22 00:33, ToddAndMargo via perl6-users wrote:
Hi All,
I have a string:
> my Str $x = "1BB67AE85A";
1BB67AE85A
which has the hexadecimal values I want to
add to a buffer:
> my buf8 $y = buf8.new($x.base(16));
No such method 'base' for invocant of type 'Str'. Did you mean any of
these: 'Bag', 'Date', 'Hash', 'are', 'asec', 'hash', 'take'?
in block <unit> at <unknown file> line 1
In other words, I want to set $y to
0x1B 0xB6 0x7A 0xE8 0x5A
How do I do this?
Many thanks,
-T
The guys on the chat line helped me figure it
out. Give me a few minutes to pretty it up
and report back.
Hexadecimal string to Buffer:
Quick example:
> my Str $x = '0x84 0x73 0x77 0x84 0x79 0x87 0x84 0x68 0x73'
> my buf8 $y = buf8.new($x.match(/(<xdigit> ** 2)/,
:g)».Str».parse-base(16));
Buf[uint8]:0x<84 73 77 84 79 87 84 68 73>
More in depth:
> use BigRoot;
Nil
> BigRoot.precision = 10;
10
> my $BigNum = BigRoot.newton's-sqrt: 11
3.3166247904
> my Str $MyCypher = sprintf $BigNum.base(16);
3.510E527FE
> $MyCypher ~~ s/ '.' //;
「.」
> $MyCypher ~~ s:g/ (..) /0x$0, /;
(「3」
0 => 「3」 「51」
0 => 「51」 「0E」
0 => 「0E」 「52」
0 => 「52」 「7F」
0 => 「7F」)
> $MyCypher ~~ s/ (.*) $(Q[,]) /$0/;
「0x3, 0x51, 0x0E, 0x52, 0x7F,」
0 => 「0x3, 0x51, 0x0E, 0x52, 0x7F」
> my buf8 $MyCypherBuf = buf8.new($MyCypher.match(/(<xdigit> ** 2)/,
:g)».Str».parse-base(16));
Buf[uint8]:0x<51 0E 52 7F>
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~