Private Function Conver_Hex(ByVal L As Long) As Array
靖西網(wǎng)站建設公司創(chuàng)新互聯(lián)建站,靖西網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為靖西成百上千提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\成都外貿網(wǎng)站建設公司要多少錢,請找那個售后服務好的靖西做網(wǎng)站的公司定做!
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
'下面是調用代碼
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
字符編碼轉換嗎?
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
'這里可以類推出好幾種。
這個就是問的int和char類型的存儲與位操作。
int類型32位,char類型8位。
代碼如下
#include stdio.h
int main()
{
int a = -368;
char b[4];
int i;
for(i = 0; i 4; i++)
{
b[i] = a 0xff;
a = a 8;
}
for(i = 0; i 4; i++)
printf("b[i] = %d\n", (int)b[i]);
i = (b[0]0xff) | (b[1]0xff)8 | (b[2]0xff)16 | (b[3]0xff)24;
printf("i = %d\n", i);
return 0;
}
Dim?b(1)?As?Byte,?x?As?Integer
x?=?-1234
b(0)?=?x?And?HFF
b(1)?=?x?/?H100?And?HFF
字節(jié)數(shù)組b()儲存的就是負數(shù)-1234
在VB.NET中如果需要將一個字符串轉換成字節(jié)數(shù)組,可以使用System.Text命名空間中某個編碼對象的GetBytes方法。
Dim strText As String = "This is an original string"
VB6.0可使用StrConv 函數(shù)將字符串轉換為字節(jié)流(數(shù)組)。
StrConv 函數(shù),返回按指定類型轉換的 Variant (String)。
實例代碼:
Option?Explicit
Dim?strSj?As?String
Dim?aryContent()?As?Byte
Dim?i?As?Long
Private?Sub?Command1_Click()
strSj?=?Text1
aryContent?=?StrConv(strSj,?vbFromUnicode)
For?i?=?0?To?UBound(aryContent)
Print?Hex(aryContent(i))
Next
End?Sub
Private?Sub?Form_Load()
Text1?=?"vb中怎么將字符類型轉換為字節(jié)類型"
End?Sub
vbFromUnicode,將字符串由 Unicode 轉成系統(tǒng)的缺省碼頁。 ?
分享題目:關于vb.net負數(shù)轉成字節(jié)的信息
本文地址:http://jinyejixie.com/article14/dssisge.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供ChatGPT、外貿網(wǎng)站建設、微信小程序、網(wǎng)站建設、Google、外貿建站
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)