Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

raw series of bytes in HEx

Status
Not open for further replies.

jez

Programmer
Apr 24, 2001
370
VN
Hi All,

I have a raw series of bytes in Hexidecimal that i need to
1) convert to something human readable.
2) possibly write back to this format from something readable.

I know it is a description for a colour, but that is about it.

Any one any ideas on what perl i need for this.

Here is a sample

Code:
thanks.
040B7374 7265616D 74797065 6481E803 84014084 8484074E 53436F6C 6F720084 84084E53 4F626A65 63740085 84016301 84046666 666683F4 FD743F83 B072083F 8377BE3F 3F0186
 
What do you mean by 'human readable'?
Are they represented always by 4 consecutive hex chars ? (not in your example)
Do you have them, presumably on some file, already in string format (as in your example) or as plain hex chars? In the second case, how are single numbers separated?
As you need to know perl to use any supplied piece of code, what have you tried (or searched) so far?

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Well, firstly thanks for the response.

I now know that it is a base64 encoded 'raw byte stream in hexideciaml'.
It is from an Apple plist file (standard preferences file for a mac).

By human readable, i mean that the sample is what has been put into this file by an OS X application to describe the background colour of a row in that app. The file containing them is a UTF-8 xml file (so readable with a text editor).
The spacing is as above.

I am trying to create these plist files from perl so that before opening the app, some settings can be created, almost like making a template file.

So i need to know what it says in order to then write back in the same format.

As for what i have tried;-

Code:
print STDOUT MIME::Base64::decode($str);
which prints a load of odd characters that can only be described as those that appear in the extended range of ASCII (like smiley faces etc).


I know it is colour definitions, so i have so far taken a different route to this by creating the colours i need in a sample file and simply storing them in a look up file.
So it may say "The background colour is Blue", or simply "Blue" or something else, i dont know.
 
Something along the lines of...

Code:
# this is your binary data, just with values here
# for the example
my $bin = chr(0x04) . chr(0x0B) . chr(0x73) . chr(0x74);

# read one character at a time
my $human = '';
for (my $i = 0; $i < length($bin); $i++) {
   my $chr = substr($bin,$i,1);

   # unpack it to decimal
   my $dec = unpack("C", $chr);

   # sprintf it into human-readable hex
   my $hex = sprintf("%02x", $dec);

   $human .= $hex;
}
print "Human-readable: $human\n";

# encode this stuff back into binary
my $newBin = '';
for (my $i = 0; $i < length($human); $i += 2) {
   # note that your hex is 2 bytes per char
   my $hex = substr($human,$i,2);

   # turn the hex into a number
   my $dec = hex("0x$hex");

   # pack it
   my $byte = pack("C", $dec);

   # add it to the binary string
   $newBin .= $byte;
}

# test if we did this correctly
if ($bin eq $newBin) {
   print "Bingo!\n";
}

Code:
Human-readable: 040b7374
Bingo!

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Thanks, however i am a little confused.

The ouput on the command line is confused but if i pipe it to a text file so that i can put the characters through the code you suggest i get different values.

Also i cannot programmatically get the chr codes of each characters... they appear like the extended character set but that does not mean that is what they are.


I appreciate your efforts but i think i am chasing the wrong solution. I don't think i will be able to properly write these files without using some OS library from OSX, then maybe something like the foundation module to link to perl.

My current solution of simply creating many of these strings seems to be working effectively, although i am confused as to why someone would populate an xml file with other data that is is plain text (i.e. all other data in the xml) yet encode this bit. I have also found that it does contain more than simply a colour;-

my output from base64 decode when opened in Komodo editor shows this;-
Code:
streamtyped?è?@???NSColor
 
Solved.... with this excerpt from the active state website....


Code:
SUPPORTED PLATFORMS

    * Linux
    * Solaris
    * Windows


Thats not mac created base64 then is it. No wonder i am not getting sense out of it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top