Article View: chile.comp.programacion
Article #1331Re: Reemplazar caracteres por hexadecimal en Perl
From: Cristian Gutierrez
Date: Wed, 27 Sep 2006 03:20
Date: Wed, 27 Sep 2006 03:20
29 lines
2037 bytes
2037 bytes
Hoy en la tarde, chano@lj.NOSPAM.cl dijo:
> Chano <chano@lj.nospam.cl> escribio:
>> Necesito reemplazar en un string cada uno de los caracteres por un
>> c�digo hexadecimal. Pensaba poner el listado de equivalencias en un
>> hash y luego usar una expresion regular de reemplazo para
>> hacerlo... pero me qued� en el hash y se me fue en collera la exp
>> regular.
[...]
> $string =~ s/([\$,\@,\#,\!,\",\%,\',\(,\),\*,\+,\,,\-,\.,\/,\:,\;,\<,\=,\>,\?])/$hash{$1}/g ;
>
> Con eso, para cada caracter que este en la lista y que encuentre en
> $string, buscara su equivalente en $hash y lo reemplazara.
Aplicando lo que sale en `perldoc -q base':
$string =~ s/(.)/sprintf("%X",ord($1))/eg;
O bien (mas eficiente pero quizas menos digerible):
$string = unpack("H*", pack("A*", $string));
Saludines,
--
Guti
When in doubt, parenthesize. At the very least it will let some poor
schmuck bounce on the % key in vi. -- Larry Wall in the perl man page
Technical Details
Message-ID:
<87lko6atbx.fsf@guti.cl>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!nntp-server.pubsub.com!news.nivel5.cl!news.cec.uchile.cl!not-for-mailReferences:
<efbvjj$3g4$1@localhost.localdomain> <efc7ho$f8u$1@localhost.localdomain>Show Full Headers
Complete Article Headers
X-Google-Thread: 1593cb,5bb18c6b72accfca X-Google-Attributes: gid1593cb,public X-Google-Language: SPANISH,ASCII Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!nntp-server.pubsub.com!news.nivel5.cl!news.cec.uchile.cl!not-for-mail From: Cristian Gutierrez <crgutier@dcc.uchile.cl> Newsgroups: chile.comp.programacion Subject: Re: Reemplazar caracteres por hexadecimal en Perl Date: Tue, 26 Sep 2006 23:20:34 -0400 Organization: DCC/Uchile Lines: 29 Message-ID: <87lko6atbx.fsf@guti.cl> References: <efbvjj$3g4$1@localhost.localdomain> <efc7ho$f8u$1@localhost.localdomain> NNTP-Posting-Host: vpn-client-039.cec.uchile.cl Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: localhost.localdomain 1159327235 9718 172.17.35.39 (27 Sep 2006 03:20:35 GMT) X-Complaints-To: news@localhost.localdomain NNTP-Posting-Date: Wed, 27 Sep 2006 03:20:35 +0000 (UTC) User-Agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:W+QBeu4kwhFyoDCsk+WneasvQT8=