NumberFormatException异常的有效的数字串 [英] NumberFormatException on valid number String

查看:166
本文介绍了NumberFormatException异常的有效的数字串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这个其他一些问题,但该错误是在字符串中涉及到的主要0。这不幸的是,不是我的情况。

我是从外部来源base64格式接收加密数据,然后我去code它(使用包含的Base64库,因为Android的SDK版本为7),解密消息,毕竟我有简单字符串的数字格式。

当我尝试将其转换为整数我得到这个错误:

  java.lang.NumberFormatException:无效的长:2551122
    在java.lang.Long.invalidLong(Long.java:125)
    在java.lang.Long.parse(Long.java:362)
    在java.lang.Long.parseLong(Long.java:353)
    在java.lang.Long.parseLong(Long.java:319)
    在com.nzn.lol.LoginActivity $ LoginTask.doInBackground(LoginActivity.java:98)
    在com.nzn.lol.LoginActivity $ LoginTask.doInBackground(LoginActivity.java:1)
    在android.os.AsyncTask $ 2.call(AsyncTask.java:264)
    在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
    在java.util.concurrent.FutureTask.run(FutureTask.java:137)
    在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
 

要检查输入我用版画,它确实是字符串2551122。 当我尝试检查平等,这也是不正确的。

 2551122.equals(numberAsString)//给我假
 

我认为这是一个编码的问题,并试图以去codeD字节,并创造了几个编码字符串,也试图去code从Base64编码字符串,这些相同的多个编码的字节,还是没有想法是什么原因造成这个错误。

请任何帮助AP preciated

更新

这是在code解密的字符串(加密器类):

 私有静态的byte []解密(byte []的原料,byte []的加密)抛出异常{
    SecretKeySpec skeySpec =新SecretKeySpec(原AES);
    密码加密= Cipher.getInstance(encryptionAlgorithim);
    cipher.init(Cipher.DECRYPT_MODE,skeySpec,新IvParameterSpec(iVector));
    byte []的解密= cipher.doFinal(加密);
    返回解密;
}

公共字符串解密(字符串encryptedString,字符串键){

    byte []的keyBytes = key.getBytes();
    byte []的德codeD = Base64.de code(encryptedString); //德codeS从BASE64字符串为byte []
    byte []的结果=解密(keyBytes,德codeD);
    返回新的字符串(结果);
}
 

这是错误是如何提高:

 加密器加密=新的加密器();
的Long.parseLong(encryptor.decrypt(base64String,SecretKey的))//抛出我的错误
 

解决方案

明文可能包含看起来像ASCII数字,但不是ASCII数字字符。请参阅<一href="http://www.fileformat.info/info/uni$c$c/category/Nd/list.htm">http://www.fileformat.info/info/uni$c$c/category/Nd/list.htm对于数字哪些不是ASCII数字列表。

要确认的是,在解密的文字和对硬codeD只要字符串执行下面的方法,并比较结果:

 公共静态字符串displayCharValues​​(String s)将{
    StringBuilder的SB =新的StringBuilder();
    对于(字符C:s.toCharArray()){
        sb.append((int)的三).append(,);
    }
    返回sb.toString();
}
 

编辑:看来,明文与BOM(字节顺序标记),这是一个不可见的字符开始。

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.

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)

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

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.

Please any help is appreciated

UPDATE

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);
}

This is how the error is raised:

Encryptor encryptor = new Encryptor();
Long.parseLong(encryptor.decrypt(base64String, secretKey)) // Throws me the error

解决方案

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();
}

EDIT: it appears that the clear text starts with a BOM (byte order mark) which is an invisible character.

这篇关于NumberFormatException异常的有效的数字串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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