Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

finding length of encrypted(binary) string

by linuxfan (Beadle)
on Dec 03, 2004 at 22:19 UTC ( #412300=perlquestion: print w/replies, xml ) Need Help??

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

Hi, My perl script reads an encrypted string from a server. It then calculates the length of this string and sends this number along with the string back to the server. I am using:
$str = read_str_from_server($args);
$len = length $str;
send_str_to_server($str,$len);

Based on the server response, I think that the above method fails for certain values of $str and $len. Is there an alternate function to count the nubmer of "bytes" in the encrypted string?

Thanks!

  • Comment on finding length of encrypted(binary) string

Replies are listed 'Best First'.
Re: finding length of encrypted(binary) string
by bart (Canon) on Dec 03, 2004 at 22:49 UTC
    You can enforce the string to be treated as pure bytes, by pushing it through an incantation of pack. Then, length will work.
    my $bytes_string = pack 'C0a*', $string; my $bytecount = length $bytes_string;
Re: finding length of encrypted(binary) string
by BrowserUk (Patriarch) on Dec 03, 2004 at 22:29 UTC

    Sounds like your binary data is being mistaken for unicode data. Using the bytes pragma, optionally localised around the call to length should fix the problem.

    $s = "\x{200}\x{400}\x{301}"; print length $s; 3 print do{ use bytes; length $s }; 6

    Examine what is said, not who speaks.
    "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      Thanks for your reply. I realized I was not using the

      use bytes pragma

      in a different place in the program while extracting the encrypted text, and this was causing my program to fail. My program works like a charm now!

Re: finding length of encrypted(binary) string
by Zed_Lopez (Chaplain) on Dec 03, 2004 at 22:23 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://412300]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2023-09-25 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?