954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Unicode and Strings

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

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:

104 0 101 0 108 0 108 0 111 0


second procedure:

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:

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! Richard

RichardSchollar
Newbie Poster
2 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 
Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Comatose - many thanks for the links. The last one was exceptionally helpful. That has sorted out a problem I was seriously confused by. Thanks again!Richard

RichardSchollar
Newbie Poster
2 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

You're Welcome!

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You