Contributed by TGI
on Oct 18, 2000 at 22:46 UTC
It seems that chr() and pack("C",hex($1)) do the same thing, namely convert a number into a character (from it's ASCII value). Is there an advantage to using one over the other? I've seen the pack approach used in several scripts, but never the chr form? Is something wrong with using chr?
|Answer: What's the difference between chr() and pack('C', hex($1))?|
contributed by Fastolfe
There is a subtle difference that may bite you. chr works on characters while pack "C" works on bytes. With a standard ASCII character set, these are indistinguishable under most circumstances, but if you're using unicode (see perlunicode) data in later versions of Perl, chr and ord behave differently than their pack/unpack counterparts. pack/unpack will still work with individual bytes while chr/ord will actually deal with the full unicode character, which may be longer than 1 byte.
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||