Hello Monks,
Can anyone tell me why the following code:
#!/usr/bin/perl
#$test = "\x{05D0}\x{20AC}";
#$test = "\xd7\x90\xe2\x82\xac";
#$test = "\x05\xD0\x20\xAC";
$test = '®';
#$test = "\x{c2ae}";
$unpacked = unpack('H*',$test);
print("string: $test\n");
print("unpacked: $unpacked\n");
print("char length: " . length($test) . "\n");
print("byte length: " . getByteLength($test) . "\n");
sub getByteLength
{
my ($string) = @_;
use Encode qw(encode decode);
$binaryString = encode('UTF-8', $string);
my $byteLength = length($binaryString);
return $byteLength;
}
produces the following output:
string: ®
unpacked: c2ae
char length: 2
byte length: 4
Why is the char-length (which should be 1) getting the byte-length (which should be 2) and why is the byte-length reporting 4 bytes, when upacked it is clearly 2 bytes?
Which one of these things is not functioning correctly?
On a related note, what is the difference between:
$test = "\x{05D0}\x{20AC}";
and
$test = "\x05\xD0\x20\xAC"
It seems that the second instance is putting in the actual hex bytes, because the following code produces the same output as the first thing above (with the squiggly brackets):
$test = "\xd7\x90\xe2\x82\xac";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
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:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.