网页功能: 加入收藏 设为首页 网站搜索  
字符串中文的问题
发表日期:2003-07-21作者:[] 出处:  

字串中文的问题,起於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

我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 字符串中文的问题
本类热点文章
  在VB中使用WMI获取系统硬件和软件有关信..
  在VB中使用WMI获取系统硬件和软件有关信..
  将繁体中文字转化成简体中文
  将繁体中文字转化成简体中文
  获取CPU的ID
  获取CPU的ID
  在vb程序中如何获取剪贴板中所复制的文..
  在vb程序中如何获取剪贴板中所复制的文..
  VB编程破解Windows屏幕保护密码
  更新桌面图片
  更新桌面图片
  让程序的鼠标支持滚轮
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2024 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00402