程序代码片段:
    Dim source(3) As Byte
    Dim  target as string
    source(0) = 97
    source(1) = 98
    source(2) = 99
    source(3) = 100
   如何让 target的值成为 “abcd” 而不是乱码“扡摣”
  代码在多线程中运行所以不能使用strconv
  考虑到效率不要循环遍历的方法
本人在使用MultiByteToWideChar 和CopyMemory进行测试没有成功,有木有高手施展一下?

解决方案 »

  1.   

    Dim source(8) As Byte
    Dim target As String
    source(1) = 0
    source(0) = 97
    source(3) = 0
    source(2) = 98
    source(5) = 0
    source(4) = 99
    source(7) = 0
    source(6) = 100
    s = source遍历了
    其实我是来签到的
      

  2.   


    Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
    Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As LongFunction ConvToUnicode(iStr() As Byte) As String
    Const CP_ACP = 0&
    Dim iLen As Long
    iLen = lstrlen(VarPtr(iStr(0))) + 2&
    ConvToUnicode = String$(iLen, 0)
    iLen = MultiByteToWideChar(CP_ACP, 0&, VarPtr(iStr(0)), -1&, StrPtr(ConvToUnicode), iLen)
    ConvToUnicode = Left$(ConvToUnicode, iLen - 1&)
    End FunctionPrivate Sub Command1_Click()
    Dim source(3) As Byte
    Dim target As String
    source(0) = 97
    source(1) = 98
    source(2) = 99
    source(3) = 100
    target = ConvToUnicode(source)
    Print target
    End Sub
      

  3.   

    收藏了
    不过运行有乱码
    abcd?
    abcd?
    abcd?
    abcd?把Dim source(3) As Byte改成Dim source(4) As Byte就没有乱码了
      

  4.   

    有可能会有乱码,原因是lstrlen正常应该返回一个以\0结束的字符串的长度。