Private Function Conver_Hex(ByVal L As Long) As Array
10年積累的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有吐魯番免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
Dim str_L As String = Hex(L)
If str_L.Length \ 2 0 Then
str_L = "0" str_L
End If
Dim byt(str_L.Length / 2 - 1) As Byte
For i = 0 To str_L.Length / 2 - 1
byt(i) = Convert.ToByte(str_L.Substring(2 * i, 2), 16)
Next
Return byt
End Function
'下面是調(diào)用代碼
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim L As Double = 123456
Dim byt() As Byte = Conver_Hex(L)
End Sub
字符編碼轉(zhuǎ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
'這里可以類推出好幾種。
本文介紹了各種編碼類型,然后討論了將字符串轉(zhuǎn)換成字節(jié)數(shù)組的方法。編碼類型用二進(jìn)制形式描述一個(gè)字符串變量有很多種方式。用來(lái)實(shí)現(xiàn)該功能的編碼對(duì)象依賴于你選擇的編碼。下面是程序開(kāi)發(fā)人員經(jīng)常使用的編碼類型:ASCII:將字符串中的每個(gè)字符使用7個(gè)比特編碼。這種編碼類型不能包含擴(kuò)展的Uincode字符。Full Unicode(UTF-16):將字符串中的每個(gè)字符使用16個(gè)比特編碼。這種情況下對(duì)每個(gè)字符一個(gè)字節(jié)數(shù)組含有兩個(gè)字節(jié)。UTF-7:對(duì)于普通的ASCII字符使用7比特編碼,對(duì)于擴(kuò)展字符使用多個(gè)7比特對(duì)編碼。這種編碼類型最常用于7比特協(xié)議中如mail等。UTF-8:對(duì)于普通的ASCII字符使用8比特編碼,對(duì)于擴(kuò)展字符使用多個(gè)8比特對(duì)編碼。這種情況下對(duì)每個(gè)字符一個(gè)字節(jié)數(shù)組包含一個(gè)字節(jié)。
你搞錯(cuò)了一點(diǎn):Unicode字符規(guī)定,每個(gè)字符占用兩個(gè)字節(jié),包括英文和數(shù)字,所以你只需將:
MarshalAs(UnmanagedType.ByValTStr, sizeconst:=3) Public s As String
改成:
MarshalAs(UnmanagedType.ByValTStr, sizeconst:=6) Public s As String
即可!
ToCharArray()
例如:str="asdfafafafaf"
str.toCharArray就是一個(gè)Char()數(shù)組了。