[源码]UnicodeTOGB,能够将Unicode串转换成GB码,方便开发。

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox

 1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
 2Me.TextBox1 = New System.Windows.Forms.TextBox   
 3Me.TextBox2 = New System.Windows.Forms.TextBox   
 4Me.SuspendLayout()   
 5'   
 6'TextBox1   
 7'   
 8Me.TextBox1.Location = New System.Drawing.Point(7, 7)   
 9Me.TextBox1.Multiline = True   
10Me.TextBox1.Name = "TextBox1"   
11Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical   
12Me.TextBox1.Size = New System.Drawing.Size(241, 121)   
13Me.TextBox1.TabIndex = 0   
14Me.TextBox1.Text = ""   
15'   
16'TextBox2   
17'   
18Me.TextBox2.Location = New System.Drawing.Point(8, 136)   
19Me.TextBox2.Multiline = True   
20Me.TextBox2.Name = "TextBox2"   
21Me.TextBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical   
22Me.TextBox2.Size = New System.Drawing.Size(241, 112)   
23Me.TextBox2.TabIndex = 1   
24Me.TextBox2.Text = ""   
25'   
26'Form1   
27'   
28Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)   
29Me.ClientSize = New System.Drawing.Size(256, 255)   
30Me.Controls.Add(Me.TextBox2)   
31Me.Controls.Add(Me.TextBox1)   
32Me.Name = "Form1"   
33Me.Text = "UnicodeToGB"   
34Me.ResumeLayout(False) 
35
36End Sub 
37
38#End Region 
39
40Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged 
41
42End Sub 
43
44Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown   
45If e.KeyCode = Keys.Escape Then   
46TextBox2.Text = UnicodeToGB(TextBox1.Text)   
47End If   
48If e.KeyCode = Keys.F1 Then   
49TextBox1.Text = ""   
50TextBox2.Text = ""   
51End If   
52End Sub 
53
54Private Function UnicodeToGB(ByVal strUnicode As String) As String   
55Dim GBCode As String   
56Dim i, j As Integer   
57Dim c() As String '临时变量   
58ReDim c(strUnicode.Length / 4) '2个字节一个中文 
59
60For j = 0 To strUnicode.Length \ 4 - 1   
61Dim d() As Char = strUnicode.ToCharArray(j * 4, 4)   
62c(j) = "&amp;H" &amp; CType(d, String)   
63c(j) = ChrW(Val(c(j)))   
64GBCode += c(j) '将最后的结果返回   
65Next   
66Return GBCode   
67End Function 
68
69Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
70
71End Sub   
72End Class</system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus