length of string

One Chinese character should be 2-byte. But if I use Len(cStr), it get the length equals 1. How can I get the byte numbers of a Chinese string?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Dim bytLen() As Byte

bytLen = YourChineseString

LenInBytes = UBound(bytLen) - LBound(bytLen)
0
Commented:
Use LenB()

works in exactly the same way as Len() only it returns the byte length rather than the character length.

All the other string manipulation functions also have byte level equivelants

ie
MidB
LeftB,
RightB

see help for more details on these

Hope this helps

Gordon
0
Author Commented:
My code is as below :

Dim bytLen() As Byte
bytLen = MyString
LenInBytes = UBound(bytLen) - LBound(bytLen)

When I first put MyString="test1",
the LenInBytes=9.
second, MyStrin=2 Chinese charactors,
the LenInBytes=3.

If I use LenB() instead of Len(),
every character(chinese or english) will be len=2.
0
Commented:
Ah, pardon.

It should have been
LenInBytes = UBound(bytLen) - LBound(bytLen) + 1

I forgot about LenB... Of course that's easier, thanks GordonP for reminding me.

Apart from that, strings in VB internally are held as unicode, so even English characters occupy two bytes...
0
Author Commented:
Now every single chinese character and english character become lenght 2.
What I need is for one chinese character legth=2, and one english character length=1.

thanks.
0
Commented:
Unfortunately due to the way VB handles strings, its not possible as far as I know.

VB uses Unicode to handle strings which means every character is stored as 2 bytes.

Sorry cant help more

Gordon
0
Commented:
Well,

What you could do is investigate the characters one at a time.

In Unicode, the English characters will not use the second byte:

Private Function StringLength(ByVal strTest As String) As Long

Dim bytTest() As Byte
Dim lngCounter As Long
Dim lngTotal As Long

bytTest = strTest

For lngCounter = LBound(bytTest) To UBound(bytTest) Step 2
If bytTest(lngCounter + 1) = 0 Then
lngTotal = lngTotal + 1
Else
lngTotal = lngTotal + 2
End If
Next

StringLength = lngTotal

End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
That's just like what I am doing now,
I just want know if there are any more efficiency  way to solve this problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.