home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / perl / 8010 < prev    next >
Encoding:
Text File  |  1993-01-28  |  1.3 KB  |  42 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!ascent!ascent!paul
  3. From: paul@ascent.com (Paul Foley)
  4. Subject: Re: How to convert string to hex?
  5. In-reply-to: gweil@ibeam.intel.com's message of Tue, 26 Jan 1993 00:59:02 GMT
  6. Message-ID: <PAUL.93Jan26141847@MountRushmore.ascent.com>
  7. Date: 26 Jan 93 14:18:47
  8. References: <C1FnMG.AMB@ibeam.intel.com> <C1Fs2F.BB3@ibeam.intel.com>
  9. Organization: Ascent Technology, Inc., Cambridge Massachusetts
  10. Lines: 30
  11.  
  12. In article <C1Fs2F.BB3@ibeam.intel.com> gweil@ibeam.intel.com (Garry Weil) writes:
  13.  
  14.    >How do I convert "Test" to 0x54 0x65 0x73 0x74 ?
  15.  
  16.    The best I have come up with, so far is the following:
  17.  
  18.    #!/usr/local/bin/perl
  19.    while (<>) {
  20.        $_ = reverse $_ ;
  21.        for ($i=1; $i < length($_);) {
  22.            $c = chop ;
  23.            printf "0X%x", ord $c ;
  24.            printf ", " unless $i == length ($_) ;
  25.        }
  26.    }
  27.    print "\n" ;
  28.  
  29.    Is there something better?
  30.  
  31. How about ...
  32.  
  33. #!/usr/local/bin/perl
  34. while (chop($line = <>)) {
  35.   print join(', ', grep(s/(.*)/sprintf("0x%x", ord($1))/e, split(/ */, $line))), "\n";
  36. }
  37. --
  38. --------------------------------------------------------------------------------
  39. Paul Foley                            Ascent Technology Inc.
  40. Development Engineer                    64 Sidney St. Cambridge, MA 02139
  41. paul@ascent.com                            (617) 225-0850 x315
  42.