Problem writing byte values to a file

G

Gregory K. Deal

I'm trying to write out bytes to a file with values anywhere from 0 to
255. I assign a value to a variable and write it out with a "%c" format.
Something like :

$r=1; $g=128; $b=1;
printf("%c%c%c",$r,$g,$b);

The problem is when the byte values exceed 127. When less than 128, I get
expected values (writing either a 1 or a 127) :


0000000 P 6 \n 3 3 \n 2 5 5 \n 001 177 001 001 001
0000020 177 001 001 177 \n 001 177 001 001 001 177 001 001 177 \n 001
0000040 177 001 001 001 177 001 001 177 \n

When I try to write a 128 (or larger), it seems to want to write the value
out in 2 bytes instead of 1 :

0000000 P 6 \n 3 3 \n 2 5 5 \n 001 302 200 001 001
0000020 001 302 200 001 001 302 200 \n 001 302 200 001 001 001 302 200
0000040 001 001 302 200 \n 001 302 200 001 001 001 302 200 001 001 302
0000060 200 \n

Here, a "302 200" seems to show up where the "177" used to be, when I
really only expected a plain "200". So, any idea what's going on here? Am
I jumping into or enabling Unicode mode? I tried "use bytes" to turn off
this behavior, but no luck. Trying the following didn't help either :

$rc=chr($r); $gc=chr($g); $bc=chr($b);
print "${rc}${gc}${bc}";

How can I predictably write out bytes to a file? Thanks for any help or
guidance.
 
J

John Stanley

When I try to write a 128 (or larger), it seems to want to write the value
out in 2 bytes instead of 1 :

Ain't UNICODE wonderful?

"binmode" is no longer a DOS-ism.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top