首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

送分100分:怎么将将1234放入2个字节

2012-02-08 
送分100分:如何将将1234放入2个字节,DIM A AS LONG ,B(1) AS BYTEA1234如何将1、2和3、4分别放入B(0) B(1)

送分100分:如何将将1234放入2个字节,
DIM A AS LONG ,B(1) AS BYTE

A=1234 如何将1、2和3、4分别放入B(0) B(1) 使B(0)高四位、低四们依次为1、2, B(1)高四位、低四们依次为3、4




[解决办法]

VB code
Private Sub Command1_Click()    Dim sj As String    Dim bytSj(1) As Byte    sj = "1234"    bytSj(0) = "&H" & Mid(sj, 1, 2)    bytSj(1) = "&H" & Mid(sj, 3, 2)    Print Hex(bytSj(0))    Print Hex(bytSj(1))End Sub
[解决办法]
VB code
Option Explicit    Dim sj As String    Dim bytSj(1) As Byte    Dim hySj As StringPrivate Sub Command1_Click()    sj = "1234"    bytSj(0) = "&H" & Mid(sj, 1, 2)    bytSj(1) = "&H" & Mid(sj, 3, 2)    Print Hex(bytSj(0))    Print Hex(bytSj(1))    Dim i As Long    For i = 0 To 1    hySj = hySj & Right("0" & Hex(bytSj(i)), 2)    Next    Print hySjEnd Sub
[解决办法]
探讨
那如何再将他从们B(1)B(0)里还原回来呢。

[解决办法]
VB code
Option ExplicitDim A As Long, B(1) As BytePrivate Sub Command1_Click()    A = 1234    B(0) = ("&H" & A) \ &H100    B(1) = ("&H" & A) Mod &H100    Print Hex(B(0))    Print Hex(B(1))End SubPrivate Sub Command2_Click()    A = Hex(B(0)) & Hex(B(1))    Print AEnd Sub
[解决办法]
VB code
'用法'Dim A As Long , B() As Byte'A=1234'B=HexToArr(Hex(A))Public Function ArrToHex(Arr() As Byte) As String    Dim C As Long, I As Long, CH As String, R As String    On Error Resume Next    C = UBound(Arr)    For I = 0 To C        CH = Hex(Arr(I))        CH = String(2 - Len(CH), "0") & CH        R = R & CH    Next    ArrToHex = REnd FunctionPublic Function HexToArr(Str As String) As Byte()    Dim C As Long, I As Long, Arr() As Byte, CH As String    On Error GoTo hErr    C = Len(Str) \ 2 - 1    ReDim Arr(C)    For I = 0 To C        CH = Mid(Str, I * 2 + 1, 2)        Arr(I) = CByte("&H" & CH)    Next    HexToArr = ArrhErr:End Function
[解决办法]
VB code
Private Sub Command1_Click()    Dim A As Long, B(1) As Byte    A = 1234    B(0) = "&H" & Left(A, 2)    B(1) = "&h" & Right(A, 2)    MsgBox B(0) & " " & B(1)    A = Hex(B(0)) & Format(Hex(B(1)), "00")    MsgBox AEnd Sub 

热点排行