Swan Lake Beta2 is here.
public function main() {
    // Creates a `byte` array using the `base64` byte array literal.
    byte[] data = base64`yPHaytRgJPg+QjjylUHakEwz1fWPx/wXCW41JSmqYW8=`;

    // Creates an `int` using a hexadecimal numeral.
    int x = 0xDEADBEEF;

    // `byte & int` will be `byte`.
    byte b = x & 0xFF;

}

Binary Data

Binary data is represented by arrays of byte values. It is a special syntax for byte arrays in base 64 and base 16. The relationship between byte and int is not the same as what you are used to. A byte is an int in the range 0 to 0xFF. byte is a subtype of int. int type supports normal bitwise operators: & | ^ ~ << >> >>>. Ballerina knows the obvious rules about when bitwise operations produce a byte.

public function main() {
    byte[] data = base64`yPHaytRgJPg+QjjylUHakEwz1fWPx/wXCW41JSmqYW8=`;

Creates a byte array using the base64 byte array literal.

    int x = 0xDEADBEEF;

Creates an int using a hexadecimal numeral.

    byte b = x & 0xFF;

byte & int will be byte.

}
bal run binary_data.bal
public function main() {
    // Creates a `byte` array using the `base64` byte array literal.
    byte[] data = base64`yPHaytRgJPg+QjjylUHakEwz1fWPx/wXCW41JSmqYW8=`;

    // Creates an `int` using a hexadecimal numeral.
    int x = 0xDEADBEEF;

    // `byte & int` will be `byte`.
    byte b = x & 0xFF;

}

Binary Data

Binary data is represented by arrays of byte values. It is a special syntax for byte arrays in base 64 and base 16. The relationship between byte and int is not the same as what you are used to. A byte is an int in the range 0 to 0xFF. byte is a subtype of int. int type supports normal bitwise operators: & | ^ ~ << >> >>>. Ballerina knows the obvious rules about when bitwise operations produce a byte.

public function main() {
    byte[] data = base64`yPHaytRgJPg+QjjylUHakEwz1fWPx/wXCW41JSmqYW8=`;

Creates a byte array using the base64 byte array literal.

    int x = 0xDEADBEEF;

Creates an int using a hexadecimal numeral.

    byte b = x & 0xFF;

byte & int will be byte.

}
bal run binary_data.bal

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand