Dim str1 as string
Dim str2 as stringstr1="12345678"
str2="987ABCD2"
要求str1的每一个字节减去0x30然后与str2对应的字节做异或,这个语法应该怎么写啊,初次用vb,呵呵

解决方案 »

  1.   

    这个容易……
    Dim tStringA As String
    Dim tStringB As String
    Dim tStringC As StringDim tBytesA() As Byte
    Dim tBytesB() As Byte
    Dim tBytesC() As ByteDim tBytesIndex As Long
    Dim tBytesLength As LongtStringA = "12345678"
    tStringB = "987ABCD2"tBytesA() = StrConv(tStringA, vbFromUnicode)
    tBytesB() = StrConv(tStringB, vbFromUnicode)tBytesLength = UBound(tBytesA())ReDim tBytesC(tBytesLength)For tBytesIndex = 0 To tBytesLength
      tBytesC(tBytesIndex) = (tBytesA(tBytesIndex) - &H30) Xor tBytesB(tBytesIndex)
    NexttStringC = StrConv(tBytesC(), vbUnicode)
      

  2.   

    tBytesA() = StrConv(tStringA, vbFromUnicode)
    -----------------
    长知识了 StrConv处理字符串的重点
      

  3.   

    UP  顺便1. 将半角字符转换为全角字符
       最简单了, 直接用StrConv函数吧:
       str=str="hello! 这是一个中文字符串!"
       debug.print StrConv(str,vbWide)2. 求一个字符串所占的字节数
       debug.print lenb(StrConv("hello! 这是一个中文字符串!",vbFromUnicode))
       ’LenB: 返回以字节数为单位的字符串长度
       ’先将字符串转换为ANSI/DBCS格式,然后求长度3. 求一个字符串中的汉字数目
       str="hello! 这是一个中文字符串!"
       debug.print lenb(StrConv(str,vbFromUnicode))-len(str)
       len(str): 英文和中文长度都是1
       lenb(StrConv(str,vbFromUnicode)): 英文是1,中文是24. 将一个带有中文字符的字符串转换为byte数组:
       dim arr() as byte
       dim strUnicode as string
       dim strAnsi as string   str="hello! 这是一个中文字符串!"
       ’Unicode: 英文和中文字符都占用2个字节
       strAnsi=StrConv(strUnicode,vbFromUnicode)   
       ’转换为ANSI/DBCS格式, 英文占一个字节, 中文占两个字节
       arr=strAnsi5. 将一个Byte数组转换为字符串:
       用 StrConv(arr, vbUnicode)进行转换, 其中arr是一个Byte数组.