当前位置:编程学习 > VB >>

【求助】怎么样用VB获取文本内容的16进制字符串?

例如,有个UTF-8编码的txt文件 内容是 “中国人”,用ue32打开,他的16进制码是 EF BB BF E4 B8 AD E5 9B E4 BA BA

我现在要获取 "EFBBBFE4B8ADE59BE4BABA"

用VB怎么写啊,从open开始,求助

本帖最后由 bcrun 于 2012-01-29 21:00:30 编辑          结果是“EFBBBFE4B8ADE59BBDE4BABA”
而不是你贴上来的“EFBBBFE4B8ADE59BE4BABA”。

Private Sub Command1_Click()
    Open "c:\1.txt" For Binary As #1
    Dim x() As Byte
    x = InputB(LOF(1), 1)
    For i = 0 To UBound(x)
      Debug.Print Hex(x(i));
    Next
End Sub



EFBBBFE4B8ADE59BBDE4BABA

将字符串复制到一个未定义大小的字节数组,然后通过hex函数可以直接从文字得到,而不需要必须存为文件,然后再读回内存。
Dim s() as byte
Dim strs as string
dim sout as string 
strs="你会好的"
s=strs
for i=lbound(s) to ubound(s)
sout=sout & hex(s(i))
next i
debug.Print sout
测试代码:
Text1.Text = HEXGetByBytes(BytesGetByFile("TestUTF.txt", 1, 11))


Private Function BytesGetByFile(ByRef pFileName As String, ByRef pStart As Long, ByRef pLength As Long) As Byte()
  'BytesGetByFile函数
  '格式:   [tOutBytes()] = BytesGetByFile(pFileName, pStart, pLength)
  '功能:   从文件指定位置获取指定数量的字节数组
  '参数:   string  pFileName   '路径及文件名
  '         long    pStart      '数组读取位置(最小为1)
  '         long    pLength     '数组读取长度(这个长度是从0开始的,是实际数量-1。读取10个字节则应该是9)
  '返回:   byte    tOutBytes() '返回字节数组
  
  Dim tOutBytes() As Byte
  Dim tOutBytes_Length As Long
  Dim tFileNumber As Integer
  Dim tFreeByte As Long
  
  tFileNumber = FreeFile
  
  Open pFileName For Binary As tFileNumber
    tFreeByte = LOF(tFileNumber) - pStart
    
    '如果pStart > 0并且tFreeByte >= 0则tOutBytes_Length是有效的值,否则为0。
    '如果tFreeByte < pLength则tOutBytes_Length = tFreeByte
    '如果tFreeByte >= pLength则tOutBytes_Length = pLength
    tOutBytes_Length = ((pStart > 0) And (tFreeByte >= 0)) And ((tFreeByte < pLength) And tFreeByte) Or ((tFreeByte >= pLength) And pLength)
    
    ReDim tOutBytes(tOutBytes_Length)
    
    Get tFileNumber, pStart, tOutBytes()
  
  Close tFileNumber
  
  BytesGetByFile = tOutBytes()
End Function

Private Function HEXGetByBytes(ByRef pBytes() As Byte) As Byte()
  'HEXGetByBytes函数
  '格式:   [tOutHEX] = HEXGetByBytes(pBytes())
  '功能:   从字节数组获取16进制字符串。
  '参数:   byte    pBytes()    '字节数组
  '返回:   string  tOutHEX     '16进制字符串(也可以是字节数组)
  
  Dim tOutHEX() As Byte
  Dim tOutHEX_Length As Long
  Dim tOutHEX_Index As Long
  Dim tBytes_Length As Long
  Dim tBytes_Index As Long
  
  tBytes_Length = UBound(pBytes())
  tOutHEX_Length = tBytes_Length * 4 - 1
  
  ReDim tOutHEX(tOutHEX_Length)
  
  For tOutHEX_Index = 0 To tOutHEX_Length Step 4
    '高位编码
    tOutHEX(tOutHEX_Index) = HexEnCode(pBytes(tBytes_Index) \ &H10)
    '低位编码
    tOutHEX(tOutHEX_Index + 2) = HexEnCode(pBytes(tBytes_Index) Mod &H10)
    '源字节地址步进
    tBytes_Index = tBytes_Index + 1
  Next
  
  HEXGetByBytes = tOutHEX()
End Function

Private Function HexEnCode(pValue As Byte) As Byte
  'HexEnCode函数
  '格式:   [tOutValue] = HexEnCode(pValue)
  '功能:   将实际的不大于16的正整数转换为Ascii对应的16进制编码。
  '参数:   byte    pValue      '16进制位值
  '返回:   byte    tOutValue   '16进制字符串编码
  HexEnCode = pValue + ((pValue > 9) And 7) + 48
End Function

补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,