Lazarus: Input of binary, hexadecimal and integer numbers
Article by Stefan Trost | Last update on 2023-01-10 | Created on 2014-01-28
What some may not know: In Lazarus, next to integers, it is readily possible to define and enter binary and hexadecimal numbers directly in the code. In this article, I would like to show you how to do that.
The Prefixes $ and %
Integer numbers can simply be written into the code as a decimal number without any prefix or other particularities that have to be cosidered.
In order to distinguish these numbers from binary and hexadecimal numbers in the code, hexadecimal values are preceded by $ and binary values by %:
var a, b, c, d, e: integer; begin a := 20; // decimal b := $0F; // hexadecimal c := %10100; // binary d := a + b + c; // = 55 e := d + 2 + $2 + $10; // = 66 end;
In this example, we set the integer variables a, b and c each to the value 20 - once in decimal (20), once in hexadecimal ($oF) and once in binary notation (%10100).
No matter how a number is specified, you can store all of them in a variable of the type Integer and you are able to mix them in your calculations. This also shows the line in which we set the variable "e" to the sum of "d" and the numbers from the other systems.
Conversion Table
To get a feeling for the conversion between the different number systems, here is a table with some values in the different systems:
Decimal | Binary | Hexadecimal |
0 | %00000 | $00 |
1 | %00001 | $01 |
2 | %00010 | $02 |
3 | %00011 | $03 |
4 | %00100 | $04 |
5 | %00101 | $05 |
6 | %00110 | $06 |
7 | %00111 | $07 |
8 | %01000 | $08 |
9 | %01001 | $09 |
10 | %01010 | $0A |
11 | %01011 | $0B |
12 | %01100 | $0C |
13 | %01101 | $0D |
14 | %01110 | $0E |
15 | %01111 | $0F |
16 | %10000 | $10 |
17 | %10001 | $11 |
18 | %10010 | $12 |
19 | %10011 | $13 |
20 | %10100 | $14 |
100 | %1100100 | $64 |
255 | %011111111 | $00FF |
256 | %100000000 | $0100 |
257 | %100000001 | $0101 |
1000 | %1111101000 | $03E8 |
The decimal system is a number system with the base 10, so it includes the digits 0 to 9. In the binary system the base is 2, it only has the digits 0 and 1, even the decimal two is the 10 here. The hexadecimal system is a system with the base 16. It consists of the "digits" 0 to 9 and A to F to represent the numbers 0 to 15. First at the decimal 16, this number system becomes double-digit.
Incidentally, in the notation of hexadecimal and binary numbers in Lazarus, the number of leading zeros does not play a role. Whether you are writing $002, $02, $2, %0010 or %10 does not matter and the leading zeros are only used for a better readability within the code.
About the Author
You can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile
Related Topics
MySQL: Line Breaks in MySQL
Tip | 0 Comments
XAMPP: How to set up SSL/HTTPS for local Projects
Tutorial | 4 Comments
HTML: Preassign HTML Form with Data
Tutorial | 0 Comments
Delphi/Lazarus: Only allow Numbers in TEdit
Tip | 0 Comments
Lazarus: Load File as Byte Array and save Byte Array as File
Tutorial | 0 Comments
How to resize Image before Upload in Browser
Tutorial | 13 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.