| Bytes and data-types | Delphi programming | mortenbs.com Last updated Jan 2012. |
A byte is an unsigned number in the range [0..255]
(256 possible values).Different ways to observe a byte| System | Range |
| Decimal | 0..255 |
| Hexadecimal | 00..FF |
| Binary | 00000000..11111111 |
Data types based on a range of bytes| Data type | Type | Size | Description | Range |
| Char | fixed | 1 byte | Ansi char | #0..#255 ($00..$FF) |
| Word | fixed | 2 bytes | 16-Bit unsigned | 0..65535 ($0000..$FFFF) |
| SmallInt | fixed | 2 bytes | 16-Bit signed | -32768..32767 ($0000..$FFFF) |
| LongWord (cardinal) | fixed | 4 bytes | 32-Bit unsigned | 0..4294967295 ($00000000..$FFFFFFFF) |
| LongInt (integer) | fixed | 4 bytes | 32-Bit signed | -2147483648..2147483647 ($00000000..$FFFFFFFF) |
| Int64 | fixed | 8 bytes | 64-Bit signed | -9223372036854775808..9223372036854775807 |
| Single | fixed | 4 bytes | float | |
| Double | fixed | 8 bytes | float | |
| Extended | fixed | 10 bytes | float | |
| String | dynamic | length(s)+1 | NULL terminated (#0) | |
Table of bits| Bit | Hex | Bytes | Value/ID | Combinations |
| 1 | $01 | <1 | $00000001 | 2 |
| 2 | $02 | <1 | $00000002 | 4 |
| 3 | $03 | <1 | $00000004 | 8 |
| 4 | $04 | <1 | $00000008 | 16 |
| 5 | $05 | <1 | $00000010 | 32 |
| 6 | $06 | <1 | $00000020 | 64 |
| 7 | $07 | <1 | $00000040 | 128 |
| 8 | $08 | 1 | $00000080 | 256 |
| 9 | $09 | <2 | $00000100 | 512 |
| 10 | $0A | <2 | $00000200 | 1.024 |
| 11 | $0B | <2 | $00000400 | 2.048 |
| 12 | $0C | <2 | $00000800 | 4.096 |
| 13 | $0D | <2 | $00001000 | 8.192 |
| 14 | $0E | <2 | $00002000 | 16.384 |
| 15 | $0F | <2 | $00004000 | 32.768 |
| 16 | $10 | 2 | $00008000 | 65.536 |
| 17 | $11 | <3 | $00010000 | 131.072 |
| 18 | $12 | <3 | $00020000 | 262.144 |
| 19 | $13 | <3 | $00040000 | 524.288 |
| 20 | $14 | <3 | $00080000 | 1.048.576 |
| 21 | $15 | <3 | $00100000 | 2.097.152 |
| 22 | $16 | <3 | $00200000 | 4.194.304 |
| 23 | $17 | <3 | $00400000 | 8.388.608 |
| 24 | $18 | 3 | $00800000 | 16.777.216 |
| 25 | $19 | <4 | $01000000 | 33.554.432 |
| 26 | $1A | <4 | $02000000 | 67.108.864 |
| 27 | $1B | <4 | $04000000 | 134.217.728 |
| 28 | $1C | <4 | $08000000 | 268.435.456 |
| 29 | $1D | <4 | $10000000 | 536.870.912 |
| 30 | $1E | <4 | $20000000 | 1.073.741.824 |
| 31 | $1F | <4 | $40000000 | 2.147.483.648 |
| 32 | $20 | 4 | $80000000 | 4.294.967.296 |
There is 8 bits of one byte.
Each bit has a value/ID:
1+
2+
4+
8+
16+
32+
64+
128 =
256Minimum value is ZERO:
256¹-
1 = [
0..
255]
For obtaining higher numbers than
255, multiple bytes is combined for having more total bits available.
Using
two bytes there is total
16 bits: (
256*
256) =
65536 possible values.
Type conversionsch:=ansiChar(b); //Byte to char
b:=byte(ch); //Char to byte
Delphi source HTML generated by PAS to HTM | Mini converter, mortenbs.comAn example of reading the 8 bits from a single byte:
procedure byteToBits(aByte:byte;out b1,b2,b3,b4,b5,b6,b7,b8:boolean);
begin
b1:= 1 and aByte<>0;
b2:= 2 and aByte<>0;
b3:= 4 and aByte<>0;
b4:= 8 and aByte<>0;
b5:= 16 and aByte<>0;
b6:= 32 and aByte<>0;
b7:= 64 and aByte<>0;
b8:=128 and aByte<>0
end;
Delphi source HTML generated by PAS to HTM | Mini converter, mortenbs.comAn example of defining the 8 bits in a single byte:
function bitsToByte(b1,b2,b3,b4,b5,b6,b7,b8:boolean):byte;
begin
if b1 then result:=1 else result:=0;
if b2 then inc(result,2);
if b3 then inc(result,4);
if b4 then inc(result,8);
if b5 then inc(result,16);
if b6 then inc(result,32);
if b7 then inc(result,64);
if b8 then inc(result,128)
end;
Delphi source HTML generated by PAS to HTM | Mini converter, mortenbs.comIn some cases the TBits class is very useful:
uses
classes;
//...
function bitsToByte(aBits:tBits):byte;
const n:array[0..7] of byte=(1,2,4,8,16,32,64,128);
var b:byte;
begin result:=0;
for b:=0 to 7 do if aBits[b] then inc(result,n[b])
end;
Delphi source HTML generated by PAS to HTM | Mini converter, mortenbs.comSee also
Delphi programming - Basics, Components, links...
IT - IT/Technology. Various projects and information about computer technology and electronic...
mortenbs.com