GB2312是GBK編碼的子集,使用GBK編碼函數(shù)即可
成都創(chuàng)新互聯(lián)公司溝通電話:028-86922220,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),成都創(chuàng)新互聯(lián)公司網(wǎng)頁制作領(lǐng)域10多年,包括成都衛(wèi)生間隔斷等多個(gè)行業(yè)擁有多年建站經(jīng)驗(yàn),選擇成都創(chuàng)新互聯(lián)公司,為企業(yè)錦上添花。
'GBK編碼(結(jié)果以百分號(hào)%進(jìn)行分隔)
Public Function GBKEncode(ByVal sInput As String) As String
Dim ret_GBKEncode As String = ""
Dim i As Integer
Dim startIndex As Integer = 0
Dim endIndex As Integer
Dim x() As Byte = System.Text.Encoding.Default.GetBytes(sInput) '字符以及字符串在vb2008中都是以u(píng)nicode編碼存儲(chǔ)的
endIndex = x.Length - 1
For i = startIndex To endIndex
ret_GBKEncode = "%" Hex(x(i))
Next
Return ret_GBKEncode
End Function
'GBK解碼
Public Function GBKDecode(ByVal sInput As String) As String
sInput = sInput.Replace("%", "")
Dim ret_GBKDecode As String = ""
Dim sLen As Integer = sInput.Length
Dim n As Integer = sLen \ 2
Dim sBytes(0 To n - 1) As Byte
'轉(zhuǎn)化為字節(jié)碼
For i As Integer = 1 To n
sBytes(i - 1) = CByte("H" sInput.Substring(2 * i - 2, 2))
Next
'將字節(jié)碼轉(zhuǎn)化為字符串
ret_GBKDecode = System.Text.Encoding.Default.GetString(sBytes)
Return ret_GBKDecode
End Function
Imports System.Text
Public Function StringAsUtf8Bytes(ByVal strData As String) As Byte()
Dim bytes() As Byte
bytes = Encoding.UTF8.GetBytes(strData)
Return bytes
End Function
說明:strData參數(shù)是GB2312字符串,函數(shù)返回UTF8字節(jié)數(shù)組
沒有指定字符集,根據(jù)頁面的設(shè)置,可以設(shè)置字符集為UTF-8或GB2312
文本框 TextBox1
上一條 Button1
下一條 Button2
修改 Button3
Imports?System.IO
Public?Class?Form1
Dim?currIndex?As?Integer?=?0
Dim?s2?As?List(Of?String)?=?New?List(Of?String)
Dim?s?As?String
Private?Sub?Form1_Load(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?MyBase.Load
s?=?File.ReadAllText(Application.StartupPath??"\txt.txt")
Dim?s1?As?String()?=?s.Split("@")
For?i?As?Integer?=?0?To?s1.Length?-?1
If?s1(i).Trim()??""?Then
s2.Add("@"??s1(i))
End?If
Next
TextBox1.Text?=?s2(0)
End?Sub
Private?Sub?Button2_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button2.Click
currIndex?=?currIndex?+?1
If?currIndex??s2.Count?-?1?Then
MsgBox("已經(jīng)是最后條了")
Return
End?If
TextBox1.Text?=?s2(currIndex)
End?Sub
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
currIndex?=?currIndex?-?1
If?currIndex??0?Then
MsgBox("已經(jīng)是第一條了")
Return
End?If
TextBox1.Text?=?s2(currIndex)
End?Sub
Private?Sub?Button3_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button3.Click
s2(currIndex)?=?TextBox1.Text
s?=?""
For?i?As?Integer?=?0?To?s2.Count?-?1
s?=?s2(i)
Next
File.WriteAllText(Application.StartupPath??"\txt.txt",?s)
End?Sub
End?Class
字符編碼轉(zhuǎn)換嗎?
1.字符與gb2312(gbk的子集):
Public Function GBKEncode(ByVal sInput As String) As String
Dim ret_GBKEncode As String = ""
Dim i As Integer
Dim startIndex As Integer = 0
Dim endIndex As Integer
Dim x() As Byte = System.Text.Encoding.Default.GetBytes(sInput) '字符以及字符串在vb2008中都是以u(píng)nicode編碼存儲(chǔ)的
endIndex = x.Length - 1
For i = startIndex To endIndex
ret_GBKEncode = "%" Hex(x(i))
Next
Return ret_GBKEncode
End Function
'GBK解碼
Public Function GBKDecode(ByVal sInput As String) As String
sInput = sInput.Replace("%", "")
Dim ret_GBKDecode As String = ""
Dim sLen As Integer = sInput.Length
Dim n As Integer = sLen \ 2
Dim sBytes(0 To n - 1) As Byte
'轉(zhuǎn)化為字節(jié)碼
For i As Integer = 1 To n
sBytes(i - 1) = CByte("H" sInput.Substring(2 * i - 2, 2))
Next
'將字節(jié)碼轉(zhuǎn)化為字符串
ret_GBKDecode = System.Text.Encoding.Default.GetString(sBytes)
Return ret_GBKDecode
End Function
2.Unicode字符串為UTF-8
Imports System.Text
Public Function StringAsUtf8Bytes(ByVal strData As String) As Byte()
Dim bytes() As Byte
bytes = Encoding.UTF8.GetBytes(strData)
Return bytes
End Function
'這里可以類推出好幾種。