ChzChz
ChzChz

Reputation: 67

algorithm for converting from base58 string into Hexadecimal string

I want to convert 1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i base58 string into hexadecimal string. I began to do it from converting base58 into decimal number and after that into heximal string.

fn base58_decode(code: &String) -> u128 {
  use std::collections::HashMap;

  const ALPHABET: &str = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

  let keys = ALPHABET.chars();
  let map: HashMap<_, u128> = keys.zip(0..).collect();

  let mut result: u128 = 0;

  for (n, ch) in code.chars().rev().enumerate() {
    let value = map.get(&ch);
    result += value.unwrap() * 58u128.pow(n as u32);
  }

  result
}

But this code throw overflow panic, so I need better algorithm for it.

Upvotes: 1

Views: 864

Answers (0)

Related Questions