I am trying to replicate some java code in perl.
The operation, I need to perform is to convert Java BigInteger to byte array
Java Code:
public class Main {
public static void main(String[] args) {
BigInteger n = new BigInteger("1677259342285725925376");
System.out.println(Arrays.toString(n.toByteArray()));
}
}
Output: [90, -20, -90, 53, 78, -38, 2, -128, 0]
As, new in perl, tried to search and got help from https://www.perlmonks.org/?node_id=229290
Perl Code:
sub bigint_to_bytearray {
my $bigint = shift;
my #bytes;
while(1) {
my ($q,$r) = $bigint->brsft(8);
push(#bytes,$r+0);
last if $q == 0;
$bigint = Math::BigInt->new($q);
}
return #bytes;
}
$n = new Math::BigInt('1677259342285725925376');
my #array = bigint_to_bytearray($n);
print "\\tOUTPUT ARRAY = ", join ', ', #array, "\n";
I am getting error
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
Use of uninitialized value $r in addition (+) at path/test.pl line 11.
This doesn't support negative numbers.
sub bigint_to_bytearray {
my $bigint = shift;
die "Negative numbers not supported" if $bigint->is_neg;
my #bytes = unpack('c*', $bigint->to_bytes);
unshift #bytes, 0x00 if $bytes[0] < 0; # Add sign bit if necessary.
return #bytes;
}
The above requires a relatively new version of Math::BigInt. The following is less efficient, but it works on far older versions:
sub bigint_to_bytearray {
my $bigint = shift;
die "Negative numbers not supported" if $bigint->is_neg;
my #bytes = unpack('c*', pack('H*', substr($bigint->as_hex, 2)));
unshift #bytes, 0x00 if $bytes[0] < 0; # Add sign bit if necessary.
return #bytes;
}
my #bytes = unpack('c*', $bigint->to_bytes);
can be replaced with
my #bytes =
map { $_ >= 0x80 ? 0x100 - $_ : $_ }
map ord,
split //,
$bigint->to_bytes;
and
my #bytes = unpack('c*', pack('H*', substr($bigint->as_hex, 2)));
can be replaced with
my $hex = substr($bigint->as_hex, 2);
$hex = "0$hex" if length($hex) % 2;
my #bytes =
map { $_ >= 0x80 ? 0x100 - $_ : $_ }
map hex,
$hex =~ /../g;
I am having an error saying that prototype not terminated at filename.txt line number 113 where as line number 113 belongs to a different program which is running successfully.
sub howmany(
my #H = #_;
my $m = 0;
foreach $x (#H) {
if ( $x > 5 ) {
$m +=1;
}
else {
$m +=0;
}
}
print "Number of elements greater than 5 is equal to: $m \n";
}
howmany(1,6,9);
The sub keyword should be followed by { } not ( ) (if you define a simple function), that's why the error
prototype not terminated
After this, always start with : use strict; use warnings;
Put this and debug your script, there's more errors.
Last but not least, indent your code properly, using an editor with syntax highlighting, you will save many time debugging
The error is due to parenthesis.
Never do $m += 0; As you actually load processor for nothing. Of course it's not gonna be visible on such a small function, but...
sub howmany {
my $m = 0;
foreach (#_) {
$m++ if ($_ > 5);
}
print "Number of elements greater than 5 is equal to: $m \n";
}
howmany(1,6,9);
I need to test a file with each line having the same number of columns and each entry is some value and I want to only select those lines with every values greater than, say 0.5.
I know I can loop through the array in each line by doing something like this:
open (IN, shift #ARGV);
while (<IN>){
chomp;
my $count = 0;
my #array = split/\t/;
foreach (#array){
if ($_ > 0.5) {
$count ++;
}
}
if ($count == scalar #array){
print $_,"\n";
}
}
close IN;
This is kinda long and I'm wondering if there is a better way to do it?
Thanks.
Use all from List::Util - it checks if passed code block (often with condition) returns true for all elements of list.
use List::Util qw(all);
if (all { $_ > 0.5 } #array) {
print "Pass!"
}
It will even short-circuit for you, terminating as soon as it finds first false value, producing most speed-effective result.
my #array = 10 .. 20;
# compare size of array with list size of grep
if (#array == grep { $_ > 0.5 } #array) {
print "All are greater than 0.5\n";
}
I have a Perl-Script, which executes a recursive function. Within it compares two elements of a 2dimensional Array:
I call the routine with a 2D-Array "#data" and "0" as a starting value. First I load the parameters into a separate 2D-Array "#test"
Then I want to see, if the array contains only one Element --> Compare if the last Element == the first. And this is where the Error occurs: Modification of non creatable array value attempted, subscript -1.
You tried to make an array value spring into existence, and the subscript was probably negative, even counting from end of the array backwards.
This didn't help me much...I'm pretty sure it has to do with the if-clause "$counter-1". But I don't know what, hope you guys can help me!
routine(#data,0);
sub routine {
my #test #(2d-Array)
my $counter = $_[-1]
for(my $c=0; $_[$c] ne $_[-1]; $c++){
for (my $j=0; $j<13;$j++){ #Each element has 13 other elements
$test[$c][$j] = $_[$c][$j];
}
}
if ($test[$counter-1][1] eq $test[-1][1]{
$puffertime = $test[$counter][4];
}
else{
for (my $l=0; $l<=$counter;$l++){
$puffertime+= $test[$l][4]
}
}
}
#
#
#
if ($puffertime <90){
if($test[$counter][8]==0){
$counter++;
routine(#test,$counter);
}
else{ return (print"false");}
}
else{return (print "true");}
Weird thing is that I tried it out this morning, and it worked. After a short time of running he again came up with this error message. Might be that I didn't catch up a error constellation, which could happen by the dynamic database-entries.
Your routine() function would be easier to read if it starts off like this:
sub routine {
my #data = #_;
my $counter = pop(#data);
my #test;
for(my $c=0; $c <= $#data; $c++){
for (my $j=0; $j<13;$j++){ #Each element has 13 other elements
$test[$c][$j] = $data[$c][$j];
}
}
You can check to see if #data only has one element by doing scalar(#data) == 1 or $#data == 0. From your code snippet, I do not see why you need to copy the data to passed to routine() to #test. Seems superfluous. You can just as well skip all this copying if you are not going to modify any of the data passed to your routine.
Your next code might look like this:
if ($#test == 0) {
$puffertime = $test[0][4];
} else {
for (my $l=0; $l <= $counter; $l++) {
$puffertime += $test[$l][4];
}
}
But if your global variable $puffertime was initialized to zero then you can replace this code with:
for (my $l=0; $l <= $counter; $l++) {
$puffertime += $test[$l][4];
}
char (* text)[1][45+1];
text = calloc(5000,(130+1));
strcpy(0[*text],"sometext)");
Now I want to encode "sometext" to base58, however, I do not know how, and oddly enough, there isn't one example of BASE58 in C.
The base58 encoding I'm interested in uses these symbols:
123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ
It's been optimized to lessen the risk of mis-reading, so 0 and 'O' are both gone, for instance.
P.S
Don't mind the weird allocation and declaration of the variables, I was experimenting.
You're not supposed to encode strings, you're supposed to encode integers.
If starting with a string, you must first decide how to interpret it as an integer (might be base128, or something), then re-encode in base58.
Satoshi has the reference implementation (https://github.com/bitcoin/bitcoin/blob/master/src/base58.h)
However, he uses some utility bignum class to do it, and it's in C++. If you have access to a bignum library, you just keep dividing by 58 until the number is broken up. If you don't have a bignum library, AFAIK you're outta luck.
Here's an implementation in PHP for large numbers I've created for Amithings, beyond the integers (Integer -> http://php.net/manual/en/language.types.integer.php).
For example, try the example below (Don't forget to pass your ID to the function in string format. Use the PHP function strval()):
$number = '123456789009876543211234567890';
$result = base58_encode($number);
echo('Encoded: ' . $result . '<br>');
echo('Decoded: ' . base58_decode($result) . '<br>');
Important: You may consider to change this routine by including some sort of key/password/encryption to ensure that others can not decode your database IDs.
function base58_encode($input)
{
$alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
$base_count = strval(strlen($alphabet));
$encoded = '';
while (floatval($input) >= floatval($base_count))
{
$div = bcdiv($input, $base_count);
$mod = bcmod($input, $base_count);
$encoded = substr($alphabet, intval($mod), 1) . $encoded;
$input = $div;
}
if (floatval($input) > 0)
{
$encoded = substr($alphabet, intval($input), 1) . $encoded;
}
return($encoded);
}
function base58_decode($input)
{
$alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
$base_count = strval(strlen($alphabet));
$decoded = strval(0);
$multi = strval(1);
while (strlen($input) > 0)
{
$digit = substr($input, strlen($input) - 1);
$decoded = bcadd($decoded, bcmul($multi, strval(strpos($alphabet, $digit))));
$multi = bcmul($multi, $base_count);
$input = substr($input, 0, strlen($input) - 1);
}
return($decoded);
}
My simple code with Crypto++ library:
string base58_encode(Integer num, string vers)
{
string alphabet[58] = {"1","2","3","4","5","6","7","8","9","A","B","C","D","E","F",
"G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c",
"d","e","f","g","h","i","j","k","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
int base_count = 58; string encoded; Integer div; Integer mod;
while (num >= base_count)
{
div = num / base_count; mod = (num - (base_count * div));
encoded = alphabet[ mod.ConvertToLong() ] + encoded; num = div;
}
encoded = vers + alphabet[ num.ConvertToLong() ] + encoded;
return encoded;
}
It's just for cryptocurrency wallets. string can be changed for other tasks.
Here is an implementation that seems to be pure c.
https://github.com/trezor/trezor-crypto/blob/master/base58.c