Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
parsing of 8bit binary data bitwise  

Posted by dharshana.ve (dharshana.ve), 9 November 2006
Hi all,

I wanted to know how to parse a 8bit binary data bitwise?

I have my input in hex and i convert it into binary form using the following logic:

%h2b = (0 => "0000", 1 => "0001", 2 => "0010", 3 => "0011",
4 => "0100", 5 => "0101", 6 => "0110", 7 => "0111",
8 => "1000", 9 => "1001", a => "1010", b => "1011",
c => "1100", d => "1101", e => "1110", f => "1111",
);

$hex = "43";
($binary = $hex) =~ s/(.)/$h2b{lc $1}/g;
print $binary, "\n";

Now the output for the above prg is

01000011

Now i have to display this binary data as shown below:

0=> abc
100=> def
0011=> lmn

How to go about this?

Posted by admin (Graham Ellis), 9 November 2006
Hi - does this question replace the one you posted an hour or two back, or is it a different one? Does our binary data handler help answer both or either? ... that demostrates conversions to and from binary (0 and 1) strings which I think is what you're lookig at.

Posted by dharshana.ve (dharshana.ve), 9 November 2006
The previous problem i solved using the code which i have specified in the second mail.

This problem is a fresh one. Let me explain it better

I get data in hex bytes from a log file. I have to parse them or analyze them according to some predefined rules or values.

For eg: a hex byte of 0xa4 is converted to binary as 10100100

Now, i have a file in which the values are predefined. It looks like

0xa4 => 10100100

in that MSB 1 stands for some abc
then the following bits 01 stands for cdf
then the remaining bits 00100 stands for xyz

(0/1)      |             2 bits               |            5 bits
abc       |        System name        |        Other details

System name                                     Other Details
00      ujj                                             00000    
01      cdf                                            00001 etc....
10      bbg
11      gtt


In the above example, the text is just an eg and not the actual text.

I get those 8 bits as a whole as hex byte which i need to parse and diplay them bitwise.


Hope this explains better.



Posted by admin (Graham Ellis), 9 November 2006
Is this the conversion you need:

Code:
dolphin:~ graham$ perl
$k = "a3";
printf("%b",hex($k));
10100011dolphin:~ graham$


Posted by dharshana.ve (dharshana.ve), 10 November 2006
Thanks for your response. I sincerely appreciate it.

I got the conversion what i needed.... It is something like the following.

use strict;
use warnings;

my $hex = "ab";

my $binary = unpack 'B*', pack 'H*', $hex;

print "$binary\n\n";

my @fields = unpack 'A1A3A4', $binary;

my $i;
printf "%-4s => %s\n", $fields[$i++], $_ foreach qw/abc def lmn/;

**OUTPUT**

10101011

1         abc
010     def
1011   lmn


Thanks once again Graham!!!!



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho