×

Loading...

写了个验证,300=A0A1E0;

sxffff (lookingforjob)
本文发表在 rolia.net/zh 相约加拿大网上社区枫下论坛
public class Z0ToString {

public static void main(String[] args) {
for (int i = 0; i < 10000000; i++) {
String convertedValue = convertBaseTenToString(i);
System.out.println("i=" + i + " " + convertedValue);
if (i!= convertStringToBaseTen(convertedValue)) {
throw new RuntimeException("not equal: i = " + i + " convertStringToBaseTen(a)="+ convertStringToBaseTen(convertedValue));
}
}
}

static String convertBaseTenToString(int number) {
char[] digits = { 'A', '0', 'A', '0', 'A', '0' };
for (int i =digits.length -1; i>=0;i-- ) {
int divisor = (i%2 !=0) ? 10 : 26;
int a = (int)digits[i];
int b = (a+ number % divisor);
digits[i] = (char)b ;
number /= divisor;
}
return new String(digits);
}

static int convertStringToBaseTen(String number) {
int returnNumber=0;
int base =1 ;
for (int i = number.length()-1; i>=0; i--) {
if (i%2 ==0) {
int value = (number.charAt(i) -((int)'A'));
if (i!= number.length()-1 ) {
base = base * 10;
}
returnNumber = value* base + returnNumber;
} else {
int value = (number.charAt(i) -(int)'0');
if (i!= number.length()-1 ) {
base = base * 26;
}
returnNumber = value* base + returnNumber;
}
}
return returnNumber;
}
}
更多精彩文章及讨论,请光临枫下论坛. 网址: rolia.net/zh
(#9942852@0)
2016-2-13 -05:00
Reply
Page address has been copied.
To share, click to copy page address.
Share Online by QR Code

Back To Topic: Integer to String 的转换

Back To Forum: HOME枫下论坛枫下论坛主坛工作学习学科技术