wtuqi
3/7/2020 - 8:51 AM

编码

  //写入时指定编码
  StreamWriter sw = new StreamWriter(@"C:\Users\jyzdz\1.txt", false, Encoding.GetEncoding("gb2312"));
  //转换编码报错处理[ "?" = 少取半个汉字显示(1字节)]
 viewmsg.Text = System.Text.Encoding.Default.GetString(outBytes, qd, bytes - 1) + "?";
 
//把GB2312编码转成汉字 
string s = Convert.ToString(47524, 2); // 转成二进制 
byte byte1 = Convert.ToByte(s.Substring(0, 8), 2); 
byte byte2 = Convert.ToByte(s.Substring(8), 2); 
byte[] array = new byte[2] { byte1, byte2 }; 
MessageBox.Show(System.Text.Encoding.GetEncoding("GB2312").GetString(array)); 


//汉字获取 GB2312 编码 
byte[] array = System.Text.Encoding.GetEncoding("GB2312").GetBytes("工"); 
string s1 = Convert.ToString(array[0], 16); 
string s2 = Convert.ToString(array[1], 16); 
int i = Convert.ToInt32(s1 + s2, 16); 
MessageBox.Show(i.ToString());
'字符串转换成二进制
function StrToBin(str)
    dim curChr, curAsc, low, high
    dim i
    for i=1 To Len(str)
        curChr = Mid(str, i, 1)
        curAsc = Asc(curChr)
        'asc对中文字符求出来的值可能为负数,
        '加上65536就可求出它的无符号数值
        '-1在机器内是用补码表示的0xffff,
        '其无符号值为65535,65535=-1+65536
        '其他负数依次类推。
        if curAsc < 0 then
            curAsc = curAsc + 65535
        end if
        '对中文的处理:把双字节低位和高位分开
        if curAsc > 255 then
            low = Left(Hex(Asc(curChr)), 2)
            high = Right(Hex(Asc(curChr)), 2)
            StrToBin = StrToBin & ChrB("&H" & low) & ChrB("&H" & high)
        else
            StrToBin = StrToBin & ChrB(AscB(CurChr))
        end If
    next
end function

'二进制转换成字符串
function BinToStr(binStr)
    if IsNull(binStr) then
        BinToStr = ""
        exit function
    end if
    
    dim newStr, chnFlag
    dim i, c
    newStr = ""
    chnFlag = true
    for i=1 To LenB(binStr)
        if chnFlag then
            c = MidB(BinStr, i, 1)
            if AscB(c) > 127 then
                'AscW 会把二进制的中文双字节字符高位和低位反转
                '所以 MidB(binStr,i+1,1)&c 表达式中,c 在后面
                newStr = newStr & Chr(AscW(MidB(binStr,i+1,1)&c))
                chnFlag = false
            else
                newStr = newStr & Chr(AscB(c)) '这里用的是 ASCB 不是 ASCW
            end If
        else
            chnFlag = true
        end If
    next
    
    BinToStr = newStr
end function 
'发送的内容转为utf8
Public Function UTF8EncodeURI(szInput)
    Dim wch, uch, szRet
    Dim X
    Dim nAsc, nAsc2, nAsc3
    If szInput = "" Then
        UTF8EncodeURI = szInput
        Exit Function
    End If
    
    For X = 1 To Len(szInput)
      wch = Mid(szInput, X, 1)
      nAsc = AscW(wch)
      If nAsc < 0 Then nAsc = nAsc + 65536
          If (nAsc And &HFF80) = 0 Then
              szRet = szRet & wch
          Else
              If (nAsc And &HF000) = 0 Then
                      uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)
                      szRet = szRet & uch
              Else
                      uch = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & "%" & _
                      Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & "%" & _
                      Hex(nAsc And &H3F Or &H80)
                      szRet = szRet & uch
              End If
      End If
    Next
End Function
'gbk转中文 如:"http://%B1%BB" 转后:"http://被"
Public Function urlGbkDecode(ByRef strURL As String) As String
    Dim I As Long
    If InStr(strURL, "%") = 0 Then URLDecode = strURL: Exit Function
    For I = 1 To Len(strURL)
        If Mid(strURL, I, 1) = "%" Then
            If Val("&H" & Mid(strURL, I + 1, 2)) > 127 Then
                URLDecode = URLDecode & Chr(Val("&H" & Mid(strURL, I + 1, 2) & Mid(strURL, I + 4, 2)))
                I = I + 5
            Else
                URLDecode = URLDecode & Chr(Val("&H" & Mid(strURL, I + 1, 2)))
                I = I + 2
            End If
        Else
            URLDecode = URLDecode & Mid(strURL, I, 1)
        End If
    Next
End Function
'中文转gbk 如:"http://被" 转后:"http://%B1%BB"
Function urlGbkEncode(nstr As String) As String
    Dim I As Integer, nmid As String, nAsc As Integer, nhex As String
    For I = 1 To Len(nstr)
        nmid = Mid(nstr, I, 1)
        nAsc = Asc(nmid)
        If nAsc < 0 Then
            nhex = Right("000" & Hex(nAsc), 4)
            URLEncodeGbk = URLEncodeGbk & "%" & Left(nhex, 2) & "%" & Right(nhex, 2)
        ElseIf nmid = " " Then
            URLEncodeGbk = URLEncodeGbk & "+"
        ElseIf (nAsc >= 48 And nAsc <= 57) Or (nAsc >= 65 And nAsc <= 90) Or (nAsc >= 97 And nAsc <= 122) Then
            URLEncodeGbk = URLEncodeGbk & nmid
        Else
            URLEncodeGbk = URLEncodeGbk & "%" & Right("0" & Hex(nAsc), 2)
        End If
    Next I
End Function
'vb将unicode转成汉字,如:\u8033\u9EA6,转后为:耳麦
Public Function urlUnicodeDecode(strCode As String) As String
    Dim Char As String, arr
    strCode = Replace(strCode, "U", "u")
    arr = Split(strCode, "\u")
    For i = 0 To UBound(arr)
        If Len(arr(i)) > 0 Then
            If Len(arr(i)) = 4 Then '//长度是4刚好是一个字
                Char = Char & ChrW("&H" & Mid(CStr(arr(i)), 1, 4))
            ElseIf Len(arr(i)) > 4 Then '//长度>4说明有其它字符
                Char = Char & ChrW("&H" & Mid(CStr(arr(i)), 1, 4)) & Mid(CStr(arr(i)), 5)
            End If
        End If
    Next
    unicodeDecode = Char
End Function
'将中文转为unicode编码,如:耳麦,转后为:\u8033\u9EA6
Function urlUnicodeEncode(strCode As String) As String
    Dim a() As String
    Dim str As String
    Dim i As Integer
    StrTemp = strCode
    
    For i = 0 To Len(strCode) - 1
    On Error Resume Next
        str = Mid(strCode, i + 1, 1)
        If isChinese(str) = True Then '//是中文
            unicodeEncode = unicodeEncode & "\u" & String(4 - Len(Hex(AscW(str))), "0") & Hex(AscW(str))
        Else '//不是中文
            unicodeEncode = unicodeEncode & str
        End If
    Next
End Function
 
'//是否为中文
Private Function isChinese(Text As String) As Boolean
    Dim l As Long
    Dim i As Long
    l = Len(Text)
    isChinese = False
    For i = 1 To l
        If Asc(Mid(Text, i, 1)) < 0 Or Asc(Mid(Text, i, 1)) < 0 Then
        isChinese = True
        Exit Function
        End If
    Next
End Function