A
Alexander Farber
Hi,
I have a small card game. The clients are Java-applets and the
server is written in C, mostly forwarding data from applet to applet.
The message format is:
1 byte: Number of unicode chars (s. below)
2 byte: Player number
3 byte: Event id
up to 510 bytes: A Java unicode string
Now I'm trying to rewrite my C-server to perl, because that way
it's easier to add features (syslog, auth against an SQL-db, etc.)
I have problems to understand what would be the best pack-format for
my messages. I have read "perldoc -f pack" numerous times and also
the many O'Reilly books I have, but the best I've come up with is
pack "C3(xC)*", length $ascii_str, $num, $id, unpack "C*",
$ascii_str;
for the cases, when I need to send an ASCII string (like an IP address
string) from the server to the Java-applet and thus have to stuff the
upper bytes of that ASCII with zeros (that's why the "x" above).
I wonder, why doesn't pack "C3U*" do the same? Here is a demo:
# perl -e '$str=pack "C3(xC)*", 4, 0, 14, unpack "C*", "test"; \
print join " ", unpack "C*", $str'
4 0 14 0 116 0 101 0 115 0 116
# perl -e '$str=pack "C3U*", 4, 0, 14, unpack "C*", "test"; \
print join " ", unpack "C*", $str'
4 0 14 116 101 115 116
As you see, the stuffing zeros are missing in the second output.
But why? Doesn't "perldoc -f pack" say
If you don't want this [UTF8] to happen, you can
begin your pattern with "C0" (or anything else) to force
Perl not to UTF8 encode your string, and then follow
this with a "U*" somewhere in your pattern.
Regards
Alex
PS: Also I wonder, if there are any nicer ways to communicate
Java-strings to Perl. "perldoc -f pack" mentions "n/..."
for Java-Strings, but doesn't elaborate. Is it "n/U*" ?
I have a small card game. The clients are Java-applets and the
server is written in C, mostly forwarding data from applet to applet.
The message format is:
1 byte: Number of unicode chars (s. below)
2 byte: Player number
3 byte: Event id
up to 510 bytes: A Java unicode string
Now I'm trying to rewrite my C-server to perl, because that way
it's easier to add features (syslog, auth against an SQL-db, etc.)
I have problems to understand what would be the best pack-format for
my messages. I have read "perldoc -f pack" numerous times and also
the many O'Reilly books I have, but the best I've come up with is
pack "C3(xC)*", length $ascii_str, $num, $id, unpack "C*",
$ascii_str;
for the cases, when I need to send an ASCII string (like an IP address
string) from the server to the Java-applet and thus have to stuff the
upper bytes of that ASCII with zeros (that's why the "x" above).
I wonder, why doesn't pack "C3U*" do the same? Here is a demo:
# perl -e '$str=pack "C3(xC)*", 4, 0, 14, unpack "C*", "test"; \
print join " ", unpack "C*", $str'
4 0 14 0 116 0 101 0 115 0 116
# perl -e '$str=pack "C3U*", 4, 0, 14, unpack "C*", "test"; \
print join " ", unpack "C*", $str'
4 0 14 116 101 115 116
As you see, the stuffing zeros are missing in the second output.
But why? Doesn't "perldoc -f pack" say
If you don't want this [UTF8] to happen, you can
begin your pattern with "C0" (or anything else) to force
Perl not to UTF8 encode your string, and then follow
this with a "U*" somewhere in your pattern.
Regards
Alex
PS: Also I wonder, if there are any nicer ways to communicate
Java-strings to Perl. "perldoc -f pack" mentions "n/..."
for Java-Strings, but doesn't elaborate. Is it "n/U*" ?