Post

Integer Literal

Integer values can be expressed in either common the Decimal system, known for its base 10 format, or the Hexadecimal system which uses the Base 16 format.

✍ Syntax

Decimal Literal

0 | NonZeroDigit Digit*

✍ Syntax

Hexadecimal Literal

HexIndicator [HexDigit+]

Here

HexIndicator = 0x|0X


✍ Syntax

Decimal Integer Literal

0 | NonZeroDigit Digit*

An Integer value can be written as a set of decimal digits. In the Decimal system, digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

An Integer value can be made negative by writing a minus (-) sign in front of the literal.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import ballerina/io;

public function main() {

    int i1 = 34368;
    int i2 = -287;
    int i3 = 0;
    int i4 = 4798;
    int i5 = -510;

    io:println(i1);
    io:println(i2);
    io:println(i3);
    io:println(i4);
    io:println(i5);
}
34368
-287
0
4798
-510
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    int i1 = 34368;
    int i2 = -287;
    int i3 = 0;
    int i4 = 4798;
    int i5 = -510;
}
// Highlights Only. Change View  to see full code.
int i1 = 34368;
int i2 = -287;
int i3 = 0;
int i4 = 4798;
int i5 = -510;

Decimal Integer Literal

Hexadecimal Integer Literal

HexIndicator [HexDigit+]

Here

HexIndicator = 0x|0X

Hexadecimal numbers start with a Hex Indicator 0x (Number zero and lowercase letter x) or 0X (Number zero and Uppercase letter X).

Hex digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a or A, b or B, c or C, d or D, e or E, f, or F. These digits stand for values from zero to fifteen in base sixteen. These digits can be represented using either lowercase or uppercase letters.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import ballerina/io;

public function main() {

    int ih1 = 0X4D3B;
    int ih2 = -0x6e1a;
    int ih3 = 0x0;
    int ih4 = -0x1B05;
    int ih5 = 0Xc8e9;

    io:println(ih1);
    io:println(ih2);
    io:println(ih3);
    io:println(ih4);
    io:println(ih5);
}
19771
-28186
0
-6917
51433
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    int ih1 = 0X4D3B;
    int ih2 = -0x6e1a;
    int ih3 = 0x0;
    int ih4 = -0x1B05;
    int ih5 = 0Xc8e9;
}
// Highlights Only. Change View  to see full code.
int ih1 = 0X4D3B;
int ih2 = -0x6e1a;
int ih3 = 0x0;
int ih4 = -0x1B05;
int ih5 = 0Xc8e9;

Hexadecimal Integer Literal

The hexadecimal number system is a useful way to represent large numbers using fewer characters. For example, the decimal number 1048575 can be expressed as FFFFF in hexadecimal. Hexadecimal is often used as a shorthand notation for binary data because it is easier to read and write. It is also used in a variety of applications, such as networking, web development, and cryptography.

However, hexadecimal is not as widely understood or used as the decimal system, which can make it more difficult for developers to work with in day-to-day operations. Unless there is a specific need, the decimal system is a better choice for everyday use. Remember the underlying bit representation of a number remains the same, regardless of whether it is represented in decimal or hexadecimal literals.

Conversion Using toString and fromString

In Ballerina, Implicit conversion between numerical types and the string type is restricted. This means that you cannot use an int value in place of a string type is needed, unless it is done explicitly.

For this, you can use the toString() language library function. To construct an int value from a string value, you can use the int:fromString() language library function.

Additionally, if you want to get the hexadecimal version, you can use the int:toHexString() and int:fromHexString() functions. These functions allow you to convert between hexadecimal and numerical values.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import ballerina/io;

public function main() returns error? {

    string ts = 19467.toString();
    int fs = check int:fromString(ts);

    string hex = 19467.toHexString();
    int fhs = check int:fromHexString(hex);

    io:println(ts);
    io:println(fs);
    io:println(hex);
    io:println(fhs);
}
19467
19467
4c0b
19467
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() returns error? {
    string ts = 19467.toString();
    int fs = check int:fromString(ts);
    string hex = 19467.toHexString();
    int fhs = check int:fromHexString(hex);
}
// Highlights Only. Change View  to see full code.
string ts = 19467.toString();
int fs = check int:fromString(ts);
string hex = 19467.toHexString();
int fhs = check int:fromHexString(hex);

Converting an int to a string

Understanding the Hexadecimal/Decimal Conversion

Here is a summary of the hexadecimal digits and their corresponding decimal and binary values.

DigitDecimal - BinaryDigitDecimal - Binary
00 - 000088 - 1000
11 - 000199 - 1001
22 - 0010a or A10 - 1010
33 - 0011b or B11 - 1011
44 - 0100c or C12 - 1100
55 - 0101d or D13 - 1101
66 - 0110e or E14 - 1110
77 - 0111f or F15 - 1111

Let us use the above table to convert Hexadecimal literal to Decimal. In a Hexadecimal literal, the rightmost digit has a place value of 16⁰(=1), the second-rightmost digit has a place value of 16¹ (=16), the third-rightmost digit has a place value of 16² (=256), and so on. By multiplying each decimal value by its corresponding place value, and then by adding them all together, the decimal value can be obtained. Here is an example.

Hexadecimal Number16² (=256)16¹ (=16)16⁰ (=1)Decimal Number
D--D (13)1 x 13 = 13
AD-A (10)D (13)16 x 10 + 1 x 13 = 173
A3DA (10)3 (3)D (13)256 x 10 + 16 x 3 + 1 x 13 = 2621

Now let us try to do the reverse by converting the same decimal literals to hexadecimal.

  1. Divide the decimal number by 16 and record the Quotient and the Remainder.
  2. Repeat this process with the quotient from the earlier step until it becomes 0. The remainder of each step is equivalent to a hexadecimal digit, where the least significant digit is the first remainder of the division. Here is an example.
Decimal NumberStepDivide by 16QuotientRemainderHex Digit for remainderHexadecimal Number
13113/16013DD
1731173/161013DAD
210/16010A
262112621/1616313DA3D
2163/161033
310/16010A

Conclusion

In this post, we have covered the basics of the Ballerina integer literal.

  • Integer literals are used to represent integer values in Ballerina.
  • Decimal literals are used to represent integer values in base 10.
  • Hexadecimal literals are used to represent integer values in base 16.
  • Unless there is a specific need, the decimal system is a better choice for everyday use.
  • Underlying bit representation of a number remains the same, regardless of whether it is represented in decimal or hexadecimal literals.
Integer Literal

Navigation

Site Settings

Site Theme

Source Code Density