ballerina/encoding module
Module overview
This module provides the necessary utilities that are required to encode and decode content using different encoding mechanisms and algorithms.
Encoding byte array to hex string
The encodeHex
function encodes provided byte array to an hex string
.
Decoding hex string to byte array
The decodeHex
function decodes a hex encoded string
to a byte array.
Encoding byte array to base64 string
The encodeBase64
function encodes provided byte array to an base64 string
.
Decoding base64 string to byte array
The decodeBase64
function decodes a base64 encoded string
to a byte array.
Encoding byte array into a string
The byteArrayToString
function can be used to encode a byte array into a string using a provided charset.
Samples
import ballerina/encoding;
import ballerina/io;
public function main() returns error? {
string charEncoding = "UTF-8";
// The string content to be hashed.
string text = "Hello Ballerina";
byte[] inputByteArr = input.toByteArray(charEncoding);
string output = encoding:encodeHex(inputByteArr)
io:println("Hex encoded string : " + output);
// Hex encoded string, decoded back into a byte array.
inputByteArr = check encoding:decodeHex(output)
output = encoding:encodeBase64(inputByteArr)
io:println("Base64 encoded string : " + output);
// Base64 encoded string, decoded back into a byte array.
inputByteArr = check encoding:decodeBase64(output)
// Convert byte array into a string.
string finalString = encoding:byteArrayToString(inputByteArr);
}
Records Summary
Record | Description | ||
---|---|---|---|
EncodingError | Error relevant to encoding operations. |
Functions Summary
Return Type | Function and Description | ||
---|---|---|---|
string | byteArrayToString(byte[] content, string encoding) Converts given byte array to a string. |
||
byte[]|error<> | decodeBase64(string input) Decode Base64 encoded |
||
byte[]|error<> | decodeBase64Url(string input) Decode Base64 URL encoded |
||
byte[]|error<> | decodeHex(string input) Decode Hex encoded |
||
string | encodeBase64(byte[] input) Returns the Base64 encoded |
||
string | encodeBase64Url(byte[] input) Returns the Base64 URL encoded |
||
string | encodeHex(byte[] input) Returns the Hex encoded |
public type EncodingError record
Error relevant to encoding operations.
Field Name | Data Type | Default Value | Description |
---|---|---|---|
message | string | Error message |
public function byteArrayToString(byte[] content, string encoding) returns (string)
Converts given byte array to a string.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
content | byte[] | Byte array content to be converted |
|
encoding | string | utf-8 | Encoding to used in byte array conversion to string |
Return Type | Description | ||
---|---|---|---|
string | String representation of the given byte array |
public function decodeBase64(string input) returns (byte[]|error<>)
Decode Base64 encoded string
into byte array.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
input | string | Value to be decoded |
Return Type | Description | ||
---|---|---|---|
byte[]|error<> | Decoded output or error if input is not a valid Base64 value |
public function decodeBase64Url(string input) returns (byte[]|error<>)
Decode Base64 URL encoded string
into byte array.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
input | string | Value to be decoded |
Return Type | Description | ||
---|---|---|---|
byte[]|error<> | Decoded output or error if input is not a valid Base64 URL encoded value |
public function decodeHex(string input) returns (byte[]|error<>)
Decode Hex encoded string
into byte array.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
input | string | Value to be decoded |
Return Type | Description | ||
---|---|---|---|
byte[]|error<> | Decoded output or error if input is not a valid Hex value |
public function encodeBase64(byte[] input) returns (string)
Returns the Base64 encoded string
value of the given byte array.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
input | byte[] | Value to be encoded |
Return Type | Description | ||
---|---|---|---|
string | Encoded output |