# How to concatenate integers?

**URL:** <https://users.rust-lang.org/t/how-to-concatenate-integers/142426>\
**Category:** uncategorized\
**Created:** [September 15, 2026, 9:43am UTC](https://users.rust-lang.org/t/how-to-concatenate-integers/142426 "2026-09-15T09:43:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![canina](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@canina](https://users.rust-lang.org/u/canina)\
**Post date:** [September 15, 2026, 9:43am UTC](https://users.rust-lang.org/t/how-to-concatenate-integers/142426/1 "2026-09-15T09:43:29Z")

</div>

```rust
const WIDTH: usize = 900;
const HEIGHT: usize = 900;

struct Image {
    width: usize,
    height: usize,
    buffer: Vec<u32>,
}

impl Image {
    fn load(filename: &str) -> Image {
        let image =
            lodepng::decode32_file(&filename).expect(&format!("Couldn't load {}", &filename));
        Image {
            width: image.width,
            height: image.height,
            buffer: image
                .buffer
                .iter()
                .map(|color| {
                    
                    // println!("{:?}", color);
                    // Is --> Rgba { r: 188, g: 147, b: 129, a: 255 }
                    // Should be --> u32
                    
                    let b = color.b;
                    let g = color.g;
                    let r = color.r;
                    let a = color.a;
                    let value = format!("{b}{g}{r}{a}");
                    // return 0x11223344;
                    return value as u32;
                })
                .collect(),
        }
    }
}

```

How can I concatenate the integer values and convert the string back to u32?

---

<div class="post-metadata">

**Author:** ![jofas](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jofas/32/30690_2.png) [@jofas](https://users.rust-lang.org/u/jofas)\
**Post date:** [September 15, 2026, 9:53am UTC](https://users.rust-lang.org/t/how-to-concatenate-integers/142426/2 "2026-09-15T09:53:08Z")

</div>

> [@canina](#):
>
> How can I concatenate the integer values and convert the string back to u32?

If you have 4 bytes (`r`, `g`, `b` and `a`), you can combine them way more efficiently into one `u32` by using [`u32::from_[le|ne|be]_bytes`](https://doc.rust-lang.org/std/primitive.u32.html#method.from_le_bytes) instead of serialising them to and from a UTF-8 string.

---

<div class="post-metadata">

**Author:** ![canina](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@canina](https://users.rust-lang.org/u/canina)\
**Post date:** [September 15, 2026, 10:03am UTC](https://users.rust-lang.org/t/how-to-concatenate-integers/142426/3 "2026-09-15T10:03:14Z")

</div>

Thank you, that was exactly what I needed!

---

<div class="post-metadata">

**Author:** ![jdahlstrom](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jdahlstrom/32/38017_2.png) [@jdahlstrom](https://users.rust-lang.org/u/jdahlstrom)\
**Post date:** [September 15, 2026, 10:51am UTC](https://users.rust-lang.org/t/how-to-concatenate-integers/142426/4 "2026-09-15T10:51:45Z")

</div>

For the sake of completeness, manually you’d do it using bit operations like this:

```rust
// given the u8’s r, g, b, a

let val = b as u32 << 24 | g as u32 << 16 | r as u32 << 8 | a as u32;

```

which gives 0xBBGGRRAA, with the advantage that you don’t have to worry about endianess.

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [September 15, 2026, 11:34am UTC](https://users.rust-lang.org/t/how-to-concatenate-integers/142426/5 "2026-09-15T11:34:25Z")

</div>

> [@jdahlstrom](#):
>
> with the advantage that you don’t have to worry about endianess.

you do have to care about endianess. it's just expressed in a different way.

with bit twiddling, the endianess is encoded in the formula directly, while with the `u32::from_xx_bytes()`, this information was in the name of the function and the sequence of the components of the array.

for example, these 3 are equivalent:

```rust
// the shift amounts for the component `b, g, r, a` in this order `24, 16, 8, 0` 
let val = b as u32 << 24 | g as u32 << 16 | r as u32 << 8 | a as u32 << 0;
// equivalent to `[b, g, r, a]` in "big" endian
let val = u32::from_be_bytes([b, g, r, a]);
// which is equivalent to `[a, r, g, b]` in "little" endian
let val = u32::from_le_bytes([a, r, g, b]);

```

while these 3 are equivalent, with opposite endianess of the above:

```rust
// `b, g, r, a` ordered in 0, 8, 16, 24 
let val = b as u32 << 0 | g as u32 << 8 | r as u32 << 16 | a as u32 << 24;
// so, `[b, g, r, a]` in "little" endian
let val = u32::from_le_bytes([b, g, r, a]);
// the reverse
let val = u32::from_be_bytes([a, r, g, b]);

```

---

<div class="post-metadata">

**Author:** ![Bruecki](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bruecki/32/40581_2.png) [@Bruecki](https://users.rust-lang.org/u/Bruecki)\
**Post date:** [September 15, 2026, 4:42pm UTC](https://users.rust-lang.org/t/how-to-concatenate-integers/142426/6 "2026-09-15T16:42:08Z")

</div>

To answer your original question: this is how you do it with string concatenation and parsing

```rust
u32::from_str_radix(&format!("{b:02X}{g:02X}{r:02X}{a:02X}"), 16).unwrap()

```

it formats the bytes as two digit hex each and parses the concatenated string as a base 16 number. This is ofc less efficient and less readable than the accepted proper solution.

---

<div class="post-metadata">

**Author:** ![shartrec](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/shartrec/32/35977_2.png) [@shartrec](https://users.rust-lang.org/u/shartrec)\
**Post date:** [September 21, 2026, 2:03am UTC](https://users.rust-lang.org/t/how-to-concatenate-integers/142426/7 "2026-09-21T02:03:07Z")

</div>

If you really have four integers of u8 and you want to combine them to a u32, then  
`a + b * 256 + c * 256 * 256 + d * 256 * 256 * 256`  
which doesn't have any knowledge of representation or endianess, but colors are not really numbers (integers), so I think previous answers are better here.

---

<div class="post-metadata">

**Author:** ![Cerber-Ursi](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cerber-ursi/32/8315_2.png) [@Cerber-Ursi](https://users.rust-lang.org/u/Cerber-Ursi)\
**Post date:** [September 21, 2026, 4:02am UTC](https://users.rust-lang.org/t/how-to-concatenate-integers/142426/8 "2026-09-21T04:02:44Z")

</div>

> [@shartrec](#):
>
> d \* 245

Typo or intended?

---

<div class="post-metadata">

**Author:** ![shartrec](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/shartrec/32/35977_2.png) [@shartrec](https://users.rust-lang.org/u/shartrec)\
**Post date:** [September 21, 2026, 6:13am UTC](https://users.rust-lang.org/t/how-to-concatenate-integers/142426/9 "2026-09-21T06:13:37Z")

</div>

Whoops. Typo. Corrected.

---

<div class="post-metadata">

**Author:** ![tczajka](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/tczajka/32/24743_2.png) [@tczajka](https://users.rust-lang.org/u/tczajka)\
**Post date:** [September 24, 2026, 1:33am UTC](https://users.rust-lang.org/t/how-to-concatenate-integers/142426/10 "2026-09-24T01:33:50Z")

</div>

> [@shartrec](#):
>
> If you really have four integers of u8 and you want to combine them to a u32, then  
> `a + b * 256 + c * 256 * 256 + d * 256 * 256 * 256`  
> which doesn't have any knowledge of representation or endianess

This is precisely what the function `u32::from_le_bytes([a, b, c, d])` does. Your formula defines what "little-endian" means.
