Click here to Skip to main content
15,890,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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 code
C#
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;


VB
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;
Posted
Comments
samyu1* 6-May-10 15:47pm    
Any one who has Delphi and c# knowledge please help me out. Thank you.

C# strings are unicode and this code is a hideous hack. You can use ToString on a number to turn it to a string representation. You can use int.TryParse to try to convert a full string of numbers into a numeric representation.
 
Share this answer
 
Comments
samyu1* 6-May-10 16:02pm    
Actually this is not just converting int to string or string to int It converts a string to an integer in the base specified i.e. Base 2 (binary), Base 10 (standard), Base 16 (Hex), etc.
For a somewhat limited number of bases there is

// Usage
// Convert.ToString(numberToConvert, baseInDecimal);

// Example
Convert.ToString(6, 2); //Will output '110'


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>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900