Почему решение через long не проходит? "abcdefabcdef" умещается в Long
public static Number convertNumberToOtherNumberSystem(Number number, NumberSystem expectedNumberSystem) {
long parsedNumber = Long.parseLong(number.getDigit(), number.getNumberSystem().getNumberSystemIntValue());
return convertTo(parsedNumber, expectedNumberSystem);
}
private static Number convertTo(long number, NumberSystem type) {
StringBuilder result = new StringBuilder();
while (number > 0) {
long lng = number % type.getNumberSystemIntValue();
if (lng > 9) {
result.append((char) (lng - 10 + 'a'));
} else {
result.append(lng);
}
number = number / type.getNumberSystemIntValue();
}
return new Number(type, result.reverse().toString());
}
package com.javarush.task.task30.task3001;
/*
Конвертер систем счислений
*/
public class Solution {
public static void main(String[] args) {
Number number = new Number(NumberSystemType._10, "6");
Number result = convertNumberToOtherNumberSystem(number, NumberSystemType._2);
System.out.println(result); //expected 110
number = new Number(NumberSystemType._16, "6df");
result = convertNumberToOtherNumberSystem(number, NumberSystemType._8);
System.out.println(result); //expected 3337
number = new Number(NumberSystemType._16, "abcdefabcdef");
result = convertNumberToOtherNumberSystem(number, NumberSystemType._16);
System.out.println(result); //expected abcdefabcdef
}
public static Number convertNumberToOtherNumberSystem(Number number, NumberSystem expectedNumberSystem) {
// //напишите тут ваш код
long parsedNumber = Long.parseLong(number.getDigit(), number.getNumberSystem().getNumberSystemIntValue());
return convertTo(parsedNumber, expectedNumberSystem);
}
private static Number convertTo(long number, NumberSystem type) {
StringBuilder result = new StringBuilder();
while (number > 0) {
long lng = number % type.getNumberSystemIntValue();
if (lng > 9) {
result.append((char) (lng - 10 + 'a'));
} else {
result.append(lng);
}
number = number / type.getNumberSystemIntValue();
}
return new Number(type, result.reverse().toString());
}
}