我有一条已签名的消息,附加了消息长度:
I agree all the terms and conditions that have been mentioned in the contract. 3AACBCB0C8FA6A23C72ACBECDE626AC7CD09C9B514CB47E8263150608E915EC2F64F7A13AA7BFEFC49FF2FDE74C0E1F34B33AA964BD03858D67966199A19D4DA5A57566812694AC1B79835EC8A1893DD5CEA709F5B59CB966C6D068837406AB16C762494BD4C9BC0A66E23B5219932FB2DFC82A62D392C0F3C23C793855AAF6C0078
这里的78只是消息的长度。 我需要借助消息长度从签名消息中提取消息。 我在 java(netbeans) 中使用了以下代码:
int MsgLen = Integer.parseInt(Msg.substring(Msg.length() - 4));
System.out.println("Message length: "+MsgLen);
但是我收到这样的错误:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "78"
如果有人可以帮助我解决问题,请尽快发布你的答案......
最佳答案
很高兴您修复了它。
编辑:我错误地认为需要删除前导零。 Integer.parseInt 为我们处理这个问题。您的原始代码实际上适用于您的示例消息。自包含示例:
public class IntegerParseTest {
public static void main(String[] args) {
String Msg = "3AACBCB0C8FA6A23C72ACBECDE626AC7CD09C9B514CB47E8263150608E915EC2F64F7A13AA7BFEFC49FF2FDE74C0E1F34B33AA964BD03858D67966199A19D4DA5A57566812694AC1B79835EC8A1893DD5CEA709F5B59CB966C6D068837406AB16C762494BD4C9BC0A66E23B5219932FB2DFC82A62D392C0F3C23C793855AAF6C0078";
Integer MsgLen = Integer.parseInt(Msg.substring(Msg.length() - 4));
System.out.println("Message length: "+MsgLen);
}
}
结果:
Message length: 78
我猜你的消息有尾随空白,这在问题中并不明显。不管怎样,很高兴你修复了它。
关于java - 线程中的异常 "AWT-EventQueue-0"java.lang.NumberFormatException : For input string: "78 error in java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11536976/