Reg file exported from XP/2003 - enumerate HEX to ASCII - my head hurts

Discussion in 'Perl Misc' started by lha404, Mar 12, 2005.

  1. lha404

    lha404 Guest

    Hello

    I am new to perl, I am looking to write a perl script which will
    convert the the HEX to ASCII for registry exports - an example
    follows

    [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\User
    Shell Folders]
    "Common Startup"=hex(2):25,00,41,00,4c,00,4c,00,55,00,53,00,45,00,52,00,53,00,\

    converted = % A L L U S E R
    S

    this translates in the following way
    25 ~ decimal 37 ~ ASCII %
    41 ~ decimal 61 ~ ASCII A
    4c ~ decimal 65 ~ ASCII L
    4c ~ decimal 65 ~ ASCII L
    bla bla bla ...

    There are two reasons for doing this :

    1 - I am learing perl and have absorbed some of the basics but when
    presented with a real problem such as this am finding it hard to
    decide what the best approach would be and would like some advice on
    how to decide on stratagies for tackling problems. I can imagine a
    number of ways to convert the text but the two main concepts are.

    - strip out the hex code from a file then assign an arrary the values
    then work on the elements of the array sending the output to another
    file.
    - use transiteration to substitute the vales


    2 - because I frequently have to look at regexports which have this
    particualr formatting of ASCII and have no control over the
    originating process.
    The current ways of converting are a complete waste of time.

    For all I know there is some simple way of doing this that I have
    overlooked or there is a perl module which can convert, I really dont
    know where to start though.

    It seems so simple but now my brain hurts and just need some sort of
    idea from another human being.

    I dont expect any one gives a toss but even the process of posting my
    problem may give me ideas.
    lha404, Mar 12, 2005
    #1
    1. Advertising

  2. lha404

    Guest

    HaHaHa Dude

    you are a looser !

    So easy - keep reading

    PNF
    , Mar 12, 2005
    #2
    1. Advertising

  3. lha404

    Guest

    wrote:
    > HaHaHa Dude
    >
    > you are a looser !
    >
    > So easy - keep reading
    >
    > PNF
    , Mar 12, 2005
    #3
  4. Re: Reg file exported from XP/2003 - enumerate HEX to ASCII - myhead hurts

    lha404 wrote:
    > I am new to perl, I am looking to write a perl script which will
    > convert the the HEX to ASCII for registry exports - an example
    > follows
    >
    > [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\User
    > Shell Folders]
    > "Common Startup"=hex(2):25,00,41,00,4c,00,4c,00,55,00,53,00,45,00,52,00,53,00,\
    >
    > converted = % A L L U S E R
    > S


    perldoc -f split
    perldoc -f hex
    perldoc -f chr

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Mar 12, 2005
    #4
  5. Re: Reg file exported from XP/2003 - enumerate HEX to ASCII - myhead hurts

    lha404 wrote:

    > I am new to perl, I am looking to write a perl script which will
    > convert the the HEX to ASCII for registry exports - an example
    > follows


    > [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\User
    > Shell Folders]
    > "Common Startup"=hex(2):25,00,41,00,4c,00,4c,00,55,00,53,00,45,00,52,00,53,00,\
    >
    > converted = % A L L U S E R
    > S
    >
    > this translates in the following way
    > 25 ~ decimal 37 ~ ASCII %
    > 41 ~ decimal 61 ~ ASCII A
    > 4c ~ decimal 65 ~ ASCII L
    > 4c ~ decimal 65 ~ ASCII L
    > bla bla bla ...


    No, it does not - that just happens to work for most cases.

    The hex in a Windows registry export file does not represent ASCII text.
    It represents Unicode text encoded in UTF16LE. To convert this back to a
    Unicode text string you'd do the following:

    my $hex_data='25,00,41,00,4c,00,4c,00,55,00,53,00,45,00,52,00,53,00';

    use Encode;
    my $data = decode 'utf16le',pack 'C*',map hex,split /,/,$hex_data;

    Note: $data is now a Unicode text string. It is best to think of it as
    a seqence of Unicode code-points not a sequence of bytes, even though
    internally Perl stores it as a sequence of bytes that are the UTF8
    representation of the Unicode string.

    > I frequently have to look at regexports which have this
    > particualr formatting of ASCII and have no control over the
    > originating process.


    Like I said, it's _not_ ASCII.

    > For all I know there is some simple way of doing this that I have
    > overlooked or there is a perl module which can convert, I really dont
    > know where to start though.


    The trick is to realise it's Unicode not ASCII - then it should become
    reasonably self evident that the Perl Unicode documentation would be a
    good starting point.

    Note: Perl Unicode support did not really exist in any useful sense in
    5.6.x and before.
    Brian McCauley, Mar 13, 2005
    #5
  6. Re: Reg file exported from XP/2003 - enumerate HEX to ASCII - my head hurts

    * Brian McCauley wrote:
    >
    > The hex in a Windows registry export file does not represent ASCII text.
    > It represents Unicode text encoded in UTF16LE. To convert this back to a
    > Unicode text string you'd do the following:
    >
    > my $hex_data='25,00,41,00,4c,00,4c,00,55,00,53,00,45,00,52,00,53,00';
    > use Encode;
    > my $data = decode 'utf16le',pack 'C*',map hex,split /,/,$hex_data;


    Only as an addition: The use of hex() is unnecessary if one uses another
    pack format. The character 'H' can do this for you automatically:

    my $data = decode 'utf16le', pack '(H*)*', split /,/, $hex_data;

    regards,
    fabian
    Fabian Pilkowski, Mar 13, 2005
    #6
  7. Re: Reg file exported from XP/2003 - enumerate HEX to ASCII - myhead hurts

    Fabian Pilkowski wrote:

    > * Brian McCauley wrote:
    >
    >>my $hex_data='25,00,41,00,4c,00,4c,00,55,00,53,00,45,00,52,00,53,00';
    >>use Encode;
    >>my $data = decode 'utf16le',pack 'C*',map hex,split /,/,$hex_data;

    >
    >
    > Only as an addition: The use of hex() is unnecessary if one uses another
    > pack format. The character 'H' can do this for you automatically:
    >
    > my $data = decode 'utf16le', pack '(H*)*', split /,/, $hex_data;


    Thanks for that.

    I never can get my head 'round what 'H' does in pack/unpack templates.
    Also I'd never even been aware of () in pack templates.
    Brian McCauley, Mar 13, 2005
    #7
  8. lha404

    Guest

    Thanks for all the help / advice

    I now have a script that does what is needed.

    When I have time I will try to do thing properly as Unicode.
    , Mar 15, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. spud
    Replies:
    24
    Views:
    1,014
    Duende
    Jan 30, 2004
  2. Replies:
    10
    Views:
    6,190
    Neredbojias
    Aug 19, 2005
  3. Bengt Richter
    Replies:
    6
    Views:
    465
    Juha Autero
    Aug 19, 2003
  4. Jeffrey Barrett

    my head hurts

    Jeffrey Barrett, Jul 21, 2004, in forum: C Programming
    Replies:
    2
    Views:
    303
    Malcolm
    Jul 21, 2004
  5. Jeffrey Barrett

    my head hurts

    Jeffrey Barrett, Jul 21, 2004, in forum: C Programming
    Replies:
    1
    Views:
    322
    =?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=
    Jul 21, 2004
Loading...

Share This Page