kvclarkusa has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a perl interface to a proprietary network protocol. I need to be able to take a string and write this to the output stream as a one byte character string. Based on my reading, I am pretty confident I can do this via pack() but I have not been able to figure out the proper characters to pass in the TEMPLATE argument. For example, I want to do something like this:
$my_value = "ABCDEF"; # the string value of what I need to send $my_value = pack ('some_TEMPLATE',$my_value); # transform it into a 1 +byte value for the network server print $socket $my_value; # send it to the server
The second line's 'some_TEMPLATE' is where I am running into problems - I have not figured out what pack characters should be used there. (For what it's worth, the equivalent java method that does what I need is DataOutputStream.writeBytes().) Any help would be greatly appreciated... Ken

Replies are listed 'Best First'.
Re: packing a string into binary
by dws (Chancellor) on Dec 11, 2001 at 09:26 UTC
    I am writing a perl interface to a proprietary network protocol. I need to be able to take a string and write this to the output stream as a one byte character string.

    I trust you realize the futility of trying to pack more than 8 bits of data into 8 bits.

    Your example shows a 6 character string. By what unstated magic do you expect that pack will be able to reduce that string to 1 byte without data loss? Even if those 6 characters represent a hex string, it will reduce to 3 bytes.

      My mistake... After reading your response, I realized I misread one of the components of the specification and didn't really have an issue at all... time to take a break... Humbly,