# How to convert a principal + subaccount into hexadecimal address (ICP token and ICRC-1)

**URL:** <https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052>\
**Category:** Motoko\
**Created:** [March 8, 2025, 8:14am UTC](https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052 "2025-03-08T08:14:50Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [March 8, 2025, 8:14am UTC](https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052/1 "2025-03-08T08:14:50Z")

</div>

Please, recommend a Motoko package to convert principal + subaccount into a hexadecimal account address.

I came up with [Mops • Motoko Package Manager](https://mops.one/account) but it uses a different address format than the NNS Wallet:

```auto
assert toText({ owner = Principal.fromText("2chl6-4hpzw-vqaaa-aaaaa-c") }) == "2chl6-4hpzw-vqaaa-aaaaa-c";

```

I am confused which address format to use: this or NNS wallet. I do want my users easily transfer from NNS Wallet to my app. So, I want hexadecimal. Please, help.

---

<div class="post-metadata">

**Author:** ![h1teshtr1path1](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/h1teshtr1path1/32/24662_2.png) [@h1teshtr1path1](https://forum.dfinity.org/u/h1teshtr1path1)\
**Post date:** [March 8, 2025, 5:42pm UTC](https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052/2 "2025-03-08T17:42:28Z")

</div>

> <https://github.com/BoomDAO/game-launcher/blob/staging/src/game_launcher_backend/utils/AccountIdentifier.mo>

I use this module to convert ICRC accounts to Account Identifier hex, it should work in your case as well.

You can find all imported modules in the same folder in repository.

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [March 9, 2025, 1:40am UTC](https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052/3 "2025-03-09T01:40:18Z")

</div>

OK. But is there a package MOPS module that does this?

---

<div class="post-metadata">

**Author:** ![ZenVoich](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/zenvoich/32/5969_2.png) [@ZenVoich](https://forum.dfinity.org/u/ZenVoich)\
**Post date:** [March 9, 2025, 8:12am UTC](https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052/4 "2025-03-09T08:12:32Z")

</div>

Probably [Principal.toLedgerAccount](https://mops.one/base/docs/Principal#toLedgerAccount) is what you need?

---

<div class="post-metadata">

**Author:** ![timo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/timo/32/5543_2.png) [@timo](https://forum.dfinity.org/u/timo)\
**Post date:** [March 9, 2025, 8:22am UTC](https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052/5 "2025-03-09T08:22:55Z")

</div>

> [@qwertytrewq](#):
>
> I am confused which address format to use: this or NNS wallet. I do want my users easily transfer from NNS Wallet to my app. So, I want hexadecimal. Please, help.

NNS wallet can send to principals as well as ICRC-1 accounts with subaccount (the bech32 encoded ones). It is not limited to legacy ICP accounts (the hex encoded ones).

I would suggest to show ICRC-1 accounts to the users instead of the hex accounts because then it is consistent for the users across all tokens (ICP and others).

Unless you want users to deposit directly from an exchange (CEX) to your app because most CEX still only understand the legacy hex accounts.

---

<div class="post-metadata">

**Author:** ![timo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/timo/32/5543_2.png) [@timo](https://forum.dfinity.org/u/timo)\
**Post date:** [March 9, 2025, 8:27am UTC](https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052/6 "2025-03-09T08:27:26Z")

</div>

> [@ZenVoich](#):
>
> Probably [Principal.toLedgerAccount](https://mops.one/base/docs/Principal#toLedgerAccount) is what you need?

Makes me think that a function `Principal.toICRC1Account` would also be useful.

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [March 9, 2025, 1:55pm UTC](https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052/7 "2025-03-09T13:55:17Z")

</div>

> [@timo](#):
>
> I would suggest to show ICRC-1 accounts to the users instead of the hex accounts because then it is consistent for the users across all tokens (ICP and others).

I think, this is impossible, because I am going to use subaccounts. Is there a format (except that “hex” one) for a principal with a subaccount?

---

<div class="post-metadata">

**Author:** ![sea-snake](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/sea-snake/32/7243_2.png) [@sea-snake](https://forum.dfinity.org/u/sea-snake)\
**Post date:** [March 9, 2025, 2:23pm UTC](https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052/8 "2025-03-09T14:23:49Z")

</div>

See the docs: [ICRC-1 standard specification | Internet Computer](https://internetcomputer.org/docs/references/icrc1-standard#non-default-accounts)

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [March 9, 2025, 2:45pm UTC](https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052/9 "2025-03-09T14:45:40Z")

</div>

The thread has the history of it if you want some light reading [ICRC-1 Account Human Readable Format - #91 by skilesare](https://forum.dfinity.org/t/icrc-1-account-human-readable-format/14682/91)

---

<div class="post-metadata">

**Author:** ![timo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/timo/32/5543_2.png) [@timo](https://forum.dfinity.org/u/timo)\
**Post date:** [March 11, 2025, 10:26am UTC](https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052/10 "2025-03-11T10:26:37Z")

</div>

What’s your overall setup and goal? Do you have a backend canister that takes deposits from users and issues an individual deposit address to each user? Do you do that for ICP only or also for other tokens?

And does your app consist of frontand and backend?

Can users withdraw as well?

Just wondering what is the total functionality that you need in the end.

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [March 11, 2025, 12:14pm UTC](https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052/11 "2025-03-11T12:14:41Z")

</div>

Yes to all questions.

---

<div class="post-metadata">

**Author:** ![timo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/timo/32/5543_2.png) [@timo](https://forum.dfinity.org/u/timo)\
**Post date:** [March 11, 2025, 4:07pm UTC](https://forum.dfinity.org/t/how-to-convert-a-principal-subaccount-into-hexadecimal-address-icp-token-and-icrc-1/42052/12 "2025-03-11T16:07:15Z")

</div>

Since I have had the exact same problem and probably many other people have to, we have developed a Motoko package for this. Not on mops yet, but in the process of getting there. The package is a class that you use in your canister that issues individual deposit accounts for users, handles all user deposits, consolidates them into a main account, and processes withdrawals from the main account, tracks user credit in the process. It has some advanced features to reduce latency for the user so that the credit can be used as quickly as possible.

It also supports deposits by allowance for users who prefer that.

The package is used in DailyBid ([https://alpha.daily-bid.com/](https://alpha.daily-bid.com/)). You can try it out there. You don’t need to trade, you can just deposit and withdraw any token to see how the flow works. See this post for an explanation: [How to Make Deposits and Withdrawals on the DailyBid Exchange](https://motokodefi.substack.com/p/how-to-make-deposits-and-withdrawals)

If it is of interest to you to use this then let me know.

The package is related to the icrc84 standard proposal and is supposed to be the first implementation of it.
