Есть:
- Код: Выделить всё
Dim Massiv(3) As Byte
Есть:
- Код: Выделить всё
Dim Chislo As Long
Как представить Chislo в виде Massiv(3)?
Т.е. как разбить 4-х байтное число на массив байт?
Dim Massiv(3) As Byte
Dim Chislo As Long
And'ом
Например, Chislo And &HFF& - старший байт.
BV писал(а):Да, младший.
Старший так:
Chislo / &HFFFFFF& And &HFF&
Option Explicit
Private Declare Sub RtlMoveMemory Lib "kernel32.dll" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
Private Type Int32_2
LWord As Integer 'Младшее слово
HWord As Integer 'Старшее слово
End Type
Private Type Int32_4
LByte1 As Byte 'Младший байт
LByte2 As Byte
HByte2 As Byte
HByte1 As Byte 'Старший байт
End Type
Dim int32Number2 As Int32_2
Dim int32Number4 As Int32_4
Dim lNumber As Long
Private Sub cmdBegin_Click()
With int32Number2
.LWord = -15846
.HWord = 24791
End With
Call RtlMoveMemory(lNumber, int32Number2, 4)
Dim intWord As Integer
Dim bByte As Byte
Me.CurrentX = 10 * Screen.TwipsPerPixelX
Me.CurrentY = 10 * Screen.TwipsPerPixelX
Me.Print "Результат разложения lNumber на 2 слова (Word):" & vbCrLf
Me.CurrentX = 20 * Screen.TwipsPerPixelX
Call RtlMoveMemory(intWord, lNumber, 2)
Me.Print "LoWord: " & Str$(intWord)
Me.CurrentX = 20 * Screen.TwipsPerPixelX
Call RtlMoveMemory(intWord, ByVal VarPtr(lNumber) + 2&, 2)
Me.Print "HiWord: " & Str$(intWord)
With int32Number4
.LByte1 = 165
.LByte2 = 49
.HByte2 = 237
.HByte1 = 198
End With
Call RtlMoveMemory(lNumber, int32Number4, 4)
Me.Print
Me.CurrentX = 10 * Screen.TwipsPerPixelX
Me.Print "Результат разложения lNumber на 4 байта:" & vbCrLf
Me.CurrentX = 20 * Screen.TwipsPerPixelX
Call RtlMoveMemory(bByte, lNumber, 1)
Me.Print "LoByte1: " & Str$(bByte)
Me.CurrentX = 20 * Screen.TwipsPerPixelX
Call RtlMoveMemory(bByte, ByVal VarPtr(lNumber) + 1&, 1)
Me.Print "LoByte2: " & Str$(bByte)
Me.CurrentX = 20 * Screen.TwipsPerPixelX
Call RtlMoveMemory(bByte, ByVal VarPtr(lNumber) + 2&, 1)
Me.Print "HiByte2: " & Str$(bByte)
Me.CurrentX = 20 * Screen.TwipsPerPixelX
Call RtlMoveMemory(bByte, ByVal VarPtr(lNumber) + 3&, 1)
Me.Print "HiByte1: " & Str$(bByte)
End Sub
Private Type tb
b1 As Byte
b2 As Byte
b3 As Byte
b4 As Byte
End Type
Private Type ta
a(3) As Byte
End Type
Private Type ti
i As Long
End Type
Public Sub Main()
Dim ii As ti, bb As tb, aa As ta
Debug.Print "---"
ii.i = 1025
LSet bb = ii
Debug.Print "1:", ii.i, bb.b4, bb.b3, bb.b2, bb.b1
LSet aa = ii
Debug.Print "2:", ii.i, aa.a(3), aa.a(2), aa.a(1), aa.a(0)
Debug.Print "==="
End Sub
Сейчас этот форум просматривают: Google-бот, SemrushBot и гости: 12