> ## Documentation Index
> Fetch the complete documentation index at: https://developers.stellar.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Convert from bytes to other types

> 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 Rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
use soroban_sdk::{Address, Bytes};

pub fn bytes_to_address(bytes: Bytes) -> Address {
    Address::from_string_bytes(&bytes)
}
```

```js JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
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();
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
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 Rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
use soroban_sdk::{String, Bytes};

pub fn bytes_to_string(bytes: Bytes) -> String {
    String::from(bytes)
}
```

```js JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
// Example bytes value
const rawBytes = Buffer.from(
  "99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe",
  "hex",
);
// Convert bytes to string
const bytesToString = rawBytes.toString("hex");
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
# Example bytes value
raw_bytes = bytes.fromhex("99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe")
# Convert bytes to string
bytes_to_string = raw_bytes.hex()
```

## 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 Rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
use soroban_sdk::{Bytes, Val};

pub fn bytes_to_val(bytes: Bytes) -> Val {
    Val::from(bytes)
}
```

```js JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
// Example bytes value
const rawBytes = Buffer.from(
  "99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe",
  "hex",
);
// Convert bytes to xdr.ScVal
const bytesToScVal = StellarSdk.nativeToScVal(rawBytes);
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
# Example bytes value
raw_bytes = bytes.fromhex("99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe")
# Convert bytes to ScVal
sc_val = stellar_sdk.scval.to_bytes(raw_bytes)
```

## BytesN to Fixed-Size Array

`BytesN<N>` is a fixed-length byte buffer used for values like hashes and public keys. It can be converted to a native Rust `[u8; N]` array using the `to_array()` method or the standard `Into` trait.

```rust Rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
use soroban_sdk::BytesN;

// Convert by value using to_array()
pub fn bytesn_to_array(b: BytesN<32>) -> [u8; 32] {
    b.to_array()
}

// Convert by value using Into
pub fn bytesn_into_array(b: BytesN<32>) -> [u8; 32] {
    b.into()
}

// Convert by reference (original BytesN is not consumed)
pub fn bytesn_ref_to_array(b: &BytesN<32>) -> [u8; 32] {
    b.into()
}
```
