How to resolve the algorithm Non-decimal radices/Convert step by step in the D programming language
How to resolve the algorithm Non-decimal radices/Convert step by step in the D programming language
Table of Contents
Problem Statement
Number base conversion is when you express a stored integer in an integer base, such as in octal (base 8) or binary (base 2). It also is involved when you take a string representing a number in a given base and convert it to the stored integer form. Normally, a stored integer is in binary, but that's typically invisible to the user, who normally enters or sees stored integers as decimal.
Write a function (or identify the built-in function) which is passed a non-negative integer to convert, and another integer representing the base. It should return a string containing the digits of the resulting number, without leading zeros except for the number 0 itself. For the digits beyond 9, one should use the lowercase English alphabet, where the digit a = 9+1, b = a+1, etc. For example: the decimal number 26 expressed in base 16 would be 1a. Write a second function which is passed a string and an integer base, and it returns an integer representing that string interpreted in that base. The programs may be limited by the word size or other such constraint of a given language. There is no need to do error checking for negatives, bases less than 2, or inappropriate digits.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Non-decimal radices/Convert step by step in the D programming language
Source code in the d programming language
import std.stdio, std.conv, std.string, std.ascii;
void main() {
"1abcd".to!int(16).writeln;
writeln(60_272_032_366.to!string(36, LetterCase.lower), ' ',
591_458.to!string(36, LetterCase.lower));
}
import std.stdio, std.array, std.ascii;
immutable string mDigits = digits ~ lowercase;
ulong atoiRadix(in string str, in uint radix=10, int* consumed=null)
nothrow {
static int dtoi(in char dc, in uint radix) nothrow {
static int[immutable char] digit;
immutable char d = dc.toLower;
if (digit.length == 0) // Not init yet.
foreach (i, c; mDigits)
digit[c] = i;
if (radix > 1 && radix <= digit.length &&
d in digit && digit[d] < radix)
return digit[d];
return int.min; // A negative for error.
}
ulong result;
int sp;
for (; sp < str.length; sp++) {
immutable int d = dtoi(str[sp], radix);
if (d >= 0) // Valid digit char.
result = radix * result + d;
else
break;
}
if (sp != str.length) // Some char in str not converted.
sp = -sp;
if (consumed !is null) // Signal error if not positive.
*consumed = sp;
return result;
}
string itoaRadix(ulong num, in uint radix=10) pure nothrow
in {
assert(radix > 1 && radix <= mDigits.length);
} body {
string result;
while (num > 0) {
immutable uint d = num % radix;
result = mDigits[d] ~ result;
num = (num - d) / radix;
}
return result.empty ? "0" : result;
}
void main() {
immutable string numStr = "1ABcdxyz???";
int ate;
writef("'%s' (base %d) = %d", numStr, 16,
atoiRadix(numStr, 16, &ate));
if (ate <= 0)
writefln("\tConverted only: '%s'", numStr[0 .. -ate]);
else
writeln();
writeln(itoaRadix(60_272_032_366, 36), " ",
itoaRadix(591_458, 36));
}
import std.stdio, std.algorithm, std.ascii, std.array, std.string;
alias Digits = ubyte[];
Digits toBase(ulong number, in ubyte base) pure nothrow @safe {
Digits result;
while (number) {
result = number % base ~ result;
number /= base;
}
return result;
}
enum fromBase = (in Digits digits, in ubyte base) pure nothrow @safe @nogc =>
reduce!((n, k) => n * base + k)(0UL, digits);
immutable myDigits = digits ~ lowercase;
enum fromDigits = (in Digits digits) pure nothrow /*@safe*/ =>
digits.map!(d => myDigits[d]).array;
enum convert = (in dchar d) pure nothrow @safe @nogc =>
cast(ubyte)(d.isDigit ? d - '0' : std.ascii.toLower(d) - 'a' + 10);
enum toDigits = (in string number) pure nothrow @safe =>
number.representation.map!convert.array;
void main() {
"1ABcd".toDigits.fromBase(16).writeln;
}
You may also check:How to resolve the algorithm Numerical integration step by step in the Ruby programming language
You may also check:How to resolve the algorithm Pentagram step by step in the Processing programming language
You may also check:How to resolve the algorithm Logical operations step by step in the Swift programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the FunL programming language
You may also check:How to resolve the algorithm Check input device is a terminal step by step in the FutureBasic programming language