1、方法1
創(chuàng)新互聯(lián)公司2013年至今,先為城區(qū)等服務建站,城區(qū)等地企業(yè),進行企業(yè)商務咨詢服務。為城區(qū)企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。
VB6中提供了LenB函數(shù)用于字符串中的字節(jié)數(shù)據(jù)。如同在雙字節(jié)字符集語言中一樣,LenB返回的是用于代表字符串的字節(jié)數(shù),而不是返回字符串中字符的數(shù)量。如為用戶自定義類型,LenB返回在內存中的大小。
LenB)
對要處理的字符串Str1,必須先使用StrConv函數(shù)把ANSI格式的Byte數(shù)組轉換為字符串,否則直接使用LenB函數(shù)得到的結果比實際結果大。
2、方法2
這里自定義了一個子函數(shù)CLen來計算漢字字符串的實際長度。同時還能判定字符串中漢字的實際個數(shù)。
Function
CLen
as
Integer
Static
HzNum
as
Integer
L
=
len
For
n=1
to
L
If
Asc)<0
Then
HzNum
=
HzNum
+
1
Next
n
Clen
=
L
+
HzNum
End
Function
CLen函數(shù)中的靜態(tài)變量HzNum返回字符串中實際漢字的數(shù)目。
字符編碼轉換嗎?
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中都是以unicode編碼存儲的
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
'轉化為字節(jié)碼
For i As Integer = 1 To n
sBytes(i - 1) = CByte("H" sInput.Substring(2 * i - 2, 2))
Next
'將字節(jié)碼轉化為字符串
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
'這里可以類推出好幾種。
GB2312是GBK編碼的子集,使用GBK編碼函數(shù)即可
'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中都是以unicode編碼存儲的
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
'轉化為字節(jié)碼
For i As Integer = 1 To n
sBytes(i - 1) = CByte("H" sInput.Substring(2 * i - 2, 2))
Next
'將字節(jié)碼轉化為字符串
ret_GBKDecode = System.Text.Encoding.Default.GetString(sBytes)
Return ret_GBKDecode
End Function
public string hz2py(string hz) //獲得漢字的區(qū)位碼
{
byte[] sarr = System.Text.Encoding.Default.GetBytes(hz);
int len = sarr.Length;
if (len1)
{
byte[] array = new byte[2];
array = System.Text.Encoding.Default.GetBytes(hz);
int i1 = (short)(array[0] - '\0');
int i2 = (short)(array[1] - '\0');
//unicode解碼方式下的漢字碼
// array = System.Text.Encoding.Unicode.GetBytes(hz);
// int i1 = (short)(array[0] - '\0');
// int i2 = (short)(array[1] - '\0');
// int t1 = Convert.ToInt32(i1,16);
// int t2 = Convert.ToInt32(i2,16);
一樣的還是使用asc函數(shù)就好了?。?,n=ASC(“漢”),不過漢字的asc碼都是小于0的
當前題目:vb.net漢字區(qū)位碼,漢字區(qū)位碼代碼
文章分享:http://muchs.cn/article4/hcjpie.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、外貿網(wǎng)站建設、Google、響應式網(wǎng)站、網(wǎng)站設計、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)