Post

Byte Array Literal

Byte Array Literal is a sequence of bytes represented in base16 or base64.

✍ Syntax

Base16 Byte Array Literal

base16 `<hex-digit>*`

You can use Base16 Byte Array Literal syntax to represent bytes in Hex textual form.


✍ Syntax

Base64 Byte Array Literal

base64 `<base64-char>*`

You can use Base64 Byte Array Literal syntax to represent bytes in Base64 encoded form.


✍ Syntax

There are two types of Byte Array Literals. Base16 Byte Array Literal and Base64 Byte Array Literal. Additionally, you can use Array Constructor Expression to create a byte array. But instead of writing bytes in textual form, you need to write bytes in decimal form in Array Constructor Expression, which is not convenient for writing long list of bytes.

Base16 Byte Array Literal

You can use Base16 Byte Array Literal syntax to represent bytes in Hex textual form.

base16 `<hex-digit>*`

  • Base16 Literal uses Hex Digits; 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.
    • In regex form, A..F | a..f | 0..9
  • You need two Hex digits to represent a byte.
    • One hex digits represent 4 bits. i.e . 0 -> 0000, 9 -> 1001, A -> 1010, F -> 1111
    • So 0A in hex means 0000 1010 which equals byte value 10. Similarly 11 -> 0001 0001(17), AB -> 1010 1011(171)
  • You can have any white space in between two Hex Digits.
    • Group Hex Digits to improve the readability of your code.
    • Writing Hex digits in pairs or even numbers is a good option.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import ballerina/io;

public function main() {

    byte[] a = base16 `11AB22CD`;
    byte[] b = base16 `c3 d4 e5 f6`;
    byte[] c = base16 ` 55 EE 66 FF
                      77 AB 88 CD
                      99 EF 00 AA
                    `;

    io:println(a);
    io:println(b);
    io:println(c);
}
[17,171,34,205]
[195,212,229,246]
[85,238,102,255,119,171,136,205,153,239,0,170]
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {

    byte[] a = base16 `11AB22CD`;
    byte[] b = base16 `c3 d4 e5 f6`;
    byte[] c = base16 ` 55 EE 66 FF
                      77 AB 88 CD
                      99 EF 00 AA
                    `;
}
// Highlights Only. Change View  to see full code.
byte[] a = base16 `11AB22CD`;
byte[] b = base16 `c3 d4 e5 f6`;

Base64 Byte Array Literal

You can use Base64 Byte Array Literal syntax to represent bytes in Base64 encoded form.

base64 `<base64-char>*`

  • Base64 Literal uses Base64 alphabet; A..Z | a..z | 0..9 | + | /

    • Total 64 characters = 26 Uppercase Letters + 26 Lowercase Letters + 10 numbers + 2 chars.
    • Each Base64 character represents 6 bits.
    • The Encoding table is the same as RFC 4648.
  • You need four Base64 characters to represent 3 bytes.

    • 4 x 6 bits = 24 bits = 3 bytes
  • The length of Base64 literal must be a multiple of four.

    • You can add one or two padding characters = to make its length multiple of 4.
    • Single = means the last four characters will represent only two bytes.
    • == means the last four characters will represent only one byte.
    • = or == always comes at the end of the literal.
  • You can have any white space in between two Base64 Characters.

    • Group Base64 Characters to improve the readability of your code.
    • Writing Base64 Characters in a group of 4 characters is a good option.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import ballerina/io;

public function main() {

    byte[] a = base64 `AAAB`;
    byte[] b = base64 `ABB=`;
    byte[] c = base64 `BC==`;
    byte[] d = base64 `ABCD pqrs
                     5678 +/12`;

    io:println(a);
    io:println(b);
    io:println(c);
    io:println(d);
}
[0,0,1]
[0,16]
[4]
[0,16,131,166,170,236,231,174,252,251,253,118]
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {

    byte[] a = base64 `AAAB`;
    byte[] b = base64 `ABB=`;
    byte[] c = base64 `BC==`;
    byte[] d = base64 `ABCD pqrs
                     5678 +/12`;
}
// Highlights Only. Change View  to see full code.
byte[] a = base64 `AAAB`;
byte[] b = base64 `ABB=`;

Base64 to Byte Array Explained

Inorder to understand the Base64 to Byte Array conversion, let’s refresh our memory on how Base64 encoding works. Following table shows the Base64 encoding table.

#CharBits#CharBits#CharBits#CharBits
0A00000016Q01000032g10000048w110000
1B00000117R01000133h10000149x110001
2C00001018S01001034i10001050y110010
3D00001119T01001135j10001151z110011
4E00010020U01010036k100100520110100
5F00010121V01010137l100101531110101
6G00011022W01011038m100110542110110
7H00011123X01011139n100111553110111
8I00100024Y01100040o101000564111000
9J00100125Z01100141p101001575111001
10K00101026a01101042q101010586111010
11L00101127b01101143r101011597111011
12M00110028c01110044s101100608111100
13N00110129d01110145t101101619111101
14O00111030e01111046u10111062+111110
15P00111131f01111147v10111163/111111

Let’s look at the following examples.

Example 1: base64 `AAAB`

Base64AAAB
Index0001
Bit pattern000000000000000000000001
Byte0000 00000000 00000000 00001
Ballerina byte001

Example 2: base64 `ABB=`

Base64ABB=
Index011X
Bit pattern000000000001000001000000
Byte0000 00000001 0000x
Ballerina byte016

Example 3: base64 `BC==`

Base64BC==
Index12XX
Bit pattern000001000010000000000000
Byte0000 0100XX
Ballerina byte4

Known Limitations in JBallerina

Byte Array Literal