Meltinglava
Meltinglava

Reputation: 349

Format/convert a number to a string in any base (including bases other than decimal or hexadecimal)

Currently I'm using the following code to return a number as a binary (base 2), octal (base 8), or hexadecimal (base 16) string.

fn convert(inp: u32, out: u32, numb: &String) -> Result<String, String> {
    match isize::from_str_radix(numb, inp) {
        Ok(a) => match out {
            2 => Ok(format!("{:b}", a)),
            8 => Ok(format!("{:o}", a)),
            16 => Ok(format!("{:x}", a)),
            10 => Ok(format!("{}", a)),
            0 | 1 => Err(format!("No base lower than 2!")),
            _ => Err(format!("printing in this base is not supported")),
        },
        Err(e) => Err(format!(
            "Could not convert {} to a number in base {}.\n{:?}\n",
            numb, inp, e
        )),
    }
}

Now I want to replace the inner match statement so I can return the number as an arbitrarily based string (e.g. base 3.) Are there any built-in functions to convert a number into any given radix, similar to JavaScript's Number.toString() method?

Upvotes: 18

Views: 10548

Answers (4)

legendree
legendree

Reputation: 13

Here is an extended solution based on the first comment which does not bind the parameter x to be a u32:

fn format_radix(mut x: u128, radix: u32) -> String {
    let mut result = vec![];

    loop {
        let m = x % radix as u128;
        x = x / radix as u128;

        // will panic if you use a bad radix (< 2 or > 36).
        result.push(std::char::from_digit(m as u32, radix).unwrap());
        if x == 0 {
            break;
        }
    }
    result.into_iter().rev().collect()
}

Upvotes: 0

Zombo
Zombo

Reputation: 1

This is faster than the other answer:

use std::char::from_digit;

fn encode(mut n: u32, r: u32) -> Option<String> {
   let mut s = String::new();
   loop {
      if let Some(c) = from_digit(n % r, r) {
         s.insert(0, c)
      } else {
         return None
      }
      n /= r;
      if n == 0 {
         break
      }
   }
   Some(s)
}

Note I also tried these, but they were slower:

Upvotes: -3

Boiethios
Boiethios

Reputation: 42789

For now, you cannot do it using the standard library, but you can:

  • use my crate radix_fmt
  • or roll your own implementation:

    fn format_radix(mut x: u32, radix: u32) -> String {
        let mut result = vec![];
    
        loop {
            let m = x % radix;
            x = x / radix;
    
            // will panic if you use a bad radix (< 2 or > 36).
            result.push(std::char::from_digit(m, radix).unwrap());
            if x == 0 {
                break;
            }
        }
        result.into_iter().rev().collect()
    }
    
    fn main() {
        assert_eq!(format_radix(1234, 10), "1234");
        assert_eq!(format_radix(1000, 10), "1000");
        assert_eq!(format_radix(0, 10), "0");
    }
    

Upvotes: 22

Shepmaster
Shepmaster

Reputation: 430921

If you wanted to eke out a little more performance, you can create a struct and implement Display or Debug for it. This avoids allocating a String. For maximum over-engineering, you can also have a stack-allocated array instead of the Vec.

Here is Boiethios' answer with these changes applied:

struct Radix {
    x: i32,
    radix: u32,
}

impl Radix {
    fn new(x: i32, radix: u32) -> Result<Self, &'static str> {
        if radix < 2 || radix > 36 {
            Err("Unnsupported radix")
        } else {
            Ok(Self { x, radix })
        }
    }
}

use std::fmt;

impl fmt::Display for Radix {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut x = self.x;
        // Good for binary formatting of `u128`s
        let mut result = ['\0'; 128];
        let mut used = 0;
        let negative = x < 0;
        if negative {
            x*=-1;
        }
        let mut x = x as u32;
        loop {
            let m = x % self.radix;
            x /= self.radix;

            result[used] = std::char::from_digit(m, self.radix).unwrap();
            used += 1;

            if x == 0 {
                break;
            }
        }

        if negative {
            write!(f, "-")?;
        }

        for c in result[..used].iter().rev() {
            write!(f, "{}", c)?;
        }

        Ok(())
    }
}

fn main() {
    assert_eq!(Radix::new(1234, 10).to_string(), "1234");
    assert_eq!(Radix::new(1000, 10).to_string(), "1000");
    assert_eq!(Radix::new(0, 10).to_string(), "0");
}

This could still be optimized by:

  • creating an ASCII array instead of a char array
  • not zero-initializing the array

Since these avenues require unsafe or an external crate like arraybuf, I have not included them. You can see sample code in internal implementation details of the standard library.

Upvotes: 6

Related Questions