vb6.0项目升级到vb.net(LenB(StrConv(str,type)))

上一篇 / 下一篇  2012-06-16 10:22:43 / 个人分类:技术杂谈--随笔录

在项目由vb6.0升级到vb2005时遇到LenB()函数,在vb2005中此函数不存在,所以找替代的方法的研究,仅供参考如有不正确请指出

系统:windowsxp x86
开发环境:vb6.0  vs2005

vb2005和vb6.0中字符串默认采用的编码方式为uncoide

vb2005中没有替代vbFormUncoide的类型

VB6.0:
Dim str as String
Dim bytesCount as Integer
str="hello"
'字母占一个字节,汉字和日文占两个字节
bytesCount=LenB(StrConv(str,vbFormUncoide)) 'StrConv(str,vbFormUncoide)将字符串的编码方式转换为系统缺省码页

结果为:bytesCount=5

VB2005中替代的方法
Dim str as String
Dim bytesCount as Integer
str="hello"
bytesCount=System.Text.Encoding.Default.GetByteCount(str) ’字符串的编码方式为系统缺省编码

结果:butesCount=5


VB6.0和vb2005:
  Len()函数计算字节的方式为字符(英文、汉字、日文)占一个字节

VB6.0:
 
Dim str as String
Dim bytesCount as Integer
str="hello"
'vbUncoide将字符编码转换为uncoide编码
bytesCount=LenB(StrConv(str,vbUncoide)) 'LenB(str)=10
'uncoide编码每个字符占两个字节
结果为:bytesCount=20


如果将str字符编码改为vbFormUncoide之后再运行上面的程序结果为10
Dim str as String
Dim bytesCount as Integer
str="hello"
str=StrConv(str,vbFormUncoide)
'StrConv(str,vbFormUncoide)将字符串的编码方式转换为系统缺省码页
'每个字符占两个字节计算
bytesCount=LenB(StrConv(str,vbUncoide))  'LenB(str)=10
结果为:bytesCount=10


Dim str as String
Dim bytesCount as Integer
str="hello"
str=StrConv(str,vbFormUncoide)
str=StrConv(str,vbFormUncoide)

bytesCount=LenB(StrConv(str,vbUncoide)) 'LenB(str)=4
结果为:bytesCount=4


vb2005:
System.Text.Encoding.Uncoide.GetByteCount(str) '每个字符占两个字节

TAG:

 

评分:0

我来说两句

龙飞

龙飞

追随自己的梦想,永不言弃。

日历

« 2024-04-30  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 71945
  • 日志数: 62
  • 文件数: 1
  • 书签数: 4
  • 建立时间: 2011-01-27
  • 更新时间: 2012-06-16

RSS订阅

Open Toolbar