Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 Converting a number from one base to another


How can I convert a number from one base to another base?   

    The following function will convert a number from one base to a number of another base:

    procedure RadixStr(NumStr : pChar; Radix : LongInt; ResultStr : pChar; NewRadix : LongInt; var ErrorCode : LongInt);

    The RadixStr() function takes a pointer to a null terminated string containing a number of one base, and fills a buffer with a null terminated string containing the number converted to another base.

    Parameters:

    NumStr: A pointer to a null terminated string containing the numeric string to convert:

    Radix: The base of the number contained in the NumStr parameter. The base must be in the range of 2 to 36;

    ResultStr : A pointer to a null terminated string buffer to place the resulting numeric string. The buffer should be sufficiently large to hold the resulting string.

    NewRadix: The base to use in the conversion. The base must be in the range of 2 to 36;

    ErrorCode: Upon return, contains the return code 0 if successful, or the character number of the offending character contained in the buffer NumStr.

    Examples of calling the RadixStr() function:

    {Convert Hex to Decimal} RadixStr('FF', 6, lpBuffer, 0, Code);

    Should return the string '255' in lpbuffer^.

    {Convert Decimal to Binary} RadixStr('255', 0, lpBuffer, 2, Code);

    Should return the string ' ' in lpbuffer^.

    {Convert Hex to Octal} RadixStr('FF', 6, lpBuffer, 8, Code);

    Should return the string '377' in lpbuffer^.

    {Function code}

    procedure RadixStr(NumStr : pChar; Radix : LongInt; ResultStr : pChar; NewRadix : LongInt; var ErrorCode : LongInt); var RadixChar : array[0..35] of Char; v : LongInt; i : LongInt; p : LongInt; c : Integer; begin if ((Abs(Radix) < 2) or (Abs(Radix) > 36)) then begin ErrorCode := p; Exit; end; StrLCopy(ResultStr, NumStr, StrLen(NumStr)); for i := 0 to 35 do begin if i <= 9 then RadixChar[i] := Char(48 + (i)) else RadixChar[i] := Char(64 + (i - 9)) end; v := 0; for i := 0 to (StrLen(ResultStr) - ) do begin ResultStr[i] := UpCase(ResultStr[i]); p := Pos(ResultStr[i], PChar(@RadixChar)) - ; if ((p < 0) or (p >= Abs(Radix))) then begin ErrorCode := i; Exit; end; v := v * Abs(Radix) + p; end; if v = 0 then begin ResultStr := '0'; ErrorCode := 0; exit; end else begin i:=0; repeat ResultStr[i] := RadixChar[v mod NewRadix]; v := v div NewRadix; Inc(i) until v = 0; if Radix < 0 then begin ResultStr[i] := '-'; ResultStr[i + ] := #0 end else ResultStr[i] := #0; p := StrLen(ResultStr); for i := 0 to ((p div 2) - ) do begin ResultStr[i] := Char(Byte(ResultStr[i]) xor Byte(ResultStr[(p - i) - ])); ResultStr[(p - i) - ] := Char(Byte(ResultStr[(p - i) - ]) xor Byte(ResultStr[i])); ResultStr[i] := Char(Byte(ResultStr[i]) xor Byte(ResultStr[(p - i) - ])) end; ResultStr[p] := #0; ErrorCode := 0; end; end; 7/ 6/98 4:3 :28 PM

     



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use