N進位轉換

最近工作上剛好有需要用到這個順手寫寫也記錄下來,在網路上都找不到相關的資源,寫出來給現在Google到的您參考參考

以下範例是以62進位來做示範,當然你可以自行創造自己的進位法,但是每個進位所要代表的符號必須由您自己來訂定。

說明:
strArr變數,這個很重要喔,想要使用此程式的人要特別注意這個變數的涵義,裡面的值代表著進位轉換的符號,變數裡的字元數也就是代表著進位數,如果你是要換算十六進位當然把變數的值改為 0123456789ABCDEF(PS:注意我裡面有寫 String2Int這個方法要注意到如果是16進位請將值統一轉為大寫或小寫以免判斷符號時找不到符號 )。

C#寫法
/// 
        /// 62進位 Int64 Convert String
        /// Ex:
        ///     Int2String(218340105584896) = "zzzzzzzz"
        /// 
        /// Decimal        /// 62 String
        static String Int2String(Int64 intValue)
        {
            String strValue = String.Empty;
            String strArr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";           
            while (intValue != 0)
            {
                int intRem = 0;
                intRem = (int)(intValue % strArr.Length);
                strValue = strArr.Substring(intRem, 1) + strValue;
                intValue /= strArr.Length;
            }
            return strValue;
        }
        /// 
        /// 62進位 String Convert Int64
        /// Ex:
        ///     String2Int("zz") = 3843
        /// 
        /// 62 String        /// Decimal
        static Int64 String2Int(String strValue)
        {
            System.Text.RegularExpressions.Regex regex =
                new System.Text.RegularExpressions.Regex(@"[^a-zA-Z0-9]");

            if (!regex.IsMatch(strValue))
            {
                Int64 intValue = 0;
                String strArr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                // 去除前端0
                for (int i = 0; i < strValue.Length; i++)
                {
                    if (strValue.Substring(i, 1) != "0")
                    {
                        strValue = strValue.Substring(i, strValue.Length - i);
                        break;
                    }
                }

                for (int i = 0; i < strValue.Length; i++)
                {
                    int intIndexOf = strArr.IndexOf(strValue.Substring(i, 1));
                    Int64 intBon = 0;
                    intBon = (strValue.Length - 1 - i);
                    intBon = (Int64)Math.Pow(strArr.Length, intBon);
                    intValue += intIndexOf * intBon;
                }
                return intValue;
            }
            else
            {
                return 0;
            }
        }
============================================================== VB寫法
''' 
''' 62進位 Int64 Convert String
''' Ex:
'''     Int2String(218340105584896) = "zzzzzzzz"
''' 
''' Decimal''' 62 String
Private Shared Function Int2String(intValue As Int64) As [String]
 Dim strValue As [String] = [String].Empty
 Dim strArr As [String] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 While intValue <> 0
  Dim intRem As Integer = 0
  intRem = CInt(intValue Mod strArr.Length)
  strValue = strArr.Substring(intRem, 1) + strValue
  intValue /= strArr.Length
 End While
 Return strValue
End Function

''' 
''' 62進位 String Convert Int64
''' Ex:
'''     String2Int("zz") = 3843
''' 
''' 62 String''' Decimal
Private Shared Function String2Int(strValue As [String]) As Int64
 Dim regex As New System.Text.RegularExpressions.Regex("[^a-zA-Z0-9]")

 If Not regex.IsMatch(strValue) Then
  Dim intValue As Int64 = 0
  Dim strArr As [String] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  ' 去除前端0
  For i As Integer = 0 To strValue.Length - 1
   If strValue.Substring(i, 1) <> "0" Then
    strValue = strValue.Substring(i, strValue.Length - i)
    Exit For
   End If
  Next

  For i As Integer = 0 To strValue.Length - 1
   Dim intIndexOf As Integer = strArr.IndexOf(strValue.Substring(i, 1))
   Dim intBon As Int64 = 0
   intBon = (strValue.Length - 1 - i)
   intBon = DirectCast(Math.Pow(strArr.Length, intBon), Int64)
   intValue += intIndexOf * intBon
  Next
  Return intValue
 Else
  Return 0
 End If
End Function

留言

這個網誌中的熱門文章

delivery note和delivery order的區別和翻譯

Eclipse 3.6.1 Helios 中文化方法

牙技專業英文--技工篇