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

# Address Format Conversion

[Tutorial GitHub Repository](https://github.com/initia-labs/examples/tree/main/evm/address-conversion)

This tutorial demonstrates how to convert between an EVM hex address and a
Cosmos bech32 address.

## Prerequisites

* [NodeJS](https://nodejs.org)

## Project Setup

First, create a new directory for your project.

```sh theme={null}
mkdir address-conversion
cd address-conversion
```

Next, initialize the project and install the `bech32-converting` package.

```sh theme={null}
npm init
npm install bech32-converting
```

Then create a new file called `index.js` in the `src` directory where you will
write your code.

<Note>
  You can use any package that allows you to encode and decode bech32-prefixed
  addresses. However, the specific steps for using those packages may vary.
</Note>

## Development

For this tutorial, assume that you have an EVM address
`0x0901aeeD1d5144987DaE3D95AB219ff75e9bA636` that you want to convert to a
Cosmos address, and then convert it back to an EVM address.

First, import the `bech32-converting` package and initialize the variable for
the chain's Bech32 prefix.

```js src/index.js theme={null}
const converter = require('bech32-converting')
let bech32Address = 'init1pyq6amga29zfsldw8k26kgvl7a0fhf3kftflmr'
```

### EVM Address to Cosmos Address

To convert an EVM address to a Cosmos address, use the `bech32-converting`
package's `toHex` function.

```js src/index.js theme={null}
const hexAddress = converter('init').toHex(bech32Address)
console.log(hexAddress) // 0x0901aeeD1d5144987DaE3D95AB219ff75e9bA636
```

### Cosmos Address to EVM Address

To convert a Cosmos address to an EVM address, use the `bech32-converting`
package's `fromHex` function.

```js src/index.js theme={null}
bech32Address = converter('init').toBech32(hexAddress)
console.log(bech32Address) // init1pyq6amga29zfsldw8k26kgvl7a0fhf3kftflmr
```

The full code for the script is then as follows:

```js src/index.js theme={null}
const converter = require('bech32-converting')

let bech32Address = 'init1pyq6amga29zfsldw8k26kgvl7a0fhf3kftflmr'

const hexAddress = converter('init').toHex(bech32Address)
console.log(hexAddress) // 0x0901aeeD1d5144987DaE3D95AB219ff75e9bA636

bech32Address = converter('init').toBech32(hexAddress)
console.log(bech32Address) // init1pyq6amga29zfsldw8k26kgvl7a0fhf3kftflmr
```

If you run this script, you should see the following output:

```sh theme={null}
0x0901aeeD1d5144987DaE3D95AB219ff75e9bA636
init1pyq6amga29zfsldw8k26kgvl7a0fhf3kftflmr
```
