# Convert u256 to string

**URL:** <https://forum.fuel.network/t/convert-u256-to-string/6059>\
**Category:** Sway\
**Created:** [June 26, 2024, 2:12pm UTC](https://forum.fuel.network/t/convert-u256-to-string/6059 "2024-06-26T14:12:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lokesh-lync](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/lokesh-lync/32/2902_2.png) [@lokesh-lync](https://forum.fuel.network/u/lokesh-lync)\
**Post date:** [June 26, 2024, 2:12pm UTC](https://forum.fuel.network/t/convert-u256-to-string/6059/1 "2024-06-26T14:12:44Z")

</div>

How to convert `u256` to string in sway?

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/nick/32/2330_2.png) [@nick](https://forum.fuel.network/u/nick)\
**Post date:** [June 26, 2024, 2:17pm UTC](https://forum.fuel.network/t/convert-u256-to-string/6059/2 "2024-06-26T14:17:54Z")

</div>

Hi @lokesh-lync ,

These docs may be useful for you:

> **[Sway | Fuel Docs](https://docs.fuel.network/docs/sway/basics/converting_types/#convert-to-u256)**
>
> Official documentation for the Sway language

> **[Sway | Fuel Docs](https://docs.fuel.network/docs/sway/basics/converting_types/#convert-to-bytes)**
>
> Official documentation for the Sway language

> **[Sway | Fuel Docs](https://docs.fuel.network/docs/sway/basics/converting_types/#string-conversions)**
>
> Official documentation for the Sway language

---

<div class="post-metadata">

**Author:** ![lokesh-lync](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/lokesh-lync/32/2902_2.png) [@lokesh-lync](https://forum.fuel.network/u/lokesh-lync)\
**Post date:** [June 27, 2024, 5:29am UTC](https://forum.fuel.network/t/convert-u256-to-string/6059/3 "2024-06-27T05:29:51Z")

</div>

hey @nick I am trying to concat a string and s u256. I was trying to do this: [Convert u64 to String for concatenation - #2 by Serafim](https://forum.fuel.network/t/convert-u64-to-string-for-concatenation/5254/2)

This is the relevent code that I wrote:

```auto
pub fn concat_str_with_num(s: String, num: u256) -> String {
    let mut result = Bytes::new();
    push_bytes(result, s.as_bytes());
    push_bytes(result, String::from_ascii_str("/").as_bytes()); // an arbitrary delimeter
    push_bytes(result, convert_num_to_ascii_bytes(num));
    String::from_ascii(result)
}

fn convert_num_to_ascii_bytes(num: u256) -> Bytes {
    let mut bytes = Bytes::new();
    let mut n = num;
    if n == 0 {
        bytes.push(48);
        return bytes;
    }
    while n != 0 {
        // 48 - is an ASCII offset for digits
        bytes.push(((n % 10) + 48).try_as_u8().unwrap());
        n /= 10;
    }
    let mut reversed_bytes = Bytes::with_capacity(bytes.len());
    while !bytes.is_empty() {
        reversed_bytes.push(bytes.pop().unwrap());
    }
    return reversed_bytes;
}

fn push_bytes(ref mut a: Bytes, b: Bytes) {
    let mut i = 0;
    while i < b.len() {
        a.push(b.get(i).unwrap());
        i = i + 1;
    }
}

```

I am getting this error:

```plaintext
    |
188 |
189 | // 48 - is an ASCII offset for digits
190 | bytes.push(((n % 10) + 48).try_as_u8().unwrap());
    | ^^^^^^^^^ No method named "try_as_u8" found for type "u256".
191 | n /= 10;
192 | }
    |
____

```

Is there some import that I am missing?

Here are my imports:

```auto
use std::primitive_conversions::u256::*;
use sway_libs::{
       ownership::{
        _owner,
        initialize_ownership,
        only_owner,
    },
    asset::metadata::*,
    reentrancy::*
};

use standards::{
    src5::{SRC5, State, AccessError},
    src20::SRC20,
    src7::{SRC7, Metadata}
};

use std::{
    asset::{
        burn,
        mint_to,
        transfer,
    },
    call_frames::{
        msg_asset_id,
    },
    constants::DEFAULT_SUB_ID,
    context::msg_amount,
    string::String,
    hash::Hash,
    storage::storage_map::StorageMap,
    storage::storage_string::StorageString,
    bytes::*
};

```

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/nick/32/2330_2.png) [@nick](https://forum.fuel.network/u/nick)\
**Post date:** [June 27, 2024, 10:38am UTC](https://forum.fuel.network/t/convert-u256-to-string/6059/4 "2024-06-27T10:38:58Z")

</div>

By the looks of your code @lokesh-lync you are trying to convert a u256 number into a u8, this downcasting is not possible or likely not what you want (as you would lose 248 bits of precision here on the number).

The u256 primitive only has a as\_b256 conversion:

> **[u256 in core - Sway](https://fuellabs.github.io/sway/master/core/primitive.u256.html#method.as_b256)**
>
> API documentation for the Sway \`u256\` primitive in \`core\`.

Do you want this number to be a u256 `((n % 10) + 48)`?

Or is this suppose to be a single byte/u8? If so, you should instantiate this as a single u8 then add it to the bytes array.

---

<div class="post-metadata">

**Author:** ![lokesh-lync](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/lokesh-lync/32/2902_2.png) [@lokesh-lync](https://forum.fuel.network/u/lokesh-lync)\
**Post date:** [June 27, 2024, 11:59am UTC](https://forum.fuel.network/t/convert-u256-to-string/6059/5 "2024-06-27T11:59:11Z")

</div>

what I am trying to do is something similar to this: [Convert u64 to String for concatenation - #2 by Serafim](https://forum.fuel.network/t/convert-u64-to-string-for-concatenation/5254/2)

I am using the same code, the difference is that this guy is converting `u64` to String and I am trying to convert `u256` to String. Wouldn’t he loose some bits of precision as well? But it seems to be working there. So, I am not sure why is it not working for u256

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/nick/32/2330_2.png) [@nick](https://forum.fuel.network/u/nick)\
**Post date:** [June 27, 2024, 12:35pm UTC](https://forum.fuel.network/t/convert-u256-to-string/6059/6 "2024-06-27T12:35:20Z")

</div>

HI @lokesh-lync ,

If you want to convert a u256 to a string I’d do the following:

1. convert the u256 to a b256  
[u256 in core - Sway](https://fuellabs.github.io/sway/master/core/primitive.u256.html#method.as_b256)
2. convert the b256 to a bytes  
[Sway | Fuel Docs](https://docs.fuel.network/docs/sway/basics/converting_types/#convert-to-a-byte-array)
3. convert the bytes to a String type [String in string - Sway](https://fuellabs.github.io/sway/master/std/string/struct.String.html#method.from_ascii)

---

<div class="post-metadata">

**Author:** ![lokesh-lync](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/lokesh-lync/32/2902_2.png) [@lokesh-lync](https://forum.fuel.network/u/lokesh-lync)\
**Post date:** [June 27, 2024, 12:37pm UTC](https://forum.fuel.network/t/convert-u256-to-string/6059/7 "2024-06-27T12:37:38Z")

</div>

ok i’ll give this a try and let you know. Thanks!

---

<div class="post-metadata">

**Author:** ![lokesh-lync](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/lokesh-lync/32/2902_2.png) [@lokesh-lync](https://forum.fuel.network/u/lokesh-lync)\
**Post date:** [June 27, 2024, 6:50pm UTC](https://forum.fuel.network/t/convert-u256-to-string/6059/8 "2024-06-27T18:50:54Z")

</div>

hey @calldelegation you suggested me to do something similar earlier here: [Problems with signature verification on chain](https://forum.fuel.network/t/problems-with-signature-verification-on-chain/5389)

can you also look into this and suggest me where I am going wrong?

---

<div class="post-metadata">

**Author:** ![lokesh-lync](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/lokesh-lync/32/2902_2.png) [@lokesh-lync](https://forum.fuel.network/u/lokesh-lync)\
**Post date:** [July 1, 2024, 7:44am UTC](https://forum.fuel.network/t/convert-u256-to-string/6059/9 "2024-07-01T07:44:38Z")

</div>

UPDATE: I was able to make u64 work. u256 does not have some methods that I wanted to use. I am going with u64 for now.
