> ## 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 an address to other types

> Convert an address to other types

The `Address` is an opaque type that represents either a 'default' externally owned account on the Stellar network, or a contract (that may also provide logic for custom externally owned accounts, see [authorization docs](https://developers.stellar.org/docs/learn/fundamentals/contract-development/authorization.md#account-abstraction) for details). For the smart contracts it normally doesn't matter which kind of `Address` is used. However, in some contexts it's useful to convert `Address` to/from different data types, such as string or XDR. The conversions have distinctly different purpose depending on whether they happen in the smart contract itself, or in the client code.

## String conversions

The default string format for `Address` is a so called 'string key' (or 'strkey'), defined fully in [SEP-23](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0023.md). For the default externally owned accounts strkey always starts with `G` and for all the contracts it starts with `C`.

### String Conversions in Client SDKs

Outside of the smart contracts, it is convenient to represent `Address` as string most of the time. It can be stored in string serialization formats such as JSON and XML. Storing addresses as strings in databases can simplify database schema design and queries. Strings are easier to manipulate and are more compatible with user interfaces and APIs. Thus

```js JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
const StellarSdk = require("@stellar/stellar-sdk");

// Example Stellar address
const stellarAddress =
  "GCM5WPR4DDR24FSAX5LIEM4J7AI3KOWJYANSXEPKYXCSZOTAYXE75AFN";
// Create an Address object from string
const address = new StellarSdk.Address(stellarAddress);
// Convert the address back to string
const addressToString = address.toString();
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from stellar_sdk import Address

# Example Stellar address
stellar_address = "GCM5WPR4DDR24FSAX5LIEM4J7AI3KOWJYANSXEPKYXCSZOTAYXE75AFN"
# Create an Address object from string
address = Address(stellar_address)
# Convert the address back to string
address_to_string = address.address
```

### String Conversions in Smart Contracts

It's generally preferred for contracts to operate directly on the `Address` type. String conversions may be useful for specialized use cases, such as passing the Stellar `Address`es to/from other chains.

```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
use soroban_sdk::{Address, String, Env};

pub fn address_to_string(address: Address) -> String {
    address.to_string()
}

pub fn address_from_string(strkey: String) -> Address {
    Address::from_string(&strkey)
}
```

`Address` can also be built from a string literal, which may be useful for testing.

```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
let test_address = Address::from_str(
    &env,
    "GCM5WPR4DDR24FSAX5LIEM4J7AI3KOWJYANSXEPKYXCSZOTAYXE75AFN",
);
```

## XDR conversions

XDR is schema-based binary serialization format used by the Stellar network. It is used for all the Stellar blockchain interactions, such as building the transactions, storing data in the ledger, communicating the transaction results etc. Stellar SDKs provide the typed wrappers for all the Stellar XDR data types. Address is represented as `ScAddress` type, which can then be wrapped into `ScVal` which is a type that represents any contract type supported by Stellar contracts.

### XDR Conversions in Client SDKs

On the client side XDR conversions are useful to build the transactions and process the transaction results.

```js JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
// Example Stellar address
const stellarAddress =
  "GCM5WPR4DDR24FSAX5LIEM4J7AI3KOWJYANSXEPKYXCSZOTAYXE75AFN";
// Create an Address object from string
const address = new StellarSdk.Address(stellarAddress);
// Convert the Address to xdr.ScVal
const scVal = address.toScVal();
// Convert scVal structure to the binary format
const scValBuffer = scVal.toXDR("raw");
// Convert the Address to xdr.ScAddress
const scAddress = address.toScAddress();
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from stellar_sdk import Address

# Example Stellar address
stellar_address = 'GBJCHUKZMTFSLOMNC7P4TS4VJJBTCYL3XKSOLXAUJSD56C4LHND5TWUC'
# Create an Address object
address = Address(stellar_address)
# Convert the Address object to an ScAddress
sc_address_xdr = address.to_xdr_sc_address()
```

### XDR Conversions in Smart Contracts

Smart contracts don't need to explicitly interact with the XDR types, as all the smart contract data types are automatically converted to XDR by the smart contract runtime. Every contract type, including `Address`, can be serialized to XDR bytes. This conversion is useful, for example, for performing hashing in smart contracts. It is also possible to convert the serialized XDR bytes back to contract types, which can be useful in some narrow use cases, such as custom authentication schemes.

Note, that XDR conversions are an advanced feature and are not necessary for most Stellar smart contracts.

Deserializing XDR bytes back into a contract type can fail in two ways. If the bytes hold valid XDR for a value of a different type, the conversion returns an error that the contract can handle. If the bytes are not valid XDR for any value at all, the conversion panics, and the SDK offers no in-contract recovery for that case. Contracts that receive XDR bytes from untrusted sources, such as a custom authentication scheme, should handle the error case and validate the input before relying on it: prefer typed arguments such as `Address` where possible, or verify the byte format in the client that submits the transaction.

```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
use soroban_sdk::{
    xdr::{FromXdr, ToXdr},
    Address, Bytes, ConversionError, Env,
};

pub fn address_to_xdr_bytes(env: Env, address: Address) -> Bytes {
    address.to_xdr(&env)
}

pub fn address_from_xdr_bytes(env: Env, bytes: Bytes) -> Result<Address, ConversionError> {
    Address::from_xdr(&env, &bytes)
}
```
