将 base64 字符串转换为十进制时出错
error when converting base64 String to decimal
下面的方法将小数转换为 BASE64
字符串:
public static String id2base64String(String mediaId) {
String postId = "";
try {
long id = Long.parseLong(mediaId);
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
while (id > 0) {
long remainder = (id % 64);
id = (id - remainder) / 64;
postId = alphabet.charAt((int) remainder) + postId;
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(String.format("input is %s, output is %s", mediaId, postId));
return postId;
}
我写了一个方法来反转函数,这样它就可以将 BASE64
字符串转换为小数:
public static String base64StringToId(String base64String) {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
long id = 0L;
for (int i = 0; i < base64String.length(); i++) {
char c = base64String.charAt(i);
int i1 = alphabet.indexOf(c);
id += i1 * Math.pow(64, base64String.length() - 1 - i);
//converted[string.length() - i - 1] = i1;
}
System.out.println(String.format("input is %s, output is %s", base64String, String.valueOf(id)));
return String.valueOf(id);
}
当使用以下代码时运行:
String base64String = "ybyPRoQWzX";
String id = "908540701891980503";
id2base64String(id);
base64StringToId(base64String);
它显示输出为:
input is 908540701891980503, output is ybyPRoQWzX
input is ybyPRoQWzX, output is 908540701891980544
如您所见,第二种方法输出与预期不符(预期为 908540701891980503
,但我得到 908540701891980544
)。
有什么想法吗?
大数的浮点运算有一个小错误。我认为这样写会更连贯。
public static String base64StringToId(String base64String) {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
long id = 0L;
for (int i = 0; i < base64String.length(); i++) {
char c = base64String.charAt(i);
int i1 = alphabet.indexOf(c);
id = (id * 64) + i1;
//id += i1 * Math.pow(64, base64String.length() - 1 - i);
//converted[string.length() - i - 1] = i1;
}
System.out.println(String.format("input is %s, output is %s", base64String, String.valueOf(id)));
return String.valueOf(id);
}
下面的方法将小数转换为 BASE64
字符串:
public static String id2base64String(String mediaId) {
String postId = "";
try {
long id = Long.parseLong(mediaId);
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
while (id > 0) {
long remainder = (id % 64);
id = (id - remainder) / 64;
postId = alphabet.charAt((int) remainder) + postId;
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(String.format("input is %s, output is %s", mediaId, postId));
return postId;
}
我写了一个方法来反转函数,这样它就可以将 BASE64
字符串转换为小数:
public static String base64StringToId(String base64String) {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
long id = 0L;
for (int i = 0; i < base64String.length(); i++) {
char c = base64String.charAt(i);
int i1 = alphabet.indexOf(c);
id += i1 * Math.pow(64, base64String.length() - 1 - i);
//converted[string.length() - i - 1] = i1;
}
System.out.println(String.format("input is %s, output is %s", base64String, String.valueOf(id)));
return String.valueOf(id);
}
当使用以下代码时运行:
String base64String = "ybyPRoQWzX";
String id = "908540701891980503";
id2base64String(id);
base64StringToId(base64String);
它显示输出为:
input is 908540701891980503, output is ybyPRoQWzX
input is ybyPRoQWzX, output is 908540701891980544
如您所见,第二种方法输出与预期不符(预期为 908540701891980503
,但我得到 908540701891980544
)。
有什么想法吗?
大数的浮点运算有一个小错误。我认为这样写会更连贯。
public static String base64StringToId(String base64String) {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
long id = 0L;
for (int i = 0; i < base64String.length(); i++) {
char c = base64String.charAt(i);
int i1 = alphabet.indexOf(c);
id = (id * 64) + i1;
//id += i1 * Math.pow(64, base64String.length() - 1 - i);
//converted[string.length() - i - 1] = i1;
}
System.out.println(String.format("input is %s, output is %s", base64String, String.valueOf(id)));
return String.valueOf(id);
}