NumberFormatException对有效数字字符串 [英] NumberFormatException on valid number String

查看:134
本文介绍了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.

当我尝试将其转换为整数我收到此错误:

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 class) :

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对有效数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆