Add the base2base() method in the main class. This static method takes a string
ID: 3665980 • Letter: A
Question
Add the base2base() method in the main class. This static method takes a string argument
consisting of base-N digits where N is between 2 and 36. The first ten digits in any base are the symbols
‘0’ through ‘9’ and any further digits that are needed use the symbols ‘A’ through ‘Z’. Both upper and
lower case alphabetic digits are acceptable. The second argument is the value of the base the input
string is represented in. The third argument is the value of the base that the output string is to be
represented in. The method returns a string of digits in the specified output base.
Explanation / Answer
class BaseConveter
{
static void Main ( string argumnts[] )
{
// sexagesimal-conversion
string yy =Int2String(42,
new char[] { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ,
'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' ,
'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' } ) ;
// (base 26, A-Z) hexavigesimal-conversion
string hexavigesimal= Int2String ( 42 , Enumerable.Range( 'A' , 26 ).Select( x=> ( char ) x ).ToArray() ) ;
// Hexadecimal-conversion
string ConHexaNum =Int2String ( 42 ,
new char[] { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ,
'A' , 'B' , 'C' , 'D' , 'E' , 'F' } ) ;
// Binary-conversion
string ConBinry =Int2String ( 42 , new char[] { '0' , '1' } ) ;
}
public static string Int2String ( int var , char[] charsBase )
{
string output =string.Empty ;
int baseTarget =charsBase.Length ;
do
{
output = charsBase[var % baseTarget] + output ;
var = var / baseTarget ;
}
while (var > 0) ;
return output ;
}
public static string Int2StringFast( int var , char[] charsBase )
{
// 32 is the worst cast buffer size for base 2 and int.MaxValue
int j= 32 ;
char[] buffrReader =new char[j] ;
int baseTarget= charsBase.Length ;
do
{
buffrReader[--j] = charsBase[var % baseTarget] ;
var = var / baseTarget ;
}
while ( var > 0 ) ;
char[] output =new char[ 32 - j ] ;
Array.Copy( buffrReader , j , output , 0 , 32 - j ) ;
return new string ( output ) ;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.