encode static method
List<int>
encode(
- String value, {
- StringEncoding type = StringEncoding.utf8,
- bool validateB64Padding = true,
- bool allowUrlSafe = true,
- Base58Alphabets base58alphabets = Base58Alphabets.bitcoin,
Encodes the given value string into a list of bytes using the specified type.
The type parameter determines the encoding type to use, with UTF-8 being the default.
Returns a list of bytes representing the encoded string.
Implementation
static List<int> encode(
String value, {
StringEncoding type = StringEncoding.utf8,
bool validateB64Padding = true,
bool allowUrlSafe = true,
Base58Alphabets base58alphabets = Base58Alphabets.bitcoin,
}) {
try {
switch (type) {
case StringEncoding.utf8:
final bytes = UTF8Encoder.encode(value);
assert(BytesUtils.bytesEqual(bytes, utf8.encode(value)));
return bytes;
case StringEncoding.base64:
case StringEncoding.base64UrlSafe:
return B64Decoder.decode(
value,
validatePadding: validateB64Padding,
urlSafe: allowUrlSafe,
);
case StringEncoding.base58:
return Base58Decoder.decode(value, base58alphabets);
case StringEncoding.base58Check:
return Base58Decoder.checkDecode(value, base58alphabets);
case StringEncoding.hex:
return BytesUtils.fromHexString(value);
case StringEncoding.ascii:
final encode = ASCIIEncoder.encode(value);
assert(BytesUtils.bytesEqual(encode, ascii.encode(value)));
return encode;
}
} catch (e) {
throw ArgumentException.invalidOperationArguments(
"encode",
name: "value",
reason: "Failed to encode strong to ${type.name} bytes",
);
}
}