11 Vote

Lazarus: Input of binary, hexadecimal and integer numbers

Article by Delphian | Last update on 2020-09-15 | 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 are simply represented as a decimal value without a prefix in the source code. In order to distinguish these numbers from binary and hexadecimal numbers, 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, once in hexadecimal and once in binary notation.

No matter how a number is specified, you can store all of them in an integer variable 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:

DecimalBinaryHexadecimal
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 two is the 10. 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.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

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.