NumberFormatException on valid number String(有效数字字符串上的 NumberFormatException)
问题描述
我已经看到了一些关于此的其他问题,但错误与字符串中的前导 0 有关.不幸的是,这不是我的情况.
I have seen some other questions about this but the errors were related to a leading 0 in the string. This unfortunately is not my case.
我从外部源接收 base64 格式的加密数据,然后我对其进行解码(使用包含的 Base64 库,因为 android sdk 版本是 7),解密消息,毕竟我有一个简单的字符串数字格式.
I am receiving encrypted data from an external source in base64 format, I then decode it (using an included Base64 library because the android sdk version is 7), decrypt the message, and after all that I have a simple string in a number format.
当我尝试将其转换为 Long
或 Integer
时,我收到此错误:
When I try to cast it to Long
or Integer
I get this error:
java.lang.NumberFormatException: Invalid long: "2551122"
at java.lang.Long.invalidLong(Long.java:125)
at java.lang.Long.parse(Long.java:362)
at java.lang.Long.parseLong(Long.java:353)
at java.lang.Long.parseLong(Long.java:319)
at com.nzn.lol.LoginActivity$LoginTask.doInBackground(LoginActivity.java:98)
at com.nzn.lol.LoginActivity$LoginTask.doInBackground(LoginActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
检查输入我使用打印,它确实是字符串2551122".当我尝试检查是否相等时,它也不正确
To check the input I used prints and it really is the string "2551122". When I try to check for equality, it is also not correct
"2551122".equals(numberAsString) // Gives me false
我认为这是一个编码问题,并尝试获取解码后的字节并以几种编码创建字符串,还尝试使用这些相同的几种编码从 base64 字符串中解码字节,但仍然不知道是什么导致了这个错误.
I thought it was an encoding issue and tried taking the decoded bytes and creating strings in several encodings, also tried to decode the bytes from the base64 string with these same several encodings and still have no idea of what is causing this error.
感谢您的帮助
更新
这是解密字符串的代码(Encryptor类):
This is the code for decrypting the string (Encryptor class):
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance(encryptionAlgorithim);
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iVector));
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public String decrypt(String encryptedString, String key) {
byte[] keyBytes = key.getBytes();
byte[] decoded = Base64.decode(encryptedString); // Decodes the string from base64 to byte[]
byte[] result = decrypt(keyBytes, decoded);
return new String(result);
}
这是引发错误的方式:
Encryptor encryptor = new Encryptor();
Long.parseLong(encryptor.decrypt(base64String, secretKey)) // Throws me the error
推荐答案
明文可能包含看起来像 ASCII 数字但不是 ASCII 数字的字符.请参阅 http://www.fileformat.info/info/unicode/category/nd/list.htm 用于非 ASCII 数字的数字列表.
The clear text probably contains characters that look like ASCII digits, but are not ASCII digits. See http://www.fileformat.info/info/unicode/category/Nd/list.htm for a list of digits which are not ASCII digits.
为了确认,对解密后的文本和硬编码的长字符串执行以下方法,并比较结果:
To confirm that, execute the following method on the decrypted text and on the hard-coded long as string, and compare the results:
public static String displayCharValues(String s) {
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
sb.append((int) c).append(",");
}
return sb.toString();
}
明文似乎以 BOM(字节顺序标记)开头是一个隐形字符.
it appears that the clear text starts with a BOM (byte order mark) which is an invisible character.
这篇关于有效数字字符串上的 NumberFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有效数字字符串上的 NumberFormatException
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01