473,246 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

size of binary string on multi-byte system?


hello!

normally, if you are given binary data in a string (such as from a call to
fread), you can call the strlen function on this string to get its size:

$buffer = fread($file, 100000);
if (strlen($buffer) < 100000)
{
echo "read less than 100000 bytes";
}
etc

now, the problem is, on systems where you have the mbstring extension
turned on, and are using function substitution to replace strlen with
mb_strlen, you are pretty much guaranteed to get a WRONG value back from
strlen, since it will find some multi-byte lead characters in that binary
file eventually.

so the question is:

are there any other methods to find the length of a string besides strlen?
the only way i can see to not screw myself right now is to not use function
substitution. any other options?

thanks,
mark.
--
I am not an ANGRY man. Remove the rage from my email to reply.
Jul 17 '05 #1
3 6013
Mark <mw@angrylanfear.com> wrote:
so the question is:

are there any other methods to find the length of a string besides strlen?
the only way i can see to not screw myself right now is to not use function
substitution. any other options?


I'd guess that "mb_strlen($str,'8bit')" would give you the actual
number of bytes in the string.

Checking.... http://nl3.php.net/mb_strlen the only comment:
If you wish to find the byte length of a multi-byte string when you are
using mbstring.func_overload 2 and UTF-8 strings, then you can use the
following:

mb_strlen($utf8_string, 'latin1');

latin1 is also a 8bit encoding, only there are ranges not defined:
0x7fz<0xa0

8bit sounds better IMHO.

Jul 17 '05 #2

"Mark" <mw@ANGRYLanfear.com> wrote in message
news:rp********************@nventure.com...

hello!

normally, if you are given binary data in a string (such as from a call to fread), you can call the strlen function on this string to get its size:

$buffer = fread($file, 100000);
if (strlen($buffer) < 100000)
{
echo "read less than 100000 bytes";
}
etc

now, the problem is, on systems where you have the mbstring extension
turned on, and are using function substitution to replace strlen with
mb_strlen, you are pretty much guaranteed to get a WRONG value back from
strlen, since it will find some multi-byte lead characters in that binary
file eventually.

so the question is:

are there any other methods to find the length of a string besides strlen? the only way i can see to not screw myself right now is to not use function substitution. any other options?

thanks,
mark.


array_sum(count_chars($s)), maybe?
Jul 17 '05 #3
Chung Leong wrote:
are there any other methods to find the length of a string besides

strlen?
the only way i can see to not screw myself right now is to not use

function
substitution. any other options?

thanks,
mark.


array_sum(count_chars($s)), maybe?

the best solution so far appears to be mb_strlen($buf, '8bit')

thanks to all.
mark.

--
I am not an ANGRY man. Remove the rage from my email to reply.
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
12
by: Bill | last post by:
Using A2000 I am using mini-databases residing on floppy disks for writing school reports which can then be sent to a central db for electronic collating. Occasionally the database has grown past...
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
3
by: yxq | last post by:
Hello, I use the com component "shockwave flash object" to play flash file, but how to get the actual size (width*height) of flash file but no control size? thank you!
4
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my...
5
by: Noel Mosa | last post by:
Hi, i have an incoming object (file) on a istream. i need to pass it to another (C programmed!)module in a char* array and providing its size(also needing the size to mallocate the char* array)....
7
by: carterweb | last post by:
This is how I do it now. 1. Determine the dimensions of the rectangle. 2. Set a my font size to a fixed maximum size. 3. Apply the font my string and measure the string using the graphics...
8
by: Allan Ebdrup | last post by:
What would be the fastest way to search 18,000 strings of an average size of 10Kb, I can have all the strings in memory, should I simply do a instr on all of the strings? Or is there a faster way?...
29
by: Harlin Seritt | last post by:
Hi... I would like to take a string like 'supercalifragilisticexpialidocius' and write it to a file in binary forms -- this way a user cannot read the string in case they were try to open in...
30
by: ggnguser | last post by:
It's part of a test and I'm stumped. There is a function void foo(char **x) The function signature is given and cannot be changed, so no passing of other values. The test case involves...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.