# Converting u64 to u256 and vice versa

**URL:** <https://forum.fuel.network/t/converting-u64-to-u256-and-vice-versa/3926>\
**Category:** Sway\
**Created:** [January 2, 2024, 7:50am UTC](https://forum.fuel.network/t/converting-u64-to-u256-and-vice-versa/3926 "2024-01-02T07:50:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nambi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/nambi/32/2011_2.png) [@Nambi](https://forum.fuel.network/u/Nambi)\
**Post date:** [January 2, 2024, 7:50am UTC](https://forum.fuel.network/t/converting-u64-to-u256-and-vice-versa/3926/1 "2024-01-02T07:50:07Z")

</div>

I’m looking to convert u64 to u256 and vice versa for the Fraction library. I’m unable to find the documentation and reverse engineering the core libs isn’t working out. I was wondering if someone could share the steps needed for this conversion. Thanks

---

<div class="post-metadata">

**Author:** ![SwayStar123](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/swaystar123/32/510_2.png) [@SwayStar123](https://forum.fuel.network/u/SwayStar123)\
**Post date:** [January 2, 2024, 1:41pm UTC](https://forum.fuel.network/t/converting-u64-to-u256-and-vice-versa/3926/2 "2024-01-02T13:41:50Z")

</div>

If 10 is your number for example

```auto
10_u64.as_u256()

```

This pattern should work for all unsigned integers for converting between the current size and a bigger size. Aka u8 → u16 would be

```auto
10_u8.as_u16()

```

You can see the underlying code here

> <https://github.com/FuelLabs/sway/blob/master/sway-lib-core/src/primitive_conversions.sw>

---

<div class="post-metadata">

**Author:** ![Nambi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/nambi/32/2011_2.png) [@Nambi](https://forum.fuel.network/u/Nambi)\
**Post date:** [January 3, 2024, 8:15am UTC](https://forum.fuel.network/t/converting-u64-to-u256-and-vice-versa/3926/3 "2024-01-03T08:15:48Z")

</div>

Thanks. Now I can convert u64 to u256. But I also need the reverse conversion. I’ve tried

1. x.try\_into().unwrap()
2. x.as\_u64()
3. x.to\_u64()
4. x.d
5. let (a, b, c, d) = x

Where x is of the type u256 but nothing worked.

---

<div class="post-metadata">

**Author:** ![SwayStar123](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/swaystar123/32/510_2.png) [@SwayStar123](https://forum.fuel.network/u/SwayStar123)\
**Post date:** [January 4, 2024, 6:20am UTC](https://forum.fuel.network/t/converting-u64-to-u256-and-vice-versa/3926/4 "2024-01-04T06:20:04Z")

</div>

As the reverse conversions are lossy, the standard library offers methods that return an option.

```auto
u64::try_from(x)

```

This will return an option, you can match on it or unwrap it.

> <https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/primitive_conversions/u64.sw>

---

<div class="post-metadata">

**Author:** ![Nambi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/nambi/32/2011_2.png) [@Nambi](https://forum.fuel.network/u/Nambi)\
**Post date:** [January 4, 2024, 8:23am UTC](https://forum.fuel.network/t/converting-u64-to-u256-and-vice-versa/3926/5 "2024-01-04T08:23:22Z")

</div>

Doesn’t work for me. Maybe I did something wrong. I’m trying:

`let np = u64::try_from((an*bn*factor.as_u256())/(ad*bd)).unwrap();`

where all the variables used in the middle are of the nature u256. The error message is:  
_ **^^^^^^^^ No method named “try\_from” found for type “u64”** _

I haven’t imported u64 at the top of the file after library; because I don’t believe we have to import primitive types. Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![Nambi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/nambi/32/2011_2.png) [@Nambi](https://forum.fuel.network/u/Nambi)\
**Post date:** [January 4, 2024, 8:32am UTC](https://forum.fuel.network/t/converting-u64-to-u256-and-vice-versa/3926/6 "2024-01-04T08:32:22Z")

</div>

[Here](https://github.com/harshnambiar/fraction_sway/blob/main/src/lib.sw#L133) is the concerned erroring line (ignore the other errors, they only appear when this function isn’t commented out).

---

<div class="post-metadata">

**Author:** ![SwayStar123](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/swaystar123/32/510_2.png) [@SwayStar123](https://forum.fuel.network/u/SwayStar123)\
**Post date:** [January 5, 2024, 5:56am UTC](https://forum.fuel.network/t/converting-u64-to-u256-and-vice-versa/3926/7 "2024-01-05T05:56:16Z")

</div>

> [@Nambi](#):
>
> I haven’t imported u64 at the top of the file after library; because I don’t believe we have to import primitive types. Am I doing something wrong?

You dont have to import the type but you do have to import the methods, as they are not imported by default.

```auto
use std::primitive_conversions::u64::*;

```

Should work

---

<div class="post-metadata">

**Author:** ![Nambi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/nambi/32/2011_2.png) [@Nambi](https://forum.fuel.network/u/Nambi)\
**Post date:** [January 5, 2024, 7:39am UTC](https://forum.fuel.network/t/converting-u64-to-u256-and-vice-versa/3926/8 "2024-01-05T07:39:55Z")

</div>

After some changes in addition to that, this error went away. Maybe we could somehow add this to the official documentation?

---

<div class="post-metadata">

**Author:** ![sandusky](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sandusky/32/1313_2.png) [@sandusky](https://forum.fuel.network/u/sandusky)\
**Post date:** [January 26, 2024, 5:59am UTC](https://forum.fuel.network/t/converting-u64-to-u256-and-vice-versa/3926/9 "2024-01-26T05:59:01Z")

</div>

Hi there 🌴  
I’m adding this documentation request to the backlog  
thanks for your report.
