Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, May 22, 2006 3:46 AM | 1 vote
dear All,
I am creating a program that generate id for any incoming material lot using hexadecimal ID, Since the number of incoming material lot increasing rapidly, we plan to move to a new ID number with alphanumeric(0-9,A-Z).
The convert class doesn't support the base 36 type. Does anyone have any idea for my problem? Any help is very appreciated.
Thank you
regards
Freddy Halim
All replies (9)
Monday, May 22, 2006 6:41 AM âś…Answered | 3 votes
Hi,
check if works for you:
public String ConvertToBase(int num, int nbase)
{
String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// check if we can convert to another base
if(nbase < 2 || nbase > chars.Length)
return "";
int r;
String newNumber = "";
// in r we have the offset of the char that was converted to the new base
while(num >= nbase)
{
r = num % nbase;
newNumber = chars[r] + newNumber;
num = num / nbase;
}
// the last number to convert
newNumber = chars[num] + newNumber;
return newNumber;
}
Monday, May 22, 2006 7:42 AM
Dear n0n4me,
Yeah its works. Looking at your code, i think i can start create the opposite conversion from base36 to base10 too.
Thank you for your help. Your help is very appreciated and invaluable for me.
Warmest Regards,
Freddy Halim
Monday, May 22, 2006 11:35 AM
Glad it helped.
If you need any help converting from base 36 -> base 10 just ask. But it shouldn't be difficult. :)
Wednesday, September 13, 2006 10:08 PM
n0n4m3,
I was hoping that you could help me by posting the code to convert from base-36 to base-10
Thanks in advance,
MMcNamara
Thursday, September 14, 2006 3:10 PM
Actually, you may want to use the standard Base-40 encoding (which is supported by .Net)
for(long i = 100000; i < 200000; ++i)
{
string enc = Convert.ToBase64String(BitConverter.GetBytes(i));
long dec = BitConverter.ToInt64(Convert.FromBase64String(enc), 0);
Console.WriteLine("From {0}, to {1}, to {2}", i, enc, dec);
}
Tuesday, June 11, 2013 5:06 PM
I needed this logic in VB, so here is your code written in Visual Basic.
(Could not find a similar forum for VB)
' Convert Base10 to any other Base (2 to 36)
' From msdn.microsoft.com forum C# code - converted to VB
Public Function convertToBase(ByVal value As Int32, base As Int32) As String
Dim chars As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim r As Int32
Dim newNumber As String
If (base < 2 Or base > chars.Length) Then
Return ""
End If
newNumber = ""
While value >= base
r = value Mod base
newNumber = chars.Substring(r, 1) & newNumber
value = Convert.ItInt32(Math.Floor(value / base)) ' Don't want to round up
End While
If value > 0 Then
newNumber = chars.Substring(value, 1) & newNumber
End If
If String.IsNullOrEmpty(newNumber) Then
newNumber = "0"
End If
r = Nothing
chars = Nothing
Return newNumber
End Function
Thursday, June 13, 2013 7:29 PM
That would be the aptly named Visual Basic forum.
Karl
When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer
My Blog: http://unlockpowershell.wordpress.com
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
Thursday, January 15, 2015 6:28 AM | 2 votes
You can succinctly perform this task with Linq and avoid having an alphabet constant at all as well as support converting from any base (2 to 36) to base 10 as follows:
string b36 = "000A", tbase = 36;
int b10 = b36
.Select(d => d >= '0' && d <= '9' ? d - '0' : 10 + char.ToUpper(d) - 'A')
.Aggregate(0, (pos, d) => pos * tbase + d);
For completeness (to go from base 10 to any base):
int value = 10, tbase = 36;
string result = "";
while (value > 0)
{
int x = value % tbase;
result = (char)(x >= 0 && x <= 9 ? x + 48 : x + 'A' - 10) + result;
value /= tbase;
}
Console.WriteLine(result.PadLeft(4,'0'));
Thursday, December 3, 2015 8:03 PM
// check if we can convert to another base
if(nbase < 2 || nbase > chars.Length)
return "";
This bounds check should throw an ArgumentOutOfRange exception rather than just returning an empty string. In .NET, if a function can't accomplish what it is supposed to do then it should throw.
Regards, David Totzke