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

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!mcsun!sunic!aun.uninett.no!nuug!nntp.uio.no!nntp.uio.no!aas
  3. From: aas@rosmer.nr.no (Gisle Aas)
  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: <AAS.93Jan26093556@rosmer.nr.no>
  7. Sender: news@ulrik.uio.no (Mr News)
  8. Nntp-Posting-Host: rosmer.nr.no
  9. Reply-To: Gisle.Aas@nr.no
  10. Organization: Norwegian Computing Centre, Oslo, Norway
  11. References: <C1FnMG.AMB@ibeam.intel.com> <C1Fs2F.BB3@ibeam.intel.com>
  12. Date: Tue, 26 Jan 1993 08:35:56 GMT
  13. Lines: 25
  14.  
  15. In article <C1Fs2F.BB3@ibeam.intel.com> gweil@ibeam.intel.com (Garry Weil) writes:
  16. >   >How do I convert "Test" to 0x54 0x65 0x73 0x74 ?
  17. >
  18. >   Is there something better?
  19. >
  20. >   BTW, I want to separate hex values with ",".
  21.  
  22. How about:
  23. perl -e 'for (split(//,"Test")) {
  24.              $a .= sprintf("0x%x,", ord)
  25.          }
  26.          chop $a; print $a'
  27.  
  28. or
  29.  
  30. #!/usr/local/bin/perl
  31. while (<>) {
  32.    chop;
  33.    print join(',', grep($_="0x".$_,unpack("H2" x length, $_))), "\n";
  34. }
  35. --
  36. Gisle Aas               |  snail: Boks 114 Blindern, N-0314 Oslo, Norway
  37. Norsk Regnesentral      |  X.400: G=Gisle;S=Aas;O=nr;P=uninett;C=no
  38. voice: +47-2-453561     |  inet:  Gisle.Aas@nr.no
  39.  
  40.