Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Obtaining length of binary string

Reply
Thread Tools

Obtaining length of binary string

 
 
Perl User
Guest
Posts: n/a
 
      12-03-2004
Hi,
Here's my problem:

I read encrypted data from a server and save it in a variable. Now, I need
to send this back to the server along with a number that tells how many
bytes long the binary data is.

I've tried
$x = read_data_from_server($args);
$y = length $x;
send_data_to_server($x,$y);

I am not sure if the length function is the right way to count the number
of bytes in the string $x. Can someone please show me how to do this?

Thanks a lot!
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      12-03-2004

Quoth Perl User <>:
> Hi,
> Here's my problem:
>
> I read encrypted data from a server and save it in a variable. Now, I need
> to send this back to the server along with a number that tells how many
> bytes long the binary data is.
>
> I've tried
> $x = read_data_from_server($args);
> $y = length $x;
> send_data_to_server($x,$y);
>
> I am not sure if the length function is the right way to count the number
> of bytes in the string $x. Can someone please show me how to do this?


It is, provided you've told perl that your data is binary not textual.
Make sure you use binmode on the socket filehandle.

Ben

--
If you put all the prophets, | You'd have so much more reason
Mystics and saints | Than ever was born
In one room together, | Out of all of the conflicts of time.
The Levellers, 'Believers'
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      12-04-2004


Perl User wrote:

> I read encrypted data from a server and save it in a variable. Now, I
> need to send this back to the server along with a number that tells how
> many bytes long the binary data is.
>
> I've tried
> $x = read_data_from_server($args);
> $y = length $x;
> send_data_to_server($x,$y);
>
> I am not sure if the length function is the right way to count the
> number of bytes in the string $x.


It is right since you say it's a binary string. If it were a text
string then length() would return the number of characters.

If you want to force length() to count bytes even in text strings:

use bytes;

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      12-04-2004
Perl User wrote:

> I am not sure if the length function is the right way to count the
> number of bytes in the string $x.


The length() function returns the number of characters in the string.
If you're not using Unicode, the number of bytes is the same as
the number of characters.
-Joe
 
Reply With Quote
 
Shawn Corey
Guest
Posts: n/a
 
      12-04-2004
Joe Smith wrote:
> The length() function returns the number of characters in the string.
> If you're not using Unicode, the number of bytes is the same as
> the number of characters.
> -Joe


If you are using Perl 5.8+ and the string is is_utf8 (see perldoc
Encode) the length returns the number of characters, not the number of
bytes.

--- Shawn

#!/usr/bin/perl

use strict;
use warnings;

my $s = "\x{2022}"; # Unicode for a bullet character

print length($s), "\n";

__END__
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      12-04-2004
Joe Smith wrote:
> Perl User wrote:
>
>> I am not sure if the length function is the right way to count the
>> number of bytes in the string $x.

>
> The length() function returns the number of characters in the string.
> If you're not using Unicode, the number of bytes is the same as
> the number of characters.


That's wrong. Any MBCS or DBCS uses more than one byte for a single
character.

jue


 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      12-04-2004
Jürgen Exner wrote:
> Joe Smith wrote:
>
>>Perl User wrote:
>>
>>
>>>I am not sure if the length function is the right way to count the
>>>number of bytes in the string $x.

>>
>>The length() function returns the number of characters in the string.
>>If you're not using Unicode, the number of bytes is the same as
>>the number of characters.

>
>
> That's wrong. Any MBCS or DBCS uses more than one byte for a single
> character.


I was not aware of any non-Unicode version of perl that does
multibyte character sets.
-Joe
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      12-04-2004
Joe Smith wrote:
> Jürgen Exner wrote:
>> Joe Smith wrote:
>>> Perl User wrote:
>>>> I am not sure if the length function is the right way to count the
>>>> number of bytes in the string $x.
>>>
>>> The length() function returns the number of characters in the
>>> string. If you're not using Unicode, the number of bytes is the
>>> same as the number of characters.

>>
>>
>> That's wrong. Any MBCS or DBCS uses more than one byte for a single
>> character.

>
> I was not aware of any non-Unicode version of perl that does
> multibyte character sets.


I am not sure what a "unicode version of perl" would be, but if the OP sends
lets say Japanese characters in Windows-932 back to the server, then he is
using a DBCS where each character is two bytes long and which has nothing to
do with Unicode.

jue


 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      12-04-2004
On Sat, 4 Dec 2004, Jürgen Exner wrote:

> Joe Smith wrote:
> > Jürgen Exner wrote:
> >>
> >> That's wrong. Any MBCS or DBCS uses more than one byte for a single
> >> character.

> >
> > I was not aware of any non-Unicode version of perl that does
> > multibyte character sets.

>
> I am not sure what a "unicode version of perl" would be,


Presumably one of the recent versions which have native Unicode
support... (5.6 or later, whatever)

> but if the OP sends lets say Japanese characters in Windows-932 back
> to the server, then he is using a DBCS where each character is two
> bytes long and which has nothing to do with Unicode.


But then Perl itself has no concept of "character" in such a piece
of data, and cannot meaningfully be asked to count "characters".

It has to be up to the programmer to count their own characters, if
the only definition of "characters" is some specification external to
Perl.

Or else they use an encode layer, or explicit encoding function, to
convert the external character encoding into native Perl (unicode)
characters, and use Perl's own functions on the result.

Doesn't that seem reasonable?
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      12-04-2004
Alan J. Flavell wrote:
[Very reasonable arguments snipped]

> Doesn't that seem reasonable?


Absolutely.

However it misses the point. Joe wrote:
>>If you're not using Unicode, the number of bytes is the same as
>>the number of characters.


And this is the statement I still don't agree with.

jue


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Convert integer to fixed length binary string Ulrich Eckhardt Python 1 06-12-2009 02:03 AM
Re: Convert integer to fixed length binary string Mark Dickinson Python 0 06-11-2009 10:01 AM
String exceeding length - Getting absolute string length james.w.appleby@gmail.com Java 5 01-11-2007 12:07 AM
left(string, length) or right(string, length)? Sam ASP .Net 3 02-17-2005 12:01 PM
How to get length of string? length() problems Mitchua Perl 5 07-17-2003 12:08 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57