标题: 字串中文的问题
ljjk5
元帅
Rank: 1


荣誉会员奖章
UID 46706
精华 1
积分 99426
帖子 49690
威望 554
金币 48489
热心 505
阅读权限 100
注册 2007-2-25
状态 离线
字串中文的问题

字串中文的问题,起於vb的字串是使用UniCode,而我们一般是使用Ascii Code。这差别在何处呢?UniCode的每个字元长度是2个byte,而Ascii是一个byte,如果说,我将们将VB的字串写入档案,有时会有意想不到的结果。例如:
    Text1.Text = "这是一个abc"
    len5 = Len(str5)
    如果我们的Access资料库有一栏位的长度是10个Byte,所以我们在TextBox中设定MaxLength = 10,但是上面的例子得到的len5是7,而不是我们认为的11,因为不管是中文或英文,vb一律以UniCode来存,所以str5的长度是7个"字元",而text1最大的长度限制是10,7没有超过10,故使用者仍可输入,但存档时,11个byte超过10个byte,所以会有错。    可是或许有人发现,使用RS232来传资料时,另一端主机是Ascii编码的机器,在vb中我们若使用String来传,一样可以通啊,其实那是vb在传送与接收data时,会做转换,使我们的程式设计较方便,但如果传的资料是Binary时,就头大啦。例如说,以字串的方式来传送资料,当想传Ascii 大於128时,常有些问题,因为ASC(Chr(129))=0,使我们不能用Chr()的指令来放资料。(事实上,您可以使用ChrW(129)来存资料,和使用AscW()来取得值,加个W代表是Word的运算),这时候,就只有使用Byte Array来做了。
1.UniCode转成ByteAry
  Dim byteAry() As Byte
  Dim str5 As String
  Dim i As Long
  str5 = "这abc"
  byteAry = str5
  For i = LBound(byteAry) To UBound(byteAry)
      Debug.Print byteAry(i)   '得 25 144 97 0 98 0 99 0
  Next i
  Debug.Print Len(str5), LenB(str5) '得4  8
  所以了,可看出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)   '得 25 144 97 98 99
  Next i
  Debug.Print LenB(StrConv(str5, vbFromUnicode)) '得5
2.ByteAry转回UniCode 使用Strconv()转换
  Dim byteAry(10) as Byte
  Dim Str5 as String
  byteAry(0) = 25
  byteAry(1) = 144
  byteAry(2) = 97
  byteAry(3) = 98
  byteAry(4) = 99
  Str5 = StrConv(byteAry, vbUniCode)

3.一些有用的函式
SubStr()    中文化取子字串,相对Mid()
Strlen()    中文化字串长度,相对Len()
StrLeft()   中文化取左字串,相对Left()
StrRight()  中文化取右字串,相对Right()
isChinese() Check某个字是否中文字
Public Function SubStr(ByVal tstr As String, start As Integer, Optional leng As Variant) As String
Dim tmpstr  As String
If IsMissing(leng) Then
   tmpstr = StrConv(MidB(StrConv(tstr, vbFromUnicode), start), vbUnicode)
Else
   tmpstr = StrConv(MidB(StrConv(tstr, vbFromUnicode), start, leng), vbUnicode)
End If
SubStr = tmpstr
End Function
Public Function Strlen(ByVal tstr As String) As Integer
   Strlen = LenB(StrConv(tstr, vbFromUnicode))
End Function
Public Function StrLeft(ByVal str5 As String, ByVal len5 As Long) As String
Dim tmpstr As String
tmpstr = StrConv(str5, vbFromUnicode)
tmpstr = LeftB(tmpstr, len5)
StrLeft = StrConv(tmpstr, vbUnicode)
End Function
Public Function StrRight(ByVal str5 As String, ByVal len5 As Long) As String
Dim tmpstr As String
tmpstr = StrConv(str5, vbFromUnicode)
tmpstr = RightB(tmpstr, len5)
StrLeft = StrConv(tmpstr, vbUnicode)
End Function
Public Function isChinese(ByVal asciiv As Integer) As Boolean
   If Len(Hex$(asciiv)) > 2 Then
      isChinese = True
   Else
      isChinese = False
   End If
End Function

网友 ljjk5 签名 - 网友社区 ===
顶部
[广告] 免费域名(Free Subdomain) 免费空间(Free hosting) PR查询(Google Pagerank)



当前时区 GMT+8, 现在时间是 2008-10-13 08:21
信产部ICP备案:京ICP备05066424号 北京市公安局网监备案:1101050648号

Powered by Discuz! 5.5.0
清除 Cookies - 联系我们 - 网友俱乐部 - Archiver - WAP