# Convert u64 to String for concatenation

**URL:** <https://forum.fuel.network/t/convert-u64-to-string-for-concatenation/5254>\
**Category:** Sway\
**Created:** [May 7, 2024, 11:12pm UTC](https://forum.fuel.network/t/convert-u64-to-string-for-concatenation/5254 "2024-05-07T23:12:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serafim](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@Serafim](https://forum.fuel.network/u/Serafim)\
**Post date:** [May 7, 2024, 11:12pm UTC](https://forum.fuel.network/t/convert-u64-to-string-for-concatenation/5254/1 "2024-05-07T23:12:48Z")

</div>

Hello team,

I have a following scenario:

```auto
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;
    }
}

pub fn concat_str_with_num(s: String, num: u64) -> 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, num.to_le_bytes()); // [link to the lib](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/bytes_conversions/u64.sw) 
    String::from_ascii(result)
}

```

The problem is: when I try to create a Unit test like the following:

```auto
#[test]
fn test_concat_str_with_num() {
    log(concat_str_with_num(String::from_ascii_str("test"), 1));
    assert(concat_str_with_num(String::from_ascii_str("test"), 1) == String::from_ascii_str("test$1"));
}

```

tests fail all the time regardless of what endian type I use in the conversion → to\_be\_bytes() also leads to the same failure of the test.

Could you please tell me if there is a way to concatenate strings like that or where I might be mistaken.

Also I have a follow-up question to this (might create another topic for that): is there any way of how one can display a pointer-based data type in the logs? Currently for any pointer type the response looks like this:

```auto
{
    "LogData": {
      "data": "0000000003ffffd20000000000000010000000000000000f",
      "digest": "f0bc62614581cb6b44520055e084416ca3359a15efd3ee394fd83e6f3edb7d92",
      "id": "0000000000000000000000000000000000000000000000000000000000000000",
      "is": 10336,
      "len": 24,
      "pc": 299280,
      "ptr": 308016,
      "ra": 0,
      "rb": 18
    }
  }

```

Thank you!

---

<div class="post-metadata">

**Author:** ![Serafim](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@Serafim](https://forum.fuel.network/u/Serafim)\
**Post date:** [May 9, 2024, 6:45pm UTC](https://forum.fuel.network/t/convert-u64-to-string-for-concatenation/5254/2 "2024-05-09T18:45:50Z")

</div>

EDIT/SOLUTION: the to\_le\_bytes() returns 8 bytes for num.to\_le\_bytes() which is totally fine and thus it might concatenate 8 symbols to the string which is not what we want.  
I tried several available built-in methods without any luck and came up with my own solution which works but might be unoptimal/not super efficient:

```auto
fn convert_num_to_ascii_bytes(num: u64) -> 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;
}

```

Then we can use it like that:

```auto
pub fn concat_str_with_num(s: String, num: u64) -> 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, num.convert_num_to_ascii_bytes()); 
    String::from_ascii(result)
}

```

---

<div class="post-metadata">

**Author:** ![calldelegation](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/calldelegation/32/1484_2.png) [@calldelegation](https://forum.fuel.network/u/calldelegation)\
**Post date:** [May 9, 2024, 9:46pm UTC](https://forum.fuel.network/t/convert-u64-to-string-for-concatenation/5254/3 "2024-05-09T21:46:54Z")

</div>

Hey @Serafim unfortunately there is currently [no directly support for string concatenation](https://fuellabs.github.io/sway/master/std/string/struct.String.html) as it would not conform to the UTF-8 format. The Sway team is aware of the short comings of strings but have been grinding other higher priority features for our upcoming devnet/mainnet. Rest assured that it is accounted for on the roadmap. I appreciate your patience here @Serafim 🙇‍♂️

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/2X/5/57d5a345cc15a64b636e0d56e042857f8a0e80b1.png) [@system](https://forum.fuel.network/u/system)\
**Post date:** [May 30, 2024, 5:47pm UTC](https://forum.fuel.network/t/convert-u64-to-string-for-concatenation/5254/4 "2024-05-30T17:47:36Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
