简体   繁体   English

如何在没有 NumberFormatException 的情况下将 String 转换为 long?

[英]How to convert String to long without NumberFormatException?

I want to append these long variables, so I turned them into Strings first.我想追加这些长变量,所以我先把它们变成了字符串。 When I return them back into a long it throws a NumberFormatException.当我将它们返回到 long 时,它会抛出一个 NumberFormatException。

public long BLZ = 12345678l;
public long KNr = 1234567890l;
public String landCode = "1314";
long fedor = Long.parseLong(String.valueOf(BLZ) + String.valueOf(KNr) + 
landCode + "00"); 
long BLZ = 12345678l;
long KNr = 1234567890l;
String landCode = "1314";
System.out.println(Long.MAX_VALUE);
System.out.println(String.valueOf(BLZ) + String.valueOf(KNr) + landCode + "00"); 

leads to the output导致输出

9223372036854775807 9223372036854775807

123456781234567890131400 123456781234567890131400

The number you try to parse in is bigger than a long can handle.您尝试解析的数字大于long可以处理的数字。

Edit: You asked how to solve your problem.编辑:您询问如何解决您的问题。 In the comments of your question there is the theory that you want to calculate the checksum of an IBAN.在您的问题的评论中,有一种理论是您要计算 IBAN 的校验和。 You can do that by using java.math.BigInteger :你可以通过使用java.math.BigInteger来做到这一点:

System.out.println(Long.MAX_VALUE);
long BLZ = 12345678l;
long KNr = 1234567890l;
String landCode = "1314";
String val = String.valueOf(BLZ) + String.valueOf(KNr) + landCode + "00";
System.out.println(val); 
System.out.println(BigInteger.valueOf(98).subtract(new BigInteger(val).mod(BigInteger.valueOf(97))));

This leads to the following output:这导致以下输出:

9223372036854775807 9223372036854775807

123456781234567890131400 123456781234567890131400

87 87

Alternatively you can check the IBAN Documentation (it's german but that shouldn't be problem for you I suppose ;-) where in chapter 4 there is a description of a way to calculate the checksum if you're limited (like here).或者,您可以查看IBAN 文档(它是德语,但我想这对您来说应该不是问题;-) 在第 4 章中,如果您受到限制(如此处),则描述了一种计算校验和的方法。 You will still need longs to be able to implement that, because 9-digit numbers can exceed the range of an int .您仍然需要longs才能实现它,因为 9 位数字可能超出int的范围。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串到长NumberFormatException - String to Long NumberFormatException 异常:java.lang.NumberFormatException将String转换为long时 - Exception : java.lang.NumberFormatException When convert String to long Java - NumberFormatException。将long bynary string转换为dec - Java - NumberFormatException. Convert long bynary string to dec 我想将二进制字符串转换为相应的十六进制字符串,但是当输入的二进制字符串很长时,则会显示NumberFormatException。如何解决? - I want to convert a binary string to corresponding hex string.But when the input binary string long then NumberFormatException is shown.How to fix it? java.lang.NumberFormatException:对于输入字符串:“”用于将编辑文本中给出的字符串转换为长字符串 - java.lang.NumberFormatException: For input string: “” for convert string given from edit text to long 如何从字符串转换为整数? 使用valueOf()继续获取NumberFormatException; - How to convert from string to integer? Keep getting NumberFormatException using valueOf(); Spring NumberFormatException:无法将类型'java.lang.String'的值转换为所需的类型'java.lang.Long - Spring NumberFormatException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long NumberFormatException,同时将十六进制字符串转换为Long - NumberFormatException while converting Hexadecimal String to Long Scala NumberFormatException当将字符串转换为Long吗? - Scala NumberFormatException when converting a String to Long? Long的NumberFormatException - NumberFormatException for Long
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM