import ballerina/io;public function main() {
    byte a = 12;
    io:println(a);
    byte[] arr1 = [5, 24, 56, 243];
    byte[] arr2 = base16 `aeeecdefabcd12345567888822`;
    byte[] arr3 = base64 `aGVsbG8gYmFsbGVyaW5hICEhIQ==`;    io:println(arr1.length());
    io:println(arr1[1]);    io:println(arr2.length());
    io:println(arr2[2]);    io:println(arr3.length());
    io:println(arr3[3]);    byte[][] arr4 = [[1, 2, 3], [23, 45, 117, 213], [45, 3, 254, 65, 78, 99]];
    io:println(arr4.length());
    io:println(arr4[2].length());
}# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run byte_type.bal
12
4
24
13
205
19
108
3
6

Byte Type

The byte type is just shorthand for 0|1|2|…|255. Along with the general byte array there is also a special syntax to define base16 and base64 based arrays of bytes. This example demonstrates how byte and byte array can be used in Ballerina programs.

import ballerina/io;
public function main() {
    byte a = 12;
    io:println(a);

Define a byte variable.

    byte[] arr1 = [5, 24, 56, 243];

Create a byte array using the list constructor.

    byte[] arr2 = base16 `aeeecdefabcd12345567888822`;

Create a byte array using the base16 byte array literal.

    byte[] arr3 = base64 `aGVsbG8gYmFsbGVyaW5hICEhIQ==`;

Create a byte array using the base64 byte array literal.

    io:println(arr1.length());
    io:println(arr1[1]);
    io:println(arr2.length());
    io:println(arr2[2]);
    io:println(arr3.length());
    io:println(arr3[3]);
    byte[][] arr4 = [[1, 2, 3], [23, 45, 117, 213], [45, 3, 254, 65, 78, 99]];
    io:println(arr4.length());
    io:println(arr4[2].length());
}
# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run byte_type.bal
12
4
24
13
205
19
108
3
6