Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 137,221 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,139 people online right now. Registration is fast and FREE... Join Now!





Convert Bases

Converts from one base to another base.

Submitted By: AdamSpeight2008
Actions:
Rating:
Views: 727

Language: VB.NET

Last Modified: July 2, 2008
Instructions: Example
Console.WriteLine(ConvertBases("50312506748036450", 10, 36)) ' => "DREAMINCODE" base 36

Snippet


  1.     Public Function ConvertBases(ByVal s As String, ByVal FromBase As Integer, ByVal ToBase As Integer) As String
  2.         '  Convert number in string representation in fromBase into toBase. Return result as a string
  3.         '  Return error if input is empty
  4.         If String.IsNullOrEmpty(s) Then Return ""
  5.         '  only do base 2 to base 36 (digit represented by charecaters 0-Z)"
  6.         If (FromBase < 2 Or FromBase > 36) Or (ToBase < 2 Or ToBase > 36) Then Return ""
  7.         s = s.ToUpper  '  Convert to uppercase
  8.         Const Allowed As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  9.         If System.Text.RegularExpressions.Regex.IsMatch(s, "^[" & Allowed.Substring(0, FromBase) & "]$*") = False Then Return ""
  10.         ' convert string to an array of integer digits representing number in frombase
  11.         Dim il As Integer = s.Length : Dim fs(il) As Integer
  12.         For i As Integer = s.Length - 1 To 0 Step -1 : fs(s.Length - (i + 1)) = Allowed.IndexOf(s(i)) : Next
  13.         Dim ol = il * (FromBase / ToBase + 1)  ' find how many digits the output needs
  14.         Dim acc(ol + 10) As Integer ' assign accumulation array
  15.         Dim Result(ol + 10) As Integer ' assign the result array
  16.         acc(0) = 1 ' initialise first acculamtion array element with number 1
  17.         Dim ip As Integer = 0
  18.         ' for each input digit
  19.         For i As Integer = 0 To il
  20.             For j As Integer = 0 To ol  ' add the input digit times (fromBase^i in baseTo) to the output accumulator
  21.                 Result(j) += acc(j) * fs(i) : ip = j
  22.                 Do While (Result(ip) >= ToBase)  ' fix & cascade any which exceeds toBase
  23.                     Result(ip + 1) += Result(ip) \ ToBase : Result(ip) = Result(ip) Mod ToBase : ip += 1
  24.                 Loop
  25.             Next
  26.             ' Calculate the next power from^i) in toBase format
  27.             For j As Integer = 0 To ol : acc(j) *= FromBase : Next
  28.             ip = 0 : Do While acc(ip) >= ToBase 'check for any which exceed toBase
  29.                 acc(ip + 1) += acc(ip) \ ToBase : acc(ip) = acc(ip) Mod ToBase : ip += 1
  30.             Loop
  31.         Next
  32.         ' convert the output to string format (digits 0,toBase-1 converted to 0-Z characters)
  33.         ConvertBases = String.Empty ' initialise output string
  34.         ip = ol : While Result(ip) = 0 : ip -= 1 : End While
  35.         While ip >= 0 : ConvertBases &= Allowed(Result(ip)) : ip -= 1 : End While
  36.         If String.IsNullOrEmpty(ConvertBases) Then Return "0" ' //input was zero, return 0
  37.         ' return the converted string
  38.         Return ConvertBases
  39.     End Function

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month