本文介绍了从Delphi到C#的BaseIntToStr或BaseStrToInt函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将Delphi函数BaseIntToStr或BaseStrToInt转换为c#与delphi相比,我没有得到确切的结果.任何人都可以在这里帮助我.
C#代码
I am trying to convert the Delphi funtions BaseIntToStr or BaseStrToInt to c# I am not getting the exact results as compared to delphi. Any one can help me out over here.
C# code
private int BaseStrToInt(string value)
{
string temp;
string validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int result = 0;
result = 0;
for (int index= 0; index < value.Length; index++)
{
temp = value.Substring(index, 1);
result = (result * 36) + validChars.IndexOf(temp);
}
return result;
}
private string BaseIntToStr(int value)
{
string result = string.Empty;
int temp = value;
char[] base36Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
//while(value != 0)
//{
temp = (value % 36);
result = base36Chars[temp] + result;
value = (value / 36);
//}
result = result.PadLeft(3-result.Length, '0');
return result.Substring(0, 3);
}
Delphi代码
Delphi code
function BaseStrToInt(const Base : byte; const strToConvert : string) : longint;
var
intResult : longint;
bytIndex : byte;
const
strValidChars : string [36] = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
begin
intResult := 0;
for bytIndex := 1 to Length(strTOConvert) do
intResult := (intResult * Base) + Pred(Pos(UpperCase(strToConvert[bytIndex]),strValidChars));
Result := intResult;
end;
function BaseIntToStr(const Base : byte; intToConvert : longint; intDigits : integer) : string;
var
intIndex : integer;
strResult : string;
const
strValidChars : string [36] = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
begin
strResult := '';
repeat
strResult := strValidChars[Succ(intToConvert mod Base)] + strResult;
intToConvert := intToConvert div Base;
until intToConvert = 0;
for intIndex := 1 to (intDigits - Length(strResult)) do
strResult := '0' + strResult;
strResult := Copy(strResult,1,intDigits);
Result := strResult;
end;
推荐答案
// Usage
// Convert.ToString(numberToConvert, baseInDecimal);
// Example
Convert.ToString(6, 2); //Will output ''110''
问题在于,它允许的唯一基数是2、8、10和16.
<编辑>
我刚刚发现将数字转换为另一个基数 [ ^ ]可能会为您提供帮助.
</Edit>
<编辑2>
此 [ ^ ]页面声称转换为2,36范围内的任何基数.
</编辑2>
The problem is that the only bases it allows are 2, 8, 10 and 16.
<Edit>
I have just found Converting numbers to another radix[^] which may assist you.
</Edit>
<Edit 2>
The c# example on this[^] page on MSDN claims to convert to any base in the range 2,36.
</Edit 2>
这篇关于从Delphi到C#的BaseIntToStr或BaseStrToInt函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!