请问vb里面有直接把byte数组直接转换成字符串的方法吗

haohaoxuexi 2004-05-06 05:54:09
如题
...全文
274 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
haohaoxuexi 2004-05-09
  • 打赏
  • 举报
回复
感谢大家
结帖了,分数给前两位
KiteGirl 2004-05-06
  • 打赏
  • 举报
回复
越界?

Dim tBytes() As Byte
tBytes()="小仙妹是个好孩子"

你看看还会吗?
online 2004-05-06
  • 打赏
  • 举报
回复
Public Function ByteArrayToString(bytArray() As Byte) As String
Dim sAns As String
Dim iPos As String

sAns = StrConv(bytArray, vbUnicode)
iPos = InStr(sAns, Chr(0))
If iPos > 0 Then sAns = Left(sAns, iPos - 1)

ByteArrayToString = sAns

End Function
broown 2004-05-06
  • 打赏
  • 举报
回复
up
haohaoxuexi 2004-05-06
  • 打赏
  • 举报
回复
tBytes()这样写会报数组越界啊
KiteGirl 2004-05-06
  • 打赏
  • 举报
回复
在VB里,Byte数组和String可以互相赋值,利用这个特性处理字符串非常方便。

如果是处理大量文本,以下用法应该熟悉。

字符串与Byte数组互相赋值:
tBytes()=tString
tString=tBytes()

需要注意的是,在VB内部字符串是UniCode表示的,而Asc函数返回的是Ascii和GBK编码(也就是默认代码页的编码)。因此,Asc函数返回的编码值与上面的Byte数组返回的值可能是不同的。

从Ascii/GBK文本获得Unicode数组:
tBytes()=StrConv(tString,vbUnicode)

从Unicode文本获得Ascii/GBK数组:
tBytes()=StrConv(tString,vbFormUnicode)

从Ascii/GBK数组获得Unicode文本:
tString=StrConv(tBytes(),vbUnicode)

从Unicode数组获得Ascii/GBK文本:
tString=StrConv(tBytes(),vbFormUnicode)

快速读取文本文件:

'将文本文件整个读到Byte数组:
Dim tBytes() As Byte
tFN=FreeFile
Open pFileName For Binary As #tFN
tFileSize=LOF(#tFN)
ReDim tBytes(tFileSize-1)
Get #tFN,1,tBytes()
Close #tFN
tString=StrConv(tBytes(),vbUnicode) '从文件获得数组转换为带换行的多行文本字符串。

反之,如果将字符串文本写为二进制文件,则要:
Dim tBytes() As Byte
tBytes()=StrConv(tString,vbFormUnicode)

tFN=FreeFile
Open pFileName For Output As #tFN '清除文件内容
Close #tFN
Open pFileName For Binary As #tFN
Put #tFN,1,tBytes()
Close #tFN

另外,还可以用API函数CopyMemory将Byte数组转换为Integer数组,在利用查表法进行内码转换、简繁转换时,该操作尤为重要。因为,在VB当中,每个“字”的编码都是16位UniCode,用Integer数组表示最为直观。但Integer数组不能直接与String赋值,因此需要一个与Byte数组转换的过程。

另外,你可以参考我在下面这个帖子里的回复:
http://expert.csdn.net/Expert/TopicView1.asp?id=2939892

在这个帖子里,我贴有一个GBK转Big5的代码,其中提供了一些比较实用的函数。

boywang 2004-05-06
  • 打赏
  • 举报
回复
ansi
s=byte
unicode
s=strconv(string,vbunicode)

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧