[mortenbs.com home]
Music
Music
IT
IT
Videos
Videos
Contact
Contact
[icon] 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
Decimal0..255
Hexadecimal  00..FF
Binary00000000..11111111

Data types based on a range of bytes
Data typeTypeSizeDescriptionRange
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
BitHexBytesValue/IDCombinations
1$01<1$000000012
2$02<1$000000024
3$03<1$000000048
4$04<1$0000000816
5$05<1$0000001032
6$06<1$0000002064
7$07<1$00000040128
8$081$00000080256
9$09<2$00000100512
10$0A<2$000002001.024
11$0B<2$000004002.048
12$0C<2$000008004.096
13$0D<2$000010008.192
14$0E<2$0000200016.384
15$0F<2$0000400032.768
16$102$0000800065.536
17$11<3$00010000131.072
18$12<3$00020000262.144
19$13<3$00040000524.288
20$14<3$000800001.048.576
21$15<3$001000002.097.152
22$16<3$002000004.194.304
23$17<3$004000008.388.608
24$183$0080000016.777.216
25$19<4$0100000033.554.432
26$1A<4$0200000067.108.864
27$1B<4$04000000134.217.728
28$1C<4$08000000268.435.456
29$1D<4$10000000536.870.912
30$1E<4$200000001.073.741.824
31$1F<4$400000002.147.483.648
32$204$800000004.294.967.296

There is 8 bits of one byte.
Each bit has a value/ID: 1+2+4+8+16+32+64+128 = 256
Minimum 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 conversions
ch:=ansiChar(b);     //Byte to char
b:=byte(ch);         //Char to byte
Delphi source HTML generated by PAS to HTM | Mini converter, mortenbs.com


An 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.com


An 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.com


In 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.com


See also
[icon] Delphi programming - Basics, Components, links...
[icon] IT - IT/Technology. Various projects and information about computer technology and electronic...


[icon] mortenbs.com