新建From1(窗体),Command1(按钮CommandButton),代码:
Private Sub Command1_Click()
Dim str_Input As String
str_Input = "码农库ma"
Dim strAnsi As String
Dim strUnicode As String
strUnicode = "码农库ma"
strAnsi = StrConv(strUnicode, vbFromUnicode)
str_Input = str_Input & Chr(13)
str_Input = str_Input & "Unicode:" & str(Len(strUnicode)) & Chr(13)
str_Input = str_Input & "Ansi:" & str(LenB(strAnsi))
Debug.Print str_Input
End Sub
运行结果:
码农库ma
Unicode: 5
Ansi: 8
关于编码可以看下《ASCII ANSI Unicode UTF-8 UTF-16等字符编码的区别》
新建From1(窗体),新建Command1和2(按钮CommandButton),代码:
Dim byteAry() As Byte
Dim str5 As String
Private Sub Command1_Click()
Dim i As Long
str5 = "码abc"
byteAry = str5
For i = LBound(byteAry) To UBound(byteAry)
Debug.Print byteAry(i) '得 1 120 97 0 98 0 99 0
Next i
Debug.Print Len(str5), LenB(str5) '得4 8
End Sub
Private Sub Command2_Click()
'所以了,可看出UniCode 的特性,程式应改一下,使用Strconv()来转换 Dim byteAry() As Byte
Dim str5 As String
Dim i As Long
str5 = "码abc"
byteAry = StrConv(str5, vbFromUnicode)
For i = LBound(byteAry) To UBound(byteAry)
Debug.Print byteAry(i) '得 194 235 97 98 99
Next i
Debug.Print LenB(StrConv(str5, vbFromUnicode)) '得5
End Sub