Re: Strings and Unicode in VBA

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Thanks Karl - yes, it is basically doing the conversion twice.

Thanks for the help!

Richard


Karl E. Peterson wrote:

Hi Richard --

My question is: why the additional zeroes from the second procedure?
Assigning a string to a byte array fills the byte array with the
unicode characters, and I thought this was essentially what the
StrConv function did (with vbUnicode option).

Calling StrConv (with vbUnicode) on a string that's *already* Unicode,
you're telling it to consider every byte to be an ANSI character code, so
it's changing the "normal" 00 bytes to 00 00. Make sense?

Later... Karl
--
Working without a .NET?
http://classicvb.org/



richardschollar@xxxxxxxxxxx wrote:
Apologies for a dual post (in Excel.Programming too) but not getting
any response there:

Given the following two code snippets, how come I get different
output?


Code:
Sub test()
Dim str As String
Dim b() As Byte
str = "hello"
b() = str
str = ""
For i = LBound(b) To UBound(b)
str = str & b(i) & " "
Next i
Debug.Print str
End Sub


The above produces the following output:


Code:
104 0 101 0 108 0 108 0 111 0


second procedure:


Code:
Sub test2()
Dim str As String
Dim b() As Byte
str = "hello"
b() = StrConv(str, vbUnicode)
str = ""
For i = LBound(b) To UBound(b)
str = str & b(i) & " "
Next i
Debug.Print str
End Sub


Which produces this output:


Code:
104 0 0 0 101 0 0 0 108 0 0 0 108 0 0 0 111 0 0 0


My question is: why the additional zeroes from the second procedure?
Assigning a string to a byte array fills the byte array with the
unicode characters, and I thought this was essentially what the
StrConv

function did (with vbUnicode option).


Can anyone settle my curiosity?


Thanks for any/all replies!


Best regards


Richard

.



Relevant Pages

  • Re: Strings and Unicode in VBA
    ... Assigning a string to a byte array fills the byte array with the ... Dim str As String ...
    (microsoft.public.office.developer.vba)
  • Re: Strings and Unicode in VBA
    ... Assigning a string to a byte array fills the byte array with the ... Calling StrConv on a string that's *already* Unicode, ... Dim str As String ...
    (microsoft.public.office.developer.vba)
  • Re: How to Return an array of strings in C (char**)
    ... int start=0; ... // to know the number of elements in the array ... The reason is that you fail to increment str. ... The you could have printed "here" before entering the main loop to see if you actually got there, then str + start to see where you go to in the string on each pass. ...
    (comp.lang.c)
  • Re: map and join or inject?
    ... It has to do with the block you gave inject() and the definition of String.+. ... concatenated to str. ... The keywords there being "new String." ... And if we use that, we can get down to less objects than map, because we don't need the Array: ...
    (comp.lang.ruby)
  • <string>: Handling of "" and copy into char x[];
    ... I am currently using a string to hold data (can be strings as well as ... copy the bytes "as they are" into the array and back? ... Tnx a lot in advance. ... Karl ...
    (comp.lang.cpp)