用.NET获取汉字的区位码

  计算机中最底层的数据都是用二进制及0和1来表示的。每个0或1称作1位,第8位二进制数叫做1个字节,它可以表示ASCII码中的一个字符。中文计算机中用两个字节即16位二进制来表示一个汉字。而在Unicode编码中所有的符号(包括汉字,英文,标题及其它众多符号)都是为两字节(16)位来表示。

  在System.Text命名空间中包含众多编码的类,可供进行操作及转换,下面用两个实例来进行区位码及汉字之间的互换,希望能起到举一反三的效果,让大家可以轻松处理文字编码方面的问题:


  

Imports System.Text
Class CodingChange
	Public Function CharacterToCoding(character As String) As String
		Dim coding As String = ""
		For i As Integer = 0 To character.Length - 1
			Dim bytes As Byte() = System.Text.Encoding.Unicode.GetBytes(character.Substring(i, 1))
			'取出二进制编码内容
			Dim lowCode As String = System.Convert.ToString(bytes(0), 16)
			'取出低字节编码内容(两位16进制)
			If lowCode.Length = 1 Then
				lowCode = "0" & lowCode
			End If
			Dim hightCode As String = System.Convert.ToString(bytes(1), 16)
			'取出高字节编码内容(两位16进制)
			If hightCode.Length = 1 Then
				hightCode = "0" & hightCode
			End If
				'加入到字符串中,
			coding += (lowCode & hightCode)
		Next
		Return coding
	End Function
	Public Function CodingToCharacter(coding As String) As String
		Dim characters As String = ""
		If coding.Length Mod 4 <> 0 Then
			'编码为16进制,必须为4的倍数。
			Throw New System.Exception("编码格式不正确")
		End If
		For i As Integer = 0 To coding.Length - 1 Step 4
			'每四位为一个汉字
			Dim bytes As Byte() = New Byte(1) {}
			Dim lowCode As String = coding.Substring(i, 2)
			'取出低字节,并以16进制进制转换
			bytes(0) = System.Convert.ToByte(lowCode, 16)
			Dim highCode As String = coding.Substring(i + 2, 2)
			'取出高字节,并以16进制进行转换
			bytes(1) = System.Convert.ToByte(highCode, 16)
			Dim character As String = System.Text.Encoding.Unicode.GetString(bytes)
			characters += character
		Next
		Return characters
	End Function
	Public Shared Sub Main()
		Dim code As New CodingChange()
		Dim coding As String = code.CharacterToCoding("中国")
		Console.WriteLine(coding)
		Console.WriteLine(code.CodingToCharacter(coding))
	End Sub
End Class

在串口通讯排队叫号LED瓶显示时使用

你可能感兴趣的:(.net,function,String,Integer,character,byte)