Convert from bytes to other types
Bytes is a contiguous growable array type containing u8s. They may represent various types of data including strings, addresses, or other information. Converting any data type to bytes ensures that the data be consistently handled by the Soroban runtime and interacting systems.
Bytes to Addressโ
When retrieving data stored on the blockchain, addresses might be stored in byte representation for compactness and efficiency. Off-chain systems, such as APIs, databases, or user interfaces, usually expect addresses in a human-readable format. In such cases, you need to convert the bytes to an address format to ensure compatibility.
- Rust
- JS
- Python
use soroban_sdk::{Address, Bytes, Env};
pub fn bytes_to_address(bytes: Bytes) -> Address {
Address::from_string_bytes(&bytes)
}
const StellarSdk = require('@stellar/stellar-sdk');
// Example bytes value
const rawBytes = Buffer.from('99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe', 'hex');
// Convert bytes to an account Address
const addressFromBytes = StellarSdk.Address.account(rawBytes);
addressFromBytes.toString();
// Convert from bytes to a string address
const addressFromBytes = StellarSdk.Address.contract(rawBytes);
addressFromBytes.toString();
from stellar_sdk.address import Address
# Example bytes value
raw_bytes = bytes.fromhex('99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe')
bytes_to_address = Address.from_raw_account(raw_bytes)
Bytes to Stringโ
When dealing with binary data, you may need to convert certain portions of the data to a human-readable format like strings for logging, debugging, processing or display.
- Rust
- JS
- Python
use soroban_sdk::{String, Bytes, Env, FromVal};
pub fn bytes_to_string(env: &Env, bytes: Bytes) -> String {
String::from_val(env, &bytes.to_val())
}
const StellarSdk = require('@stellar/stellar-sdk');
// Example bytes value
const rawBytes = Buffer.from('99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe', 'hex');
// Convert bytes to string
const bytesToString = rawBytes.toString('hex');
from stellar_sdk.address import Address
# Example bytes
raw_bytes = b'99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe'
# Convert bytes to string
bytes_to_string = raw_bytes.decode('utf-8')
Bytes to ScValโ
In a Soroban smart contract that interacts with an external oracle service to provide price data in raw byte format, you would need to convert the bytes to ScVal to process and manipulate the data within your contract.
- Rust
- JS
- Python
use soroban_sdk::{Bytes, Env, FromVal, Val};
pub fn bytes_to_val(env: &Env, bytes: Bytes) -> Val {
Val::from_val(env, &bytes.to_val())
}
const StellarSdk = require('@stellar/stellar-sdk');
// Example bytes value
const rawBytes = Buffer.from('99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe', 'hex');
// Convert bytes to xdr.ScVal
const bytesToScVal = StellarSdk.xdr.ScVal.scvBytes(rawBytes);
import stellar_sdk
# Example bytes value
raw_bytes = b'example_bytes_data'
# Convert bytes to ScVal
sc_val = stellar_sdk.scval.to_bytes(raw_bytes)
Guides in this category:
๐๏ธ Convert an address to other types
Convert an address to other types
๐๏ธ Convert from bytes to other types
Convert from bytes to other types
๐๏ธ Convert a ScVal to other type
Convert a ScVal to other type
๐๏ธ Convert a string to other types
Convert a string to other types