Arokh Posted April 10, 2009 Posted April 10, 2009 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. Quote
JumpyNET Posted April 10, 2009 Posted April 10, 2009 Is there a simple way to do this? Will this do? 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 Quote
Arokh Posted April 10, 2009 Author Posted April 10, 2009 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. Quote
Administrators PlausiblyDamp Posted April 12, 2009 Administrators Posted April 12, 2009 Just to check - are you wanting to treat the data as 16 bit values rather than 8 bit values? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Arokh Posted April 12, 2009 Author Posted April 12, 2009 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.