마음의 안정을 찾기 위하여 - [Delphi] Pointer to Byte array
1738113
284
722
관리자새글쓰기
태그위치로그방명록
별일없다의 생각
dawnsea's me2day/2010
색상(RGB)코드 추출기(Color...
Connection Generator/2010
최승호PD, '4대강 거짓말 검...
Green Monkey**/2010
Syng의 생각
syng's me2DAY/2010
천재 작곡가 윤일상이 기획,...
엘븐킹's Digital Factory/2010
[Delphi] Pointer to Byte array
Delphi/Etc Tip | 2019/07/25 15:47
Did you ever needed to get a variable as an array of byte? I mean you should know that the smallest value with which you can work is a byte(you can play with bits also but normally you don't) so a Word is represented on 2 Bytes, a Integer/Cardinal on 4 bytes, Double 7 Bytes, Int64 8 Bytes(se Delphi help for more info)...
First we need to declare 2 types
type
    (* declare a dynamic array of Byte type *)
    TypeByteArray = array of Byte;
    (* declare a dynamic array of PByte type *)
    TypePtrByteArray = array of PByte;
and now we need the utility functions which translates a pointer to an array of bytes, these utility functions take 2 parameters, the first parameter is the pointer to a variable(the @ operator can be used) and the second parameter takes the size in bytes of the variable, for instance if we pass a Integer value then the parameters should look like so: (@myInteger, 4), if the size of the passed parameter is 4 then you do not need to pass the second parameter, because it is declared as a constant of 4 bytes
function PointerToByteArray(
         (* the pointer to variable,
            @ operator can be used *)
         Value: Pointer;
         (* the size in bytes of the
            variable:
            Byte = 1
            Word = 2
            Integer/Cardinal = 4
            Double = 7
            Int64 = 8 *)
         const SizeInBytes: Cardinal = 4
         ): TypeByteArray;
var Address, (* store the address locally *)
    index: integer; (* for loop *)
begin
     (* get the pointer address *)
     Address := Integer(Value);
     (* set the length of the array *)
     SetLength(Result, SizeInBytes);
     (* loop to get all bytes *)
     for index := 0 to SizeInBytes do
         (* convert the address + index to a PByte pointer,
            use the ^ operator so compiler knows that
            we refer to the pointer's value *)
         Result[index] := PByte(Ptr(Address + Index))^;
end;

function PointerToPtrByteArray
         (* the pointer to variable,
            @ operator can be used *)
         (Value: Pointer;
         (* the size in bytes of the
            variable:
            Byte = 1
            Word = 2
            Integer/Cardinal = 4
            Double = 7
            Int64 = 8 *)
         const SizeinBytes: Cardinal = 4): TypePtrByteArray;
var Address, (* store the address locally *)
    index: integer; (* for loop *)
begin
     (* get the pointer address *)
     Address := Integer(Value);
     (* set the length of the array *)
     SetLength(Result, SizeInBytes);
     (* loop to get all bytes *)
     for index := 0 to SizeInBytes do
         (* convert the address + index to a PByte pointer *)
         Result[index] := Ptr(Address + Index);
end;
An example of getting an integer value as an array of bytes and display each byte value
procedure TForm1.Button1Click(Sender: TObject);
const
     CH_CR = #13; (* da' return char *)
var
   value: Integer;
   s: String;
   arr: TypeByteArray;
   index: integer;
begin
     (* assign a value to "Value" variable *)
     value := 2009;
     (* store the bytes on our dynamic array *)
     arr := PointerToByteArray(@Value);
     (* loop to get each byte from array *)
     for index := 0 to 3 do
         s := s + CH_CR + IntToStr(arr[index]);
     (* display array values in a message *)
     ShowMessage(s);
     (* resize the array to 0 elements *)
     SetLength(arr, 0);
     (* nil it *)
     arr := nil;
end;
2019/07/25 15:47 2019/07/25 15:47
Article tag list Go to top
View Comment 0
Trackback URL :: 이 글에는 트랙백을 보낼 수 없습니다
 
 
 
 
: [1] ... [17][18][19][20][21][22][23][24][25] ... [817] :
«   2020/07   »
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
전체 (817)
출판 준비 (0)
My-Pro... (41)
사는 ... (461)
블로그... (22)
My Lib... (32)
게임 ... (23)
개발관... (3)
Smart ... (1)
Delphi (76)
C Builder (0)
Object... (0)
VC, MF... (9)
Window... (1)
Open API (3)
Visual... (0)
Java, JSP (2)
ASP.NET (0)
PHP (4)
Database (12)
리눅스 (28)
Windows (20)
Device... (1)
Embedded (1)
게임 ... (0)
Web Se... (2)
Web, S... (19)
잡다한... (5)
프로젝트 (0)
Personal (0)
대통령... (9)
Link (2)