I have a hexadecimal string representation of a Unicode codepoint:
my $unicode_character_hexadecimal_string = '0x20ac';

It would be easier/better to leave off the quotes -- this way, you don't need to use eval later on:

my $unicode_hex_codepoint = 0x20ac;
From here, I want to get to some hexadecimal string representation of the UTF-8 encoding of this Unicode codepoint:
'0xE2 0x82 0xAC'

I don't understand why you would want to do that. Normally, you want to go directly to a perl-internal utf8 character:

my $unicode_char = chr( $unicode_hex_codepoint );
Then, as strange as it seems, I want to get to an analagous hexadecimal string representation of the same sequence of bytes with the most significant bit turned off (i.e, with 0x80 substracted):
'0x62 0x02 0x2C'

You're right. That does seem very strange. I'll be losing sleep trying to imagine what sort of purpose this could possibly serve. In any case, if your ultimate goal is a print-out that looks something like this (I'm just guessing about the format):


  €  ==  20ac  == e2 82 ac == 11100010 10000010 10101100 ^^ 62 02 2c == 01100010 00000010 00101100

Then something like this, perhaps:
use strict; my $uni_hex = 0x20ac; my $uni_chr = chr($uni_hex); my ( $u8_byts, $strpd_byts ); $u8_byts .= sprintf( "%02x ", $_) for ( unpack( "C*", $uni_chr )); $strpd_byts .= sprintf( "%02x ", $_ & 0x7f ) for ( unpack( "C*", $uni_ +chr )); ( my $u8_bits = unpack( "B*", $uni_chr )) =~ s/(.{8})/$1 /g; ( my $strpd_bits = $u8_bits ) =~ s/\b1/0/g; printf( "%s == %04x == %s == %s ^^ %s == %s\n", $uni_chr, $uni_hex, $u8_byts, $u8_bits, $strpd_byts, $strpd_bi +ts );
There are other ways to do it, which might be more suitable, depending on why you really want to do this (and what sorts of data you'll be dealing with).

In reply to Re: Need Help With Seemingly Bizarre Unicode Task by graff
in thread Need Help With Seemingly Bizarre Unicode Task by Jim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.