Help convert [00] from a string to hex

T

tgillitzer

I have a non-fixed length string that contains hex values in a readable
format and other text. I need to convert the hex values [..] to thier
respective hex value (using pack i would assume).

Lets say the string is:
'[FF]test[FF]test[FF]test[FF]'

I need to convert [..] to the hex value.
So, the result would be:
'ÿtestÿtestÿtestÿ' if printed (where ÿ is the hex value converted
to ascii of the display font)

I'm not sure how to do this with perl, but any help would be
appreciated.

Thanks
 
T

Tony Curtis

On 13 Mar 2005 11:19:34 -0800,
I have a non-fixed length string that contains hex values in
a readable format and other text. I need to convert the hex
values [..] to thier respective hex value (using pack i
would assume).

Err, that's a no-op.
Lets say the string is: '[FF]test[FF]test[FF]test[FF]'
I need to convert [..] to the hex value. So, the result
would be: 'ÿtestÿtestÿtestÿ' if printed (where ÿ is the hex
value converted to ascii of the display font)

But it already *is* a hex value!

Do you mean:

I have a string containing delimited hexadecimal numerals
and I want to extract those substrings, and replace them
with the character, from the current character set, whose
numeric value is represented by those numerals

?

If so, you're probably looking for

perldoc -f hex
perldoc -f chr

hth
t
 
T

Tim Heaney

tgillitzer said:
Lets say the string is:
'[FF]test[FF]test[FF]test[FF]'

I need to convert [..] to the hex value.
So, the result would be:
'ÿtestÿtestÿtestÿ' if printed (where ÿ is the hex value converted
to ascii of the display font)

Something like

$string =~ s/\[([0-9A-F]{2})\]/chr(hex($1))/eg;

might be what you're after.

I hope this helps,

Tim
 
T

tgillitzer

On 13 Mar 2005 11:19:34 -0800,
I have a non-fixed length string that contains hex values in
a readable format and other text. I need to convert the hex
values [..] to thier respective hex value (using pack i
would assume).

Err, that's a no-op.
Lets say the string is: '[FF]test[FF]test[FF]test[FF]'
I need to convert [..] to the hex value. So, the result
would be: 'ÿtestÿtestÿtestÿ' if printed (where ÿ is the hex
value converted to ascii of the display font)

But it already *is* a hex value!
No. It is four bytes that represent a single byte that was recieved.
Such as, a single byte of FF is recieved and is displayed as [FF]
instead of ÿ. I need to convert it back to the single byte version.
 
T

tgillitzer

Tim said:
tgillitzer said:
Lets say the string is:
'[FF]test[FF]test[FF]test[FF]'

I need to convert [..] to the hex value.
So, the result would be:
'ÿtestÿtestÿtestÿ' if printed (where ÿ is the hex value converted
to ascii of the display font)

Something like

$string =~ s/\[([0-9A-F]{2})\]/chr(hex($1))/eg;

Ah... the e option. Guess I should have looked more closely at that.
This took care of it, as I was able to use pack.
$_ =~ s/\[(..)\]/pack('H2',$1)/eg;
seems to have taken care of it. Thanks!
 
F

Fabian Pilkowski

* Tim Heaney said:
tgillitzer said:
Lets say the string is:
'[FF]test[FF]test[FF]test[FF]'

I need to convert [..] to the hex value.
So, the result would be:
'ÿtestÿtestÿtestÿ' if printed (where ÿ is the hex value converted
to ascii of the display font)

Something like

$string =~ s/\[([0-9A-F]{2})\]/chr(hex($1))/eg;

might be what you're after.

Based on his own idea ("using pack i would assume") he could also use
pack() in this substition:

$string =~ s/\[([0-9A-F]{2})\]/pack'H*',$1/eg;

Nevertheless, there are many other possibilities of reaching this.

regards,
fabian
 

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

Members online

No members online now.

Forum statistics

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

Latest Threads

Top