As shown in the code . I have taken a hex number converted to binary , added it to an array and reshuffled .
I want to convert it back to hex and stuck at the step where converting the array to a variable and then to hexa. I have used couple of steps which I comented and they didn't work. Is there a way to convert an array to variable and then to hex for my case
#!/usr/bin/env perl
use warnings;
import math
my $value = hex ( 'FE300B0' );
print "Dec: ",$value,"\n";
printf ( "Hex: %X\n", $value );
my $binval = sprintf ( "%0*b\n",32, $value );
print $binval;
my @binval_arr = split('', $binval, 32);
#print "Size: ",scalar @binval_arr,"\n";
#print "\$binval_arr[27] = $binval_arr[27]\n";
print @binval_arr;
#my @mod_bin = @binval_arr;
#my @mod_bin;
my @mod_bin = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
#modifeid 20..31
@mod_bin[20..31] = @binval_arr[20..31];
@mod_bin[9] = $binval_arr[10];
@mod_bin[18,19] = @binval_arr[14,15];
@mod_bin[10..17] = @binval_arr[2..9];
#print join("\n",@binval_arr),"\n";
print @mod_bin,"\n";
#my $mod_str = join('', @mod_bin);
#print $mod_str,"\n";
my $mod_int = int(@mod_bin);
my $mod_int = \@mod_bin;
print $mod_int,"\n";
output ::
Dec: 266535088
Hex: FE300B0
00001111111000110000000010110000
00001111111000110000000010110000
00000000010011111111000010110000
ARRAY(0x5635e48dcca8)
CodePudding user response:
You shouldn't be converting to/from binary at all. You should be using bit arithmetic operators.
my $value = hex( 'FE300B0' );
my $modified =
( ( $value >> 2 ) & 0x0FFF ) << 2
| ( ( $value >> 21 ) & 0x0001 ) << 22
| ( ( $value >> 16 ) & 0x0003 ) << 12
| ( ( $value >> 22 ) & 0x00FF ) << 14;
Note that this assigns to bit 13 ($mod_bin[18]
in the original) twice, just like your code. This is surely a mistake?
CodePudding user response:
One way to do the binary to hex operation is:
$hex = sprintf "%x", oct("0b$binary");
To convert an array of "0" and "1" elements into a 32-character scalar of "0" and "1" characters, use
$binary = join("", @mod_bin);
CodePudding user response:
Complete code which works as intended. This was to reshuffle bits inside a 32 bit addrs for internal use in Dept of CPU registers . There may be easy ways in Perl than this lengthy code .
#!/usr/bin/env perl
use warnings;
import math
my $value = hex (FE300B0);
print "Dec: ",$value,"\n";
printf ( "Hex: %X\n", $value );
my $binval = sprintf ( "%0*b\n",32, $value );
print $binval;
my @binval_arr = split('', $binval, 32);
#print "Size: ",scalar @binval_arr,"\n";
#print "\$binval_arr[27] = $binval_arr[27]\n";
print @binval_arr;
#my @mod_bin = @binval_arr;
#my @mod_bin;
my @mod_bin = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
@mod_bin[20..31] = @binval_arr[20..31];
@mod_bin[9] = $binval_arr[10];
@mod_bin[18,19] = @binval_arr[14,15];
@mod_bin[10..17] = @binval_arr[2..9];
#print join("\n",@binval_arr),"\n";
print @mod_bin,"\n";
my $mod_str = join('', @mod_bin);
#print $mod_str,"\n";
#my $mod_int = int(@mod_bin);
#my $mod_int = \@mod_bin;
#print $mod_int,"\n";
my $mod_hex = sprintf "%x", oct("0b$mod_str");
print $mod_hex,"\n";