Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help convert [00] from a string to hex

Reply
Thread Tools

Help convert [00] from a string to hex

 
 
tgillitzer
Guest
Posts: n/a
 
      03-13-2005
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

 
Reply With Quote
 
 
 
 
Tony Curtis
Guest
Posts: n/a
 
      03-13-2005
>> On 13 Mar 2005 11:19:34 -0800,
>> "tgillitzer" <> said:


> 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
 
Reply With Quote
 
 
 
 
Tim Heaney
Guest
Posts: n/a
 
      03-13-2005
"tgillitzer" <> writes:
>
> 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
 
Reply With Quote
 
tgillitzer
Guest
Posts: n/a
 
      03-13-2005
> >> On 13 Mar 2005 11:19:34 -0800,
> >> "tgillitzer" <> said:

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


 
Reply With Quote
 
tgillitzer
Guest
Posts: n/a
 
      03-13-2005

Tim Heaney wrote:
> "tgillitzer" <> writes:
> >
> > 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!

>
> might be what you're after.
>
> I hope this helps,
>
> Tim


 
Reply With Quote
 
Fabian Pilkowski
Guest
Posts: n/a
 
      03-13-2005
* Tim Heaney wrote:
> "tgillitzer" <> writes:
> >
> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hex string to hex value tim Python 8 11-23-2005 06:27 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
How to convert an hex string to a Hex number chirs Javascript 3 12-01-2003 10:06 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments