Hello tajinderpalsingh61, and welcome to the Monastery!
To expand on swl’s answer, here is one approach:
use strict; use warnings; use Data::Dump; use Math::BigInt; my $string = '1677259342285725925376'; my $n = Math::BigInt->new($string); my @bytes = map { ord } split //, $n->to_bytes(); @bytes = map { $_ >= 128 ? $_ - 256 : $_ } @bytes; dd \@bytes;
Output:
16:16 >perl 2015_SoPW.pl [90, -20, -90, 53, 78, -38, 2, -128, 0] 16:16 >
Explanation: Math::BigInt->to_bytes returns a reprsentation of the integer as a byte string. split // splits that string into its component bytes. ord() turns a byte from a character into a number (its numerical representation).
The useful function map applies the expression in braces to each element of the list it receives, and returns the resulting new list; so I’ve used it twice, once to convert byte characters into their numerical representations, and again to convert those numbers above 127 into their corresponding signed 8-bit integers.
For the ?: ternary operator, see Conditional Operator. And Data::Dump is a useful CPAN module for displaying complex data structures.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re: How to convert bigint to bytearray in perl
by Athanasius
in thread How to convert bigint to bytearray in perl
by tajinderpalsingh61
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |