cast::base

Trait ToBase

Source
pub trait ToBase {
    type Err;

    // Required method
    fn to_base(&self, base: Base, add_prefix: bool) -> Result<String, Self::Err>;
}
Expand description

Facilitates formatting an integer into a Base.

Required Associated Types§

Required Methods§

Source

fn to_base(&self, base: Base, add_prefix: bool) -> Result<String, Self::Err>

Formats self into a base, specifying whether to add the base prefix or not.

Tries converting self into a NumberWithBase and then formats into the provided base by using the Debug implementation.

§Example
use alloy_primitives::U256;
use cast::base::{Base, ToBase};

// Any type that implements ToBase
let number = U256::from(12345);
assert_eq!(number.to_base(Base::Decimal, false).unwrap(), "12345");
assert_eq!(number.to_base(Base::Hexadecimal, false).unwrap(), "3039");
assert_eq!(number.to_base(Base::Hexadecimal, true).unwrap(), "0x3039");
assert_eq!(number.to_base(Base::Binary, true).unwrap(), "0b11000000111001");
assert_eq!(number.to_base(Base::Octal, true).unwrap(), "0o30071");

Implementations on Foreign Types§

Source§

impl ToBase for str

Source§

type Err = Report

Source§

fn to_base(&self, base: Base, add_prefix: bool) -> Result<String, Self::Err>

Source§

impl ToBase for String

Source§

type Err = Report

Source§

fn to_base(&self, base: Base, add_prefix: bool) -> Result<String, Self::Err>

Implementors§