Convert bytearray (of arbitrary size) to string of arbitrary base

Arokh

Centurion
Joined
Apr 11, 2006
Messages
124
Hi,

My current situation:
After hashing I get a bytearray of 24 bytes,
normally one would just convert it to hex which is easy.

But now I have to convert it to the base of 36 (0-9 + A-Z),
which is not so easy anymore.

Is there a simple way to do this?

The idea I have come up with seems a bit complex for the problem:
[CSharp]public static byte[] ByteMultiply(byte[] b, int x)
public static byte[] ByteDivision(byte[] b, int x)
public static byte[] ByteAdd(byte[] b, int x)
public static byte[] ByteSubstract(byte[] b1, byte[] b2)
public static bool ByteEqual(byte[] b, int x)
public static bool ByteGreaterThan(byte[] b1, byte[] b2)

public static string ToString(byte[] b, int baseNumber) {
byte[] baseDigit = new byte[0];
string baseStr = "";

baseDigit = ByteAdd(baseDigit, 1);
while(true) {
baseDigit = ByteMultiply(baseDigit, baseNumber);
if(ByteGreaterThan(baseDigit, b)) break;
}
baseDigit = ByteDivision(baseDigit, baseNumber);

int count;

while(!ByteEqual(baseDigit, 1)){
count = 0;
while(ByteGreaterThan(b, baseDigit)) {
b = ByteSubstract(b, baseDigit);
count++;
}
baseStr += map[count];
baseDigit = ByteDivision(baseDigit, baseNumber);
}

count = 0;
while(ByteGreaterThan(b, baseDigit)) {
b = ByteSubstract(b, baseDigit);
count++;
}
baseStr += map[count];


return baseStr;
}[/CSharp]

Ah, oh well the '[' issue in the CSharp tag is still around.
 
Is there a simple way to do this?

Will this do?

Code:
    Private Sub TestConversion()
        Dim ByteArray() As Byte = {14, 215, 153}
        Dim Converter As New Converter(Of Byte, String)(AddressOf ConvertToBase32)
        Dim StringArray() As String = Array.ConvertAll(ByteArray, Converter)
        For i As Integer = 0 To ByteArray.Length - 1
            Debug.Print(ByteArray(i) & " --> " & StringArray(i))
        Next
    End Sub

    Public Shared Function ConvertToBase32(ByVal ByteValue As Byte) As String
        'Code from: http://en.wikipedia.org/wiki/Base_36#C.23_Conversion_Class
        Dim returnValue As String = ""
        Dim base36Chars = New Char() {"0"c, "1"c, "2"c, "3"c, "4"c, "5"c, _
                                      "6"c, "7"c, "8"c, "9"c, "a"c, "b"c, _
                                      "c"c, "d"c, "e"c, "f"c, "g"c, "h"c, _
                                      "i"c, "j"c, "k"c, "l"c, "m"c, "n"c, _
                                      "o"c, "p"c, "q"c, "r"c, "s"c, "t"c, _
                                      "u"c, "v"c, "w"c, "x"c, "y"c, "z"c}
        While ByteValue <> 0
            returnValue = base36Chars(ByteValue Mod base36Chars.Length) + returnValue
            ByteValue /= base36Chars.Length
        End While
        Return returnValue.ToUpper()
    End Function
 
Since it converts every byte by itself to the base of 36, it is not what I need.

Lets assume I have a byte array of {1, 0},
now the number I need to convert is not 00000001 & 00000000 but
0000000100000000 (In decimal 256)

Your code would produce {"1", "0"} what I need is "74"

A quick site to test for lower numbers:
http://www.mste.uiuc.edu/users/exner/ncsa/base/default.html

But thanks for the nice wiki find,
based on that I can simplify my code a bit.
 
Just to check - are you wanting to treat the data as 16 bit values rather than 8 bit values?

Yes, that is exactly what I want,
only I'll use that function for >=192bit values.
 
Back
Top