Convert byte array... -Reply to "high group" Questions

Source: Internet
Author: User
Problem Source: http://www.cnblogs.com/del/archive/2009/03/21/1410030.html#1483614
{Convert tbytes to integer} procedure tform1.button1click (Sender: tobject); var BS: tbytes; {tbytes is the dynamic array of byte} I: integer; begin {it should be the same size as integer for conversion} setlength (BS, 4); BS [0]: = $10; BS [1]: = $27; BS [2]: = 0; BS [3]: = 0; {because tbytes is a dynamic array, its variable BS is a pointer. Therefore, it is first converted to pinteger} I: = pinteger (BS) ^; showmessage (inttostr (I); {10000} end; {It is easier to convert from bytes static array to integer} procedure tform1.button2click (Sender: tobject); var BS: array [0 .. 3] of byte; I: integer; begin BS [0]: = $10; BS [1]: = $27; BS [2]: = 0; BS [3]: = 0; I: = INTEGER (BS); showmessage (inttostr (I) ;{ 10000} end; {convert to custom structure} procedure tform1.button3click (Sender: tobject); Type tdata = packed record a: integer; B: word; end; var BS: array [0 .. 5] of byte; {the array should be consistent with the structure size} data: tdata; begin fillchar (BS, length (BS), 0); BS [0]: = $10; BS [1]: = $27; Data: = tdata (BS); showmessage (inttostr (data. a); {10000} end; {A member converted to the custom structure} procedure tform1.button4click (Sender: tobject); Type tdata = packed record a: integer; B: word; end; var BS: array [0 .. 3] of byte; Data: tdata; begin fillchar (BS, length (BS), 0); BS [0]: = $10; BS [1]: = $27; data. a: = INTEGER (BS); showmessage (inttostr (data. a); {10000} end;
 
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.