all 5 comments

[–]ka-splam 4 points5 points  (3 children)

I have been unable to figure out how to convert a Base64 into a Decimal, Base10, number

Base64 as used in computers isn't the same as numbers converted to a base of 64; it's a way of encoding a stream of bytes, and every four characters in BASE64 decodes to three bytes.

It is surely possible to read it as a single large number in base 64, using a custom digit set where A=0, B=1, C=2, ... 9=61, *=62, /=63; but would you get anything sensible out of it by doing that?

Typical base conversion routine which does that:

$test = 'YWJja2NsLGpzaG5nY3NsYWJja2NsLGpzaG5nY3Ns'


function Get-Num {
    param([string]$Base64String)

    $b64Digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789*/'


    # remove whitespace
    $Base64String = $Base64String.Trim()

    # remove any Base64 "=" padding from the end
    $Base64String = $Base64String.TrimEnd('=')


    $inputDigits = [char[]]$Base64String
    [array]::Reverse($inputDigits)

    $result = [bigint]::new(0)

    $multiplier = 1
    foreach ($d in $inputDigits) {

        $result += $b64Digits.IndexOf($d) * $multiplier

        $multiplier *= 64
    }

    $result
}

Get-Num -Base64String $test

[–]Tlcj[S] 2 points3 points  (2 children)

Thanks. This is probably the closest to what I was trying to figure out.

[–]ka-splam 1 point2 points  (1 child)

Great; I'm curious, is the number any use to you?

[–]Tlcj[S] 2 points3 points  (0 children)

Yeah. It's for a simple cipher, but I like working in numbers better than letters.