Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › having perl read data
New Posts  All Forums:Forum Nav:

having perl read data

post #1 of 2
Thread Starter 
im new-ish to programming. I've been programming for a little over a year now but I have yet to understand exactly how to load a binary file into a perl script or rather have the perl script read a binary file. For example, I have a file that contains the bits, (no spaces)

01000001 01000010 01000011

in ASCII this is, ABC. How can i have a my script do something like the following? I'm perfectly happy doing this in hex if that's easier. 0x414243
Code:
if ($bit1 == 1) {
     print "true"; 
} else {
     print "false"; 
}
dBag
(9 items)
 
yep...
(10 items)
 
air
(8 items)
 
CPUMotherboardRAMHard Drive
xeon e3-1230v2 supermicro mbd x9scm-0 KVR1333D3E9S/4G OCZ vertex3 60GB 
CoolingOSPowerCase
1u 80mm blower + passive cpu  proxmox 3.1 supermicro 300w 1u supermicro 1u 
Other
2x intel pro1000 nics  
CPUMotherboardGraphicsRAM
A8-3870K @ 3.7GHz GIGABYTE A75M-UD2H Power Cooler 7870 Team Group Inc 
Hard DriveCoolingOSPower
3x OCZ Agility 3 60GB (RAID-0 on Rocket RAID 27... Corsair H-80 core win-7 Seagate 750 Gold 
CaseAudio
some lian-li case... M-Audio AV40s + 10" Sony ___? 
CPUMotherboardGraphicsRAM
D525 @ 1.8GHz Supermicro onboard 2x2GB DDR1333 
Hard DriveOSCaseOther
WD Green ubuntu 12 1u  3x40mm fans  
  hide details  
Reply
dBag
(9 items)
 
yep...
(10 items)
 
air
(8 items)
 
CPUMotherboardRAMHard Drive
xeon e3-1230v2 supermicro mbd x9scm-0 KVR1333D3E9S/4G OCZ vertex3 60GB 
CoolingOSPowerCase
1u 80mm blower + passive cpu  proxmox 3.1 supermicro 300w 1u supermicro 1u 
Other
2x intel pro1000 nics  
CPUMotherboardGraphicsRAM
A8-3870K @ 3.7GHz GIGABYTE A75M-UD2H Power Cooler 7870 Team Group Inc 
Hard DriveCoolingOSPower
3x OCZ Agility 3 60GB (RAID-0 on Rocket RAID 27... Corsair H-80 core win-7 Seagate 750 Gold 
CaseAudio
some lian-li case... M-Audio AV40s + 10" Sony ___? 
CPUMotherboardGraphicsRAM
D525 @ 1.8GHz Supermicro onboard 2x2GB DDR1333 
Hard DriveOSCaseOther
WD Green ubuntu 12 1u  3x40mm fans  
  hide details  
Reply
post #2 of 2
There's a few ways you could do this:

The first, being my preferred method, is using the split function to turn the string into a single character array and then you can query each character on it's own (arrays will start from zero).
Code:
#!/usr/bin/perl

my $binary_string = "010000010100001001000011";

my @binary_array = split (//, $binary_string);

if ($binary_array[0] == 1) {
        print "true";
} else {
        print "false";
}

You can also do this with substrings:
Code:
#!/usr/bin/perl

my $binary_string = "010000010100001001000011";

if (substr($binary_string, 0, 1) == 1) {
        print "true";
} else {
        print "false";
}

It's probably also worth noting that in both of the above examples, we're abusing the loose type system. What I mean by this is it's technically a string comparison so we really should be doing the comparison like this:
Code:
if ($binary_value eq "0") {
        # do something
}
however is the value is always either 1 or 0, we can get away with a numeric comparison. But it's worth noting this because if you're getting your binary data from an unclean source (eg user input), then you'll need to cleanse it first (eg remove any alpha characters) else your program will crash. Thankfully you can strip out those characters quite easily with some clever regex in the split function in the first code example. Something like /([^0-9]|)/ might do the trick, but I've not tested that code.

Lastly, if you want to get really sophisticated, it might be worth investigating CPAN for some pre-existing modules. Depending on your end goal of this project, you may find some killer modules.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › having perl read data