Or chr(), as in
perl -le 'print chr(1234&0xff).chr(1234>>8)'
| [reply] [d/l] [select] |
I think the chr(1234>>8) is superfluous.
--
John.
| [reply] |
What do you mean by "superfluous"? It's the closest analogue to writing out bytes from C, assuming you're on a little-endian machine.
But the question was ambiguous about the desired outcome. Writing out the 'int' value 1234 might mean (1) writing either a little- or big-endian 0x4d2, (2) writing a host- or network-order 0x4d2, or (3) writing out the unicode codepoint 1234. I picked interpretation #1. #2 should be done with pack(), but since the sample C code didn't include htonl() or a variant, it apparently wasn't desired. #3 should be done with either pack() or a plain chr(1234).
Except I'm still wrong, because I was really treating the 'int' as a 'short' and writing out only 16 bits. To match the C code, which wrote a 32-bit value, that should be chr(1234 & 0xff).chr(1234>>8).chr(0).chr(0)
The distinction seemed unimportant for the simple goal of mentioning chr()!
| [reply] [d/l] [select] |