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

L

lha404

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.
 
G

Gunnar Hjalmarsson

lha404 said:
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
 
B

Brian McCauley

lha404 said:
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.
 
F

Fabian Pilkowski

* Brian McCauley said:
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
 
B

Brian McCauley

Fabian said:
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.
 
L

lha404

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top