[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Suggest a tool for decoding binary data



On Sun, Feb 2, 2014 at 1:28 AM, Chen Wei <weichen302@icloud.com> wrote:
> On Sat, Feb 01, 2014 at 05:36:19PM -0800, Kevin O'Gorman wrote:
>> I'm about to tackle GPT partitioned disks, and want to decode the
>> label.
>>
>> The data is little-endian, but I want my code to work on little- or
>> big-endian machines.  I want it to be a script -- nothing compiled.
>>
>> I've figured out that on my little-endian machines, I can use bash
>> with something like
>> otherlabel=$(($(dd if=label bs=1 skip=32 count=8 | od -An -t d8) ))
>
>
> I am not quit understand why need dump the label, but it can be done
> easily by script language, say, Python.
>
>
> # --------- begin --------------
> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
>
> import sys
> from struct import unpack
>
> skip = 32
> count = 8
> fp = open(path_to_dd_image, 'rb').read(skip + count)
> data_of_interest = fp[skip:]
>
> if sys.byteorder == 'little':
>     #assuming the label is a 8 bytes unsigned integer
>     label = unpack('<Q', data_of_interest)[0]
> else:
>     label = unpack('>Q', data_of_interest)[0]
> # --------- end --------------

Ah, thank you, that is a nice start.  I'll remove the test for system
byteorder because the data is always little-endian.  It's the sytem I
was worred about and the '<' is the ticket.

-- 
Kevin O'Gorman

programmer, n. an organism that transmutes caffeine into software.
Please consider the environment before printing this email.


Reply to: