Help convert [00] from a string to hex

Discussion in 'Perl Misc' started by tgillitzer, Mar 13, 2005.

  1. tgillitzer

    tgillitzer Guest

    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
    tgillitzer, Mar 13, 2005
    #1
    1. Advertising

  2. tgillitzer

    Tony Curtis Guest

    >> 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
    Tony Curtis, Mar 13, 2005
    #2
    1. Advertising

  3. tgillitzer

    Tim Heaney Guest

    "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
    Tim Heaney, Mar 13, 2005
    #3
  4. tgillitzer

    tgillitzer Guest

    > >> 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
    tgillitzer, Mar 13, 2005
    #4
  5. tgillitzer

    tgillitzer Guest

    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
    tgillitzer, Mar 13, 2005
    #5
  6. * 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
    Fabian Pilkowski, Mar 13, 2005
    #6
    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. Replies:
    10
    Views:
    6,189
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    465
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    585
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    18,848
  5. chirs
    Replies:
    3
    Views:
    161
    Dr John Stockton
    Dec 1, 2003
Loading...

Share This Page