字符編碼轉(zhuǎn)換嗎?
為共和等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及共和網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、共和網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎ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
'這里可以類推出好幾種。
把字符串“12341168C5B7”轉(zhuǎn)換為16進(jìn)制數(shù)存為string型
用shuzi=Convert.ToInt32("12341168C5B7",16)
就ok了。
反之用 shuzi.ToString("X")就行了。
給分?。?!
12341168C5B7轉(zhuǎn)換成10進(jìn)制數(shù)就是20014839678391。。
而int32的最大值為2,147,483,647。。所以產(chǎn)生錯(cuò)誤。
所以要用int64,它的最大值為9,223,372,036,854,775,807。也就是16位十六進(jìn)制數(shù)0X7FFFFFFFFFFFFFFF。
難道還有比F靠后的數(shù)??????
1、字節(jié)數(shù)組轉(zhuǎn)換為字符串
byte[] byBuffer = new byte[20];
String strRead = new String(byBuffer);
strRead = String.copyValueOf(strRead.toCharArray(), 0, byBuffer.length]);
2、字符串轉(zhuǎn)換成字節(jié)數(shù)組
byte[] byBuffer = new byte[200];
String strInput=abcdefg;
byBuffer= strInput.getBytes();
注意:如果字符串里面含有中文,要特別注意,在android系統(tǒng)下,默認(rèn)是UTF8編碼,一個(gè)中文字符相當(dāng)于3個(gè)字節(jié),只有g(shù)b2312下一個(gè)中文相當(dāng)于2字節(jié)。這種情況下可采取以下辦法:
Dim?arr1()?As?Byte?=?{HA,?H0,?H1,?H3}
MsgBox(CStr(arr1(0)))