关于 符 的 半形 / 全形 转换
关于 符 的 半形 / 全形 转换
关于 符 的 半形 / 全形 转换
<< VB.Net >>
Imports System.Text
Public Class Form1
' 如何判断输入的符是 "全形" 或 "半形" ?
' 方法 1
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) _
Handles TextBox1.KeyPress
' 判断输入符的 ASCII 符码之 16 进制长度 , 2 为半形
If Hex(Asc(e.KeyChar)).Length = 2 Then
MessageBox.Show("半形符")
Else
MessageBox.Show("全形符")
End If
End Sub
' KeyPressEventArgs.KeyChar 属性 : 取得对应于所按下按键的符。
' Hex 函数 : 返回代表数字十六进制值的字符串。
' Asc 函数 : 返回Integer 值,表示与符对应的符码。
' Asc 会返回输入符的“字码指针”(Code Point) 或符码。
' 这可以是值从 0 到 255 的单一字节符集 ( Single-Byte Character Set,SBCS ),
' 和值从 -32768 到 32767 的双字节符集 ( DBCS )。
' ASCII 字码表含有扩充 ASCII (American Standards Committee for Information Interchange)
' 符集的 十进制 和 十六进制值。
' 扩充符集包括 ASCII 符集和 128 个绘制图形和线条的其他符,通常称为“IBM 符集”。
' 在Windows 中,字码大于 127 的符,其显示会依选取的字体而不同。
' ================================================================
' 方法 2
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) _
Handles TextBox2.KeyPress
' 判断输入符于 Unicode 编码时的 第二个字节 若是 255 则是 全形, 反之则为 半形
If Encoding.Unicode.GetBytes(e.KeyChar)(1) = 255 Then
MessageBox.Show("全形符")
Else
MessageBox.Show("半形符")
End If
' PS : 此方法不适用于 中文字
End Sub
' System.Text 命名空间 Encoding 类
' Unicode 属性 : 以 Little-Endian 字节顺序取得 UTF-16 格式的 编码方式。
' GetBytes 方法 : 在衍生类中覆写时,将符集编码成字节序列。
' ================================================================
' 如何将使用者所输入的 全形符 转成 半形符 ?
' 方法 1
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) _
Handles TextBox3.KeyPress
Dim KeyAscii As Short = Asc(e.KeyChar)
If KeyAscii < 0 Then e.KeyChar = Chr(Asc(StrConv(Chr(KeyAscii), VbStrConv.Narrow)))
' StrConv 函数 : 返回依照指定方式转换的字符串。
' VbStrConv.Narrow : 将字符串中的 全形符 转换为 半形符。
End Sub
' KeyPressEventArgs.KeyChar 属性 : 设定对应于所按下按键的符。
' Chr 函数 : 返回与指定符码关联的符 ; Chr 的有效范围是 0 到 255。
' Public Function Chr(ByVal CharCode As Integer) As Char
' CharCode : 代表符的“字码指针”或 符码的 Integer 运算式。
' 如果 CharCode 不在有效范围内,则会发生 ArgumentException 错误。
' 如何将使用者所输入的 半形符 转成 全形符 ?
Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) _
Handles TextBox4.KeyPress
Dim KeyAscii As Short = Asc(e.KeyChar)
If KeyAscii > 0 Then e.KeyChar = Chr(Asc(StrConv(Chr(KeyAscii), VbStrConv.Wide)))
' VbStrConv.Wide : 将字符串中的 半形符 转换为 全形符 。
End Sub
' ================================================================
' 如何将使用者所输入的 全形符 转成 半形符 ?
' 方法 2
Private Sub TextBox5_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) _
Handles TextBox5.KeyPress
Dim byt() As Byte = Encoding.Unicode.GetBytes(e.KeyChar)
If byt(1) = 255 Then ' 若第二个字节是 255 , 则将 全形 转 半形
byt(0) = byt(0) + 32 ' 第一个字节 + 32
byt(1) = 0 ' 第二个字节设为 0
e.KeyChar = Encoding.Unicode.GetChars(byt)(0)
End If
End Sub
' System.Text 命名空间 Encoding 类
' Unicode 属性 : 以Little-Endian 字节顺序取得 UTF-16 格式的编码方式。
' GetChars 方法 : 在衍生类中覆写时,将字节序列解码成符集。
' 如何将使用者所输入的 半形符 转成 全形符 ?
Private Sub TextBox6_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) _
Handles TextBox6.KeyPress
Dim byt() As Byte = Encoding.Unicode.GetBytes(e.KeyChar)
If byt(1) = 0 Then ' 若第二个字节是 0 , 则将 半形 转 全形
byt(0) = byt(0) - 32 ' 第一个字节 - 32
byt(1) = 255 ' 第二个字节设为 255
e.KeyChar = Encoding.Unicode.GetChars(byt)(0)
End If
End Sub
End Class
' ================================================================
全型 / 半型 转换 Function 供参考
' 半型 转 全型
Public Function Narrow2Wide(ByVal data As String) As String
Return String.Join("", Array.ConvertAll(Of Char, String)(data.ToCharArray, AddressOf Cvt_N_2_W))
End Function
Private Function Cvt_W_2_N(ByVal c As Char) As String
Dim byt() As Byte = Encoding.Unicode.GetBytes(c)
If byt(1) = 255 Then Return Encoding.Unicode.GetString(New Byte() {byt(0) + 32, 0}) Else Return c
End Function
' --------------------------------------------------------------------------------------------------------------------
' 全型 转 半型
Public Function Wide2Narrow(ByVal data As String) As String
data = data.Replace("〔", "[").Replace("〕", "]").Replace("'", "'").Replace(" ", " ")
Return String.Join("", Array.ConvertAll(Of Char, String)(data.ToCharArray(), AddressOf Cvt_W_2_N))
End Function
Private Function Cvt_N_2_W(ByVal c As Char) As String
Dim byt() As Byte = Encoding.Unicode.GetBytes(c)
If byt(1) = 0 Then Return Encoding.Unicode.GetString(New Byte() {byt(0) - 32, 255}) Else Return c
End Function
' --------------------------------------------------------------------------------------------------------------------
' 半型 转 全型
Public Function ToWchr(ByRef data As String) As String
Dim sb As New StringBuilder
Dim ascii As Integer = 0
For Each c As Char In data.ToCharArray()
ascii = Convert.ToInt32(c)
If ascii = 32 Then
sb.Append(Convert.ToChar(12288))
Else
sb.Append(Convert.ToChar(ascii + IIf(ascii < 127, 65248, 0)))
End If
Next
Return sb.ToString
End Function
' --------------------------------------------------------------------------------------------------------------------
' 全型 转 半型
Public Function ToNchr(ByRef data As String) As String
Dim sb As New StringBuilder
Dim ascii As Integer = 0
For Each c As Char In data.Replace("〔", "[").Replace("〕", "]").Replace("'", "'").ToCharArray()
ascii = Convert.ToInt32(c)
If ascii = 12288 Then
sb.Append(Convert.ToChar(32))
Else
If ascii > 65280 And ascii < 65375 Then
sb.Append(Convert.ToChar(ascii - 65248))
Else
sb.Append(Convert.ToChar(ascii))
End If
End If
Next
Return sb.ToString
End Function
PS :
半形 空格 ASCII 为 32 , 全形 空格 ASCII 为12288
其他符 半形ASCII 33~126 与 全形ASCII 65281~65374 对应之 ASCII 皆相差 65248
全形符号 〔 〕' 转 半型时 ASCII 对应关系不同 , 因此直接用 Replace 做处理
' ================================================================
<< VB6 >>
' 如何判断输入的符是 "全形" 或 "半形" ?
' 方法 1
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim strChr As String
strChr = Chr(KeyAscii)
If LenB(strChr) = LenB(StrConv(strChr, vbFromUnicode)) Then
MsgBox "全形符"
Else
MsgBox "半形符"
End If
End Sub
' Chr 函数 : 返回一个含有与指定的符码相关之符的 String。
' 语法
' Chr (charcode)
' charcode 参数是一个用来识别某符的Long。
' 请注意 : 0 到 31 之间的数字与一般、非打印的ASCII码相同,例如,Chr(10) 会返回换行符。
' charcode 的正常范围为 0-255。然而,在DBCS系统,charcode 的实际范围为 -32768 到 65535。
' LenB 函数 : 返回一Long,为保存一变量所需的字节数。
' 语法
' Len(string | varname)
' string 为任何正确的字符串运算式。如果 string 所含为 Null,会返回Null。
' Varname 为任何正确的变量名称。如果 varname 所含为 Null,会返回 Null。
' 如果 varname 是Variant,Len 会视其为 String并且返回其所含之符数。
' 请注意 : 两参数必须有其一 (而且只能有一)。如为使用者自订型态,,Len 会返回其写至文件的大小。
' LenB 函数是使用在字节数据字符串上,如同在双字节符集 (DBCS) 语言中一样。
' 所以 LenB 返回的是字节位置,而非符位置。
' 如为使用者自订型态,LenB 会返回在内存中之大小,包括组件之间的间隙。
' StrConv 函数 : 返回一特定转换后的 Variant (String)。
' 语法
' StrConv(string, conversion, LCID)
' string 必要参数。为欲转换的字符串运算式。
' conversion 必要参数:为Integer。其值的和决定转换的型态。
' string 必要参数。为欲转换的字符串运算式。
' LCID 选项的。如果与系统LocaleID不同,则为LocaleID(系统LocaleID为缺省值。)
' conversion 参数的设定:
' vbFromUnicode ( 128 ) : 将字符串由 Unicode 转成系统的默认符码对应页。
' vbUnicode ( 64 ) : 据系统的默认符码对应页将字符串转成 Unicode。
' vbWide ( 4 ) : 将字符串中单字节符转成双字节符。 ( 适用于远东地区 )
' vbNarrow ( 8 ) : 将字符串中双字节符转成单字节符。 ( 适用于远东地区 )
' ================================================================
' 方法 2
Private Sub Text2_KeyPress(KeyAscii As Integer)
Dim strChr As String
Dim byt() As Byte
strChr = Chr(KeyAscii)
byt = StrConv(strChr, vbFromUnicode)
If byt(0) > 128 Then
MsgBox "全形符"
Else
MsgBox "半形符"
End If
Erase byt
End Sub
' ================================================================
' 方法 3
Private Sub Text3_KeyPress(KeyAscii As Integer)
If Len(Hex(KeyAscii)) > 2 Then
MsgBox "全形符"
Else
MsgBox "半形符"
End If
End Sub
' ================================================================
' 方法 4
Private Sub Text4_KeyPress(KeyAscii As Integer)
Dim strChr As String
strChr = Chr(KeyAscii)
If LenB(StrConv(strChr, vbFromUnicode)) = 2 Then
MsgBox "全形符"
Else
MsgBox "半形符"
End If
End Sub
' ================================================================
' 方法 5
Private Sub Text5_KeyPress(KeyAscii As Integer)
Dim strChr As String
Dim byt() As Byte
strChr = Chr(KeyAscii)
byt = StrConv(strChr, vbFromUnicode)
If UBound(byt) = 1 Then
MsgBox "全形符"
Else
MsgBox "半形符"
End If
Erase byt
End Sub
' UBound 函数 : 返回 Long值,表示指定数组某维最大可使用的数组索引。
' 语法
' UBound(arrayname[, dimension])
' arrayname 必要的参数。数组变量的名称,遵循标准变量命名规格。
' dimension 选择性参数。Variant (Long),表示返回的是那一维的上限。
' 1 表示第一维,2 表示第二维,依此类推。如果省略 dimension,则默认是 1。
' Erase 陈述式 : 重新初始化固定大小数组的元素,并释放动态数组的保存空间。
' ================================================================
' 半形符 转换 全形符
Private Sub Text6_KeyPress(KeyAscii As Integer)
Dim strChr As String
strChr = Chr(KeyAscii)
KeyAscii = Asc(StrConv(strChr, vbWide))
End Sub
' ================================================================
' 全形符 转换 半形符
Private Sub Text7_KeyPress(KeyAscii As Integer)
Dim strChr As String
strChr = Chr(KeyAscii)
KeyAscii = Asc(StrConv(strChr, vbNarrow))
End Sub
' Asc 函数 : 返回一个 Integer,为字符串中第一个字母的符码。
' 语法
' Asc(string)
' string 参数是任何可用的字符串运算式。若是 string 中没有包含任何符,则会产生执行阶段错误。
' 请注意
' 在非双字节符集系统下,其返回范围为 0 - 255。 若在双字节符集系统下,则为 -32768 - 32767。
' 附注 AscB 函数是用来处理包含字节数据的字符串,AscB 会返回第一个字节,而非第一个符的符码。
' AscW 函数会返回Unicode符码,若平台不支持Unicode,则与Asc函数功能相同。