自定义进制:
/// <summary>
/// 二进制System Of Numeration (进制)
/// </summary>
const _char* SN2 = _t("01");
/// <summary>
/// 十进制System Of Numeration (进制)
/// </summary>
const _char* SN10 = _t("0123456789");
/// <summary>
/// 十六进制System Of Numeration (进制)
/// </summary>
const _char* SN16 = _t("0123456789ABCDEF");
/// <summary>
///62进制表示
/// System Of Numeration (进制)
/// 0123456789 10 6061
/// </summary>
const _char* SN62 = _t("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
算法:
/// <summary>
/// 把一个整数转换为任意进制数
/// </summary>
/// <param name="iNumber">要转换的数字</param>
/// <param name="snDef">进制定义</param>
/// <returns></returns>
/// 创建时间: 2023-04-15 最后一次修改时间:2023-04-15 已测试
_string _Math::IntToStr(const __int64& iNumber, const _char* snDefine)
{
_string sResult(_t(""), 50);
__int64 iFlat = (iNumber > 0 ? iNumber : -iNumber);
if (iNumber < 0)
{
sResult.Add(_t('-'));
}
int nStart = sResult.Length;
int sn = _Math::StrLen(snDefine);
while (iFlat) {
int nIndex = iFlat - (iFlat / sn * sn);
sResult.Add(snDefine[nIndex]);
iFlat = iFlat / sn;
}
if (sResult.Length == nStart) //iNumber == 0
{
sResult.Add(_t('0'));
}
else
{
_Math::StrReverse((_char*)(sResult.Data + nStart), sResult.Length - nStart);
}
return sResult;
}
例子:
输出结果: