sAddress="广东珠海建业5路108号"1,由于一个汉字在TXT文件中占两个字节,所以我希望求出Length的长度为20,而用LEN函数求出的是12。如何做?
2,一个汉字是由两个十六进制的编码组成,怎样得到汉字的十六进制编码?

解决方案 »

  1.   

    for i=1 to len(sAddress)
        if asc(mid(saddress,i,1))<0 then 
            slen=slen+2
        else
            slen=slen+1
        end if
    next i
      

  2.   

    Function GetCharCount(sIn As String, lWideByteCnt As Long, lNarrowByteCnt As Long) As Long
        On Error GoTo ErrorHandler
        Dim lTotalByteCnt As Long
        GetCharCount = False
        
        lTotalByteCnt = LenB(StrConv(sIn, vbFromUnicode))
        lNarrowByteCnt = LenB(sIn) - lTotalByteCnt
        lWideByteCnt = (lTotalByteCnt - lNarrowByteCnt) / 2
        
        GetCharCount = lTotalByteCnt
        Exit Function
    ErrorHandler:
    End Function
      

  3.   

    GetCharCount函数返回的是宽字符加窄字符的个数
    lWideByteCnt返回的是宽字符的个数,lNarrowByteCnt返回的是窄字符的个数sAddress="广东珠海建业5路108号"
    i=GetCharCount(sAddress,i1,i2)得到:i=20,i1=8,i2=4
      

  4.   

    字符串的ASCII码实际上只是第一个字符的ASCII。倘若想区分字符串中某字符是中文还是英文,方法有很多。

      ch = Mid(strString, I, 1)  可以判断其长度:
      If LenB(StrConv(ch, vbFromUnicode)) = 2 Then
         中文字符
      Else
         英文字符
      End If  还可以使用Asscii码来判断
      If Abs(Asc(ch)) > 256 Then
         中文字符
      Else
         英文字符
      End If
         
      

  5.   

    LenB(StrConv("广东珠海建业5路108号", vbFromUnicode))
      

  6.   

    Dim B() As Byte
    Dim I As Integer
    Dim HexCode As String 
    B = StrConv("广东珠海建业5路108号", vbFromUnicode)
    HexCode = Empty
    For I = 0 To Ubound(B)
        HexCode = HexCode & Right("0" & Hex(B(I)), 2)
    Next