以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]关于字符串转GB2132编码的方法!  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=71277)

--  作者:lhpc120
--  发布时间:2015/7/8 15:31:00
--  [求助]关于字符串转GB2132编码的方法!

Function ToUTF8(sStr As String)
Dim DAT() As String, DAT1() As Byte, ST As String
Dim Z As String
Dim zTem As String
Dim zCod As String
Dim zHex As String
Dim zBin As String
Dim zRes As Long
Dim zAsc As Long
Dim L  As Long   \'字节计数
Dim i1 As Long
Dim i2 As Integer
For i1 = 1 To Len(sStr)
        Z = Mid(sStr, i1, 1)
        zAsc = Asc(Z)
        If zAsc > 0 Then           \'如果不是汉字
          ReDim Preserve DAT(L + 1) As String
          DAT(L) = zAsc
          L = L + 1
        Else
          ReDim Preserve DAT(L + 3) As String
          DAT1 = Z
          ST = Right("0 " & Hex(DAT1(1)), 2) & Right("0 " & Hex(DAT1(0)), 2)
          For i2 = 1 To Len(ST)
             Z = Mid(ST, i2, 1)
             Select Case Z       \'将汉字的16进制Unicode编码转换为二进制的
               Case Is = "0":         zBin = zBin & "0000"
               Case Is = "1":         zBin = zBin & "0001"
               Case Is = "2":         zBin = zBin & "0010"
               Case Is = "3":         zBin = zBin & "0011"
               Case Is = "4":         zBin = zBin & "0100"
               Case Is = "5":         zBin = zBin & "0101"
               Case Is = "6":         zBin = zBin & "0110"
               Case Is = "7":         zBin = zBin & "0111"
               Case Is = "8":         zBin = zBin & "1000"
               Case Is = "9":         zBin = zBin & "1001"
               Case Is = "A":         zBin = zBin & "1010"
               Case Is = "B":         zBin = zBin & "1011"
               Case Is = "C":         zBin = zBin & "1100"
               Case Is = "D":         zBin = zBin & "1101"
               Case Is = "E":         zBin = zBin & "1110"
               Case Is = "F":         zBin = zBin & "1111"
               End Select
            Next
            zBin = Trim(zBin)
            zTem = "1110" & Left(zBin, 4) & "10" & Mid(zBin, 5, 6) & "10" & Right(zBin, 6)
            \'MsgBox zBin & "|" & Left(Trim(zBin), 4)
            \'MsgBox zTem
            For i2 = 1 To 24
               Z = Mid(zTem, i2, 1)
               zRes = zRes + IIf(Z = "1", 1 * 2 ^ (24 - i2), 0 * 2 ^ (24 - i2))
            Next
            Z = Hex(zRes)       \'汉字已转换为3字节的16进制数据
            DAT(L) = Left(Z, 2)
            DAT(L + 1) = Mid(Z, 3, 2)
            DAT(L + 2) = Right(Z, 2)
            L = L + 3
        End If
        zBin = " ":       zRes = 0
Next
For i = 0 To UBound(DAT) - 1
  ToUTF8 = ToUTF8 & "%" & DAT(i)
Next
End Function

 

 

上面借助了VBA的方法!不知道在狐表里面怎么实现呢?


--  作者:大红袍
--  发布时间:2015/7/8 15:36:00
--  
 直接说你想做什么。
--  作者:大红袍
--  发布时间:2015/7/8 15:40:00
--  

编码转换?

 

Dim file As String = "文件.txt"
Dim bytes() As Byte
Dim utf As Encoding = Encoding.GetEncoding("utf-8")  
Dim def As Encoding = Encoding.Default
Bytes = def.GetBytes(file)
Bytes = Encoding.Convert(def, utf, Bytes)  
file = def.GetString(Bytes)  
Output.Show(file)

 

----------------------

 

Dim file As String = "鏂囦欢.txt"
Dim bytes() As Byte
Dim utf As Encoding = Encoding.GetEncoding("utf-8")  
Dim def As Encoding = Encoding.Default
Bytes = utf.GetBytes(file)
Bytes = Encoding.Convert(utf, def, Bytes)
file = utf.GetString(Bytes)  
Output.Show(file)


 

http://msdn.microsoft.com/zh-cn/library/system.text.encoding%28v=vs.80%29.aspx

[此贴子已经被作者于2015/7/8 15:40:30编辑过]

--  作者:lhpc120
--  发布时间:2015/7/8 15:51:00
--  

太复杂了,直接这样就可以吧!

 

Output.Show((Hex(Asc(StrToWide("你")))))


--  作者:大红袍
--  发布时间:2015/7/8 16:03:00
--  

 呃...

 

Dim file As String = "文是3订单"
Dim bytes() As Byte
Dim def As Encoding = Encoding.Default
Bytes = def.GetBytes(file)
For Each b As Byte In Bytes
    output.show(b)
Next


--  作者:lhpc120
--  发布时间:2015/7/8 16:12:00
--  

你发的是什么意思?

转换为:Bytes类型?


--  作者:大红袍
--  发布时间:2015/7/8 16:13:00
--  
 我也不知道你要的是什么。
--  作者:lhpc120
--  发布时间:2015/7/8 16:15:00
--  

全角字符的“我”转换为 十进制的 GB2312编码为52946  转换为16进制为CED2  ,你给我的是什么?

 

我想达到的目的是 ,把 字符 “我”转换为   CED2 并转换为10进制的ASCLL编码


--  作者:大红袍
--  发布时间:2015/7/8 16:18:00
--  
Dim file As String = "我"
Dim bytes() As Byte
Dim def As Encoding = Encoding.Default
Bytes = def.GetBytes(file)
For Each b As Byte In Bytes
    output.show(b)
Next

--  作者:lhpc120
--  发布时间:2015/7/8 16:28:00
--  

字符转GB2312的十进制或者十六进制按照上面的公式转换的数不对啊。

 

http://www.qqxiuzi.cn/bianma/zifuji.php

 

请参考上面网址转换值.

 

比如将字符  我 转换为GB2312的十六进制为CED2,10进制为:52946

 

如果我用狐表的命令Output.Show((Hex(Asc(StrToWide("我")))))

转换出来的为:FFFFCED2   然后我去掉前面的4个F

再次转换为ASCLL编码传送,传送到COM口则是:67 69 68 50

 

请问老师,有没有更捷径的转换方式呢?