pub fn convert(number: &[u32], from_base: u32, to_base: u32) -> Result<Vec<u32>, Error> {
    if from_base <= 1 {
        return Err(Error::InvalidInputBase);
    }

    if to_base <= 1 {
        return Err(Error::InvalidOutputBase);
    }

    let mut sum = 0;
    for (i, n) in number.iter().rev().enumerate() {
        if *n >= from_base {
            return Err(Error::InvalidDigit(*n));
        }

        sum += n * from_base.pow(i as u32);
    }

    let mut ret: Vec<u32> = vec![];
    while sum > 0 {
        ret.push(sum % to_base);
        sum /= to_base;
    }

    if ret.is_empty() {
        ret.push(0);
    }

    Ok(ret.into_iter().rev().collect())
}
///
/// Convert a number between two bases.
///
/// A number is any slice of digits.
/// A digit is any unsigned integer (e.g. u8, u16, u32, u64, or usize).
/// Bases are specified as unsigned integers.
///
/// Return an `Err(.)` if the conversion is impossible.
/// The tests do not test for specific values inside the `Err(.)`.
///
///
/// You are allowed to change the function signature as long as all test still pass.
///
///
/// Example:
/// Input
///   number: &[4, 2]
///   from_base: 10
///   to_base: 2
/// Result
///   Ok(vec![1, 0, 1, 0, 1, 0])
///
/// The example corresponds to converting the number 42 from decimal
/// which is equivalent to 101010 in binary.
///
///
/// Notes:
///  * The empty slice ( "[]" ) is equal to the number 0.
///  * Never output leading 0 digits, unless the input number is 0, in which the output must be `[0]`.
///    However, your function must be able to process input with leading 0 digits.
///
Convert a number between two bases.
A number is any slice of digits.
A digit is any unsigned integer .
Bases are specified as unsigned integers.

Return an `Err(.)` if the conversion is impossible.
The tests do not test for specific values inside the `Err(.)`.

You are allowed to change the function signature as long as all test still pass.



The example corresponds to converting the number 42 from decimal
which is equivalent to 101010 in binary.

The empty slice ( "[]" ) is equal to the number 0.
Never output leading 0 digits, unless the input number is 0, in which the output must be `[0]`.
However, your function must be able to process input with leading 0 digits.
Convert a number between two bases . A number is any slice of digits . A digit is any unsigned integer . Bases are specified as unsigned integers . Return an ` Err ( . ) ` if the conversion is impossible . The tests do not test for specific values inside the ` Err ( . ) ` . You are allowed to change the function signature as long as all test still pass . The example corresponds to converting the number 42 from decimal which is equivalent to 101010 in binary . The empty slice ( " [] " ) is equal to the number 0 . Never output leading 0 digits unless the input number is 0 in which the output must be ` [ 0 ] ` . However your function must be able to process input with leading 0 digits .
pub fn convert(number: &[u32], from_base: u32, to_base: u32) -> Result<Vec<u32>, Error> {
    if from_base <= 1 {
        return Err(Error::InvalidInputBase);
    }
    if to_base <= 1 {
        return Err(Error::InvalidOutputBase);
    }
    let mut sum = 0;
    for (i, n) in number.iter().rev().enumerate() {
        if *n >= from_base {
            return Err(Error::InvalidDigit(*n));
        }
        sum += n * from_base.pow(i as u32);
    }
    let mut ret: Vec<u32> = vec![];
    while sum > 0 {
        ret.push(sum % to_base);
        sum /= to_base;
    }
    if ret.is_empty() {
        ret.push(0);
    }
    Ok(ret.into_iter().rev().collect())
}
pub fn convert ( number : & [ u32 ] , from_base : u32 , to_base : u32 ) -> Result < Vec < u32 > , Error > { if from_base <= 1 { return Err ( Error :: InvalidInputBase ) ; } if to_base <= 1 { return Err ( Error :: InvalidOutputBase ) ; } let mut sum = 0 ; for ( i , n ) in number . iter ( ) . rev ( ) . enumerate ( ) { if * n >= from_base { return Err ( Error :: InvalidDigit ( * n ) ) ; } sum += n * from_base . pow ( i as u32 ) ; } let mut ret : Vec < u32 > = vec ! [ ] ; while sum > 0 { ret . push ( sum % to_base ) ; sum /= to_base ; } if ret . is_empty ( ) { ret . push ( 0 ) ; } Ok ( ret . into_iter ( ) . rev ( ) . collect ( ) ) }
Convert a number between two bases.
Convert a number between two bases .
number
&[u32]
from_base
u32
to_base
u32
number
&[u32]
from_base
u32
to_base
u32