首页 > 其他分享 >十进制转任意进制

十进制转任意进制

时间:2022-11-30 10:44:44浏览次数:44  
标签:slen 进制 int num serialChars return 任意 十进制 string

存个档;

    //十进制转成任意进制
    private static string GetCusSN(int num, int strLen, string serialChars) {
        int slen = serialChars.Length;
        if (num > Math.Pow(slen, strLen) - 1) return "-1";
        string sn = "";
        if (num <= slen - 1) sn = serialChars.Substring(num, 1);
        else {
            while (num > slen - 1) {
                int remainder = num % slen;
                num /= slen;
                sn = serialChars.Substring(remainder, 1) + sn;
                if (num <= slen - 1) sn = serialChars.Substring(num, 1) + sn;
            }
        }
        return sn.PadLeft(strLen, serialChars.Left(1).ToCharArray()[0]);
    }

    //十进制左边补0
    private static string GetDecSN(int num, int decLen) {
        if (num > Math.Pow(10, decLen) - 1) return "-1";
        else return $"{num}".PadLeft(decLen, '0');
    }

 

标签:slen,进制,int,num,serialChars,return,任意,十进制,string
From: https://www.cnblogs.com/leavind/p/16937706.html

相关文章