in reply to Perl/TK hex2char - char2hex convertor

I like it, but i do have a couple of suggestions:

First, please pick an indentation style and stick with it. Check out perltidy.

Second, you are using foreach where you should be using map, and you are using map where you should be using foreach (or for). Try this instead:
sub convert_hex { my @charconv = map { pack "H*", $_ } split(/\s/, $hexval->get); $charval->delete(0, 'end'); $charval->insert(0, $_) for reverse @charconv; } sub convert_char { my @hexconv = map { unpack "H*", $_ } split(//, $charval->get); $hexval->delete(0, 'end'); $hexval->insert(0, $_) for reverse @hexconv; }
Use map when you want to transform one list (array) into another - don't use it in void context, use for (or foreach) instead. Other then that, looks good to me. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Perl/TK hex2char - char2hex convertor
by Anonymous Monk on May 30, 2003 at 03:18 UTC
    I guess I am the only one, but I was not able to successfully run the script. I get a error "Unrecognized character \xAD at test.pl line 4." when I execute it. I am not a Perl programmer but I need to convert a file from hex to chars. Any idea why? Thanks Dan
      This is probably the wrong thread ... check out converting hex to char instead. Also, the following might be more helpful for what you need:
      use strict; use warnings; print char2hex('a'),$/; print hex2char(62),$/; sub char2hex { return sprintf('%x',ord($_[0])); } sub hex2char { return chr(hex($_[0])); }
      Goo luck :)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)