in reply to Re^2: Character in 'b' format wrapped in unpack
in thread Character in 'b' format wrapped in unpack
In other words, when you want to do low-level, C-like bit twiddling, just use pack and unpack - that's what those are for - and give up on pretending that higher-level, linguistically oriented functions (chr and ord) can do the same thing.
I agree how sad it is that every user must pay the performance cost of unicode support, whether or not they actually need or use it. But then, it's also sad that every script must pay the overhead for untyped variables, no matter how much of that flexibility is actually needed or used.
UPDATE: Having said that, I realize I'm probably still deficient in my understanding of your particular example. You said you "wanted the shift to discard the high bit, as it does with integers", and if I'm not mistaken (am I?), that's actually what happens, with or without the "use bytes" pragma (i.e. with or without the warning). Here's a simpler example - am I missing something?
When I run that, I get:#use bytes; $x = "\xAA"; print unpack 'B*', $x; print " --> "; $x = chr( ord( $x ) << 1 ); print unpack 'B*', $x; print "\n---\n"; $x = pack( "C*", 0xAA ); print unpack 'B*', $x; print " --> "; $x = pack( "C", unpack( "C", $x ) << 1 ); print unpack 'B*', $x; print "\n---\n";
(Note the warning from using the "C" format on pack.) Looks to me like the high bit got shifted off in both cases - no difference. When I uncomment the "use bytes", the only difference I see is that the "B" format warning goes away (but the "C" format warning still shows up.) Is there a problem I'm not seeing?Character in 'B' format wrapped in unpack at /tmp/j1.pl line 7. 10101010 --> 01010100 --- Character in 'C' format wrapped in pack at /tmp/j1.pl line 14. 10101010 --> 01010100 ---
In case it matters, I'm using perl 5.18 on macosx 10.10.2 ("yoesemite").
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Character in 'b' format wrapped in unpack
by BrowserUk (Patriarch) on Mar 29, 2015 at 20:49 UTC | |
by choroba (Cardinal) on Mar 29, 2015 at 22:23 UTC | |
by BrowserUk (Patriarch) on Mar 29, 2015 at 23:04 UTC | |
by ikegami (Patriarch) on Mar 29, 2015 at 23:24 UTC | |
by ikegami (Patriarch) on Mar 29, 2015 at 23:07 UTC | |
by BrowserUk (Patriarch) on Mar 30, 2015 at 00:24 UTC |