#!/usr/bin/env perl ## 0001-binaryTransform.pl - Jake Deery, 2023 use warnings; use strict; use CGI; ## initialise cgi & headers my $cgi = CGI->new; print $cgi->header({ -type => "text/plain", -charset => "utf-8", }); ## global vars my $verbose = 0; my $binaryStr = "00000001 00000010 00000100 00001000"; my $numberStr = ""; sub main { ## vars my @binaryArr = split(' ', $binaryStr); my @numberArr = (); ## loop through binary array for(my $i = 0; $i < ($#binaryArr + 1); $i++) { my $presentInt = 0; ## loop through this element for(my $j = 0; $j < 8; $j++) { my $char = substr($binaryArr[$i], $j, 1); $presentInt += 128 if($j == 0 && $char == "1"); $presentInt += 64 if($j == 1 && $char == "1"); $presentInt += 32 if($j == 2 && $char == "1"); $presentInt += 16 if($j == 3 && $char == "1"); $presentInt += 8 if($j == 4 && $char == "1"); $presentInt += 4 if($j == 5 && $char == "1"); $presentInt += 2 if($j == 6 && $char == "1"); $presentInt += 1 if($j == 7 && $char == "1"); } push(@numberArr, $presentInt); } $numberStr = join(' ', @numberArr); print("\$binaryStr = " . $binaryStr . "\n"); print("\$numberStr = " . $numberStr . "\n"); return 0; } main();