The underlying byte array. Can also be accessed through toByteArray
.
The underlying ArrayBuffer in case you need to manipulate it directly.
Most likely toUtf8String
or toHexString
is the better choice.
const input = Bytes.fromHexString("6574682d75736463");
Console.log(String.UTF8.decode(input.buffer);); // eth-usdc
The length of the underlying byte array.
const data = Bytes.fromHexString("6574682d75736463");
Console.log(data.length); // 8
Create a new Bytes instance which is a combination of the first instance and the argument.
the bytes to add to this instance
const part1 = Bytes.fromUtf8String("Hello, ");
const part2 = Bytes.fromUtf8String("world!");
const combined = part1.concat(part2);
Console.log(combined.toUtf8String()); // "Hello, world!"
Create a new Bytes instance from a slice of the original instance.
Inclusive beginning of the slice to copy
Exclusive end of the slice to copy
const data = Bytes.fromHexString("68656c6c6f2073656461");
// Without begin and end it returns a new copy
const copy = data.slice();
Console.log(copy.toUtf8String()); // "hello seda"
// Without end it starts the slice at begin and copies until the end
const noEnd = data.slice(6);
Console.log(noEnd.toUtf8String()); // "seda"
// Without begin and end it returns a new copy
const beginAndEnd = data.slice(1, 5);
Console.log(beginAndEnd.toUtf8String()); // "ello"
The underlying byte array in case you need to use it directly.
Can also be accessed through value
.
const data = Bytes.fromHexString("6574682d75736463");
const byteArray = data.toByteArray();
Console.log(byteArray.length); // 8
Attempts to deserialize the Bytes into the '@json' annotated class <T>
.
an instance of '@json' annotated class
@json
class Data {
id!: i64;
value!: string;
}
const data = Bytes.fromString("{"id":1,"value":"test"}");
const responseData = data.toJSON<Data>();
Console.log(responseData.value); // "test"
Convert the Bytes instance to the number Bytes.toU128
and Bytes.toU256
if the Bytes instance should be read as big-endian (defaults to little-endian)
const littleEndianBytes = Bytes.fromHexString("0x0a000000");
const num1 = littleEndianBytes.toNumber<u32>();
Console.log(num1); // Outputs: 10
const bigEndianBytes = Bytes.fromHexString("0x00000000000000f3");
const number = bigEndianBytes.toNumber<u64>(true);
Console.log(number); // Outputs: 243
Convert the Bytes instance to a u128 number
for smaller number types see Bytes.toNumber
for u256 see Bytes.toU256
if the Bytes instance should be read as big-endian (defaults to little-endian)
const littleEndianBytes = Bytes.fromHexString("0x0000000000000000000000003ade68b1");
const num1 = littleEndianBytes.toU128();
Console.log(num1); // Outputs: 235817861417383168075506718003194494976
const bigEndianBytes = Bytes.fromHexString("0x0000000000000000000000003ade68b1");
const number = bigEndianBytes.toU128(true);
Console.log(number); // Outputs: 987654321
Convert the Bytes instance to a u256 number
for smaller number types see Bytes.toNumber
for u128 see Bytes.toU128
if the Bytes instance should be read as big-endian (defaults to little-endian)
const littleEndianBytes = Bytes.fromHexString("0x00000000000000000000000000000000000000000000000000000000075bcd15");
const num1 = littleEndianBytes.toU256();
Console.log(num1); // Outputs: 9861401716165347554763518477098801055286775394839307868237211366843748450304
const bigEndianBytes = Bytes.fromHexString("0x00000000000000000000000000000000000000000000000000000000075bcd15");
const number = bigEndianBytes.toU256(true);
Console.log(number); // Outputs: 123456789
Decode this Bytes instance as a UTF-8 string.
const input = Bytes.fromHexString("6574682d75736463");
const pairsString = input.toUtf8String();
const pairsList = pairsString.split("-");
const a = pairsList[0];
const b = pairsList[1];
Console.log(a); // eth
Console.log(b); // usdc
Private
validateStatic
concatStatic
emptyStatic
fromStatic
fromDecode a hexidecimal string to Bytes. Ignores any '0x' prefix.
const data = Bytes.fromHexString("6574682d75736463");
// Equivalent to
const dataWithPrefix = Bytes.fromHexString("0x6574682d75736463");
Console.log(data.toUtf8String); // "eth-usdc"
Console.log(dataWithPrefix.toUtf8String); // "eth-usdc"
Static
fromJSONSerialises a '@json' annotated class (<T>
) as a string and returns the Bytes representation
of that string.
an instance of the '@json' annotated class
@json
class Data {
id!: i64;
value!: string;
}
const data = new Data();
data.id = 1;
data.value = "test";
Process.success(Bytes.fromJSON<Data>(data)); // Sets "{"id":1,"value":"test"}" in bytes as the result
Static
fromCreates a new Bytes instance from a number
the number you want to convert to a Bytes instance
if you want to store the number as big-endian. Defaults to little-endian
const number = Bytes.fromNumber<u32>(10);
Console.log(number.toHexString()); // Outputs: 0a000000
const bigEndianNumber = Bytes.fromNumber<u32>(10, true);
Console.log(number.toHexString()); // Outputs: 0000000a
Static
from
A class to make dealing with bytes easier. Allows to quickly convert between UTF-8, hexadecimal, and byte array representations of data.
Example