| |
Welcome to the dBforums forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact contact support.
If you prefer not to see double-underlined words and corresponding ads, place your cursor here for ContentLink opt out.
|
 |

06-07-04, 17:10
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 4
|
|
|
arrays into scalar
|
Hi,
I have an array of binary numbers and I'd like to convert that into hex. I know how to do it using the scalar format, but not with any array.
thanks
|
|

06-07-04, 18:32
|
|
Resident Curmudgeon
|
|
Join Date: Feb 2004
Location: In front of the computer
Posts: 9,573
|
|
You can reference array items something like:
Code:
my @a, $line;
@a = <>;
$line = $a[6];
print reverse($line); # print just line 7 backwards
-PatP
|
|

06-07-04, 19:57
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 4
|
|
|
array of binary into array of hex
|
Thanks Pat,
I know how to reference an array, but for example:
Have:
@array = 0 1 0 0 1 0 1 0;
Need:
@array = 4A
I don't know how to use the built in functions for hex and binary because I need 4 elements of the array at a time to convert.
|
|

06-07-04, 23:19
|
|
Resident Curmudgeon
|
|
Join Date: Feb 2004
Location: In front of the computer
Posts: 9,573
|
|
There is probably a quicker way, but you could use:
Code:
# ptp 20040607 Repackage array of bool as a variable
my @a, $val;
@a = (0, 1, 0, 0, 1, 0, 1, 0);
$val = 0;
foreach my $i (@a) { $val = 2 * $val + $i };
print "The value is: $val\n\n";
-PatP
|
|

06-08-04, 02:35
|
|
Registered User
|
|
Join Date: Jan 2004
Location: Germany
Posts: 167
|
|
Quote:
|
Originally Posted by Dittocat15
Hi,
I have an array of binary numbers and I'd like to convert that into hex. I know how to do it using the scalar format, but not with any array.
thanks
|
To get a scalar of the array you can use join (join("",@array)) and the you can convert the scalar!
__________________
board.perl-community.de - The German Perl-Community
|
|

07-09-04, 14:38
|
|
Registered User
|
|
Join Date: Jun 2004
Location: Nowhere Near You
Posts: 89
|
|
Code:
my(@a) = (1, 1, 1, 1, 1, 1, 1, 0);
$a=sprintf "%10lx",oct("0b".join('',@a));
#print $a;
|
|

07-09-04, 22:03
|
|
Registered User
|
|
Join Date: Jun 2004
Location: Nowhere Near You
Posts: 89
|
|
Of course if your "binary string" is really long ...
Code:
#!\user\bin\perl -w
my(@a)=(1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0);
print BinToHex(\@a),"\n";
sub BinToHex{
my(%h)=('1000'=>'1','0100'=>'2','1100'=>'3','0010'=>'4','1010'=>'5','0110'=>'6','1110'=>'7','0001'=>'8','1001'=>'9','0101'=>'A','1101'=>'B','0011'=>'C','1011'=>'D','0111'=>'E','1111'=>'F'
,'1'=>'1', '01'=>'2', '11'=>'3', '001'=>'4', '101'=>'5', '011'=>'6', '111'=>'7');
$_=reverse join('',@{$_[0]});
s/([01]{1,4})/$h{$1}/g;
return scalar reverse;
};
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|