In Java we have multiple ways to convert String to long. We will see a few of those ways in this post.
1. Convert String to long using Long.parseLong()
1 2 |
String str1 = "500"; long convertedLong = Long.parseLong(str1); |
If the String value doesn’t contain all digits OR if the String value has all digits but exceeds the max limit that can be allowed in an Long, then during converting String to long, a NumberFormatException is thrown. So you may want to handle this exception when converting String to long.
A String can also be converted to long using a radix specified by
public static long parseLong(String s,int radix)
See Examples
1 2 3 4 5 6 |
parseLong("0", 10) returns 0 parseLong("500", 10) returns 500L parseLong("+42", 10) returns 42L parseLong("-0", 10) returns 0L parseLong("-FF", 16) returns -255L parseLong("1100110", 2) returns 102L |
In the examples the 2nd param we are passing is the format of String we have received.
10 – regular
16- Hexadecimal
2- Binary
Complete Example
Lets see an example that will use both the methods to convert string to long.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.kscodes.sampleproject; public class StringToLongExample { public static void main(String[] args) { // Convert regular String String str1 = "500"; try { long convertedStr1 = Long.parseLong(str1); System.out.println("After converting Regular String :: " + convertedStr1); } catch (NumberFormatException nfe) { System.out.println("Invalid String!!!!"); } // Convert Hexadecimal String String str2 = "FFE"; try { long convertedStr2 = Long.parseLong(str2, 16); System.out.println("After converting Hexadecimal String :: " + convertedStr2); } catch (NumberFormatException nfe) { System.out.println("Invalid String!!!!"); } // Convert Binary String String str3 = "1101001"; try { long convertedStr3 = Long.parseLong(str3, 2); System.out.println("After converting Hexadecimal String :: " + convertedStr3); } catch (NumberFormatException nfe) { System.out.println("Invalid String!!!!"); } // Give illegar value String str4 = "500xxxx"; try { long convertedStr4 = Long.parseLong(str4); System.out.println("After converting String :: " + convertedStr4); } catch (NumberFormatException nfe) { System.out.println("Invalid String !!!!"); } } } |
Output
1 2 3 4 |
After converting Regular String :: 500 After converting Hexadecimal String :: 4094 After converting Hexadecimal String :: 105 Invalid String !!!! |
2. Convert String to Long using Long.valueOf()
Similar to Long.parseLong(), we can convert String to Long using Long.valueOf().
We can also convert the hexadecimals or binary values using Long.valueOf(String s,int radix)
We will see a small example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kscodes.sampleproject; public class StringToLongExample { public static void main(String[] args) { // Convert regular String String str1 = "500"; try{ Long convertedStr1 = Long.valueOf(str1); System.out.println("After converting Regular String :: " + convertedStr1); }catch(NumberFormatException nfe){ System.out.println("Invalid String!!!!"); } } } |
Difference between Long.parseLong and Long.valueOf()
Long.parseLong() returns a primitive long while Long.valueOf() returns a new Long() object
Long.valueOf() internally uses Long.parseLong(), hence if you want a primitive value you should use Long.parseLong().
Other methods to Convert String to long
There are many other ways to convert string to long. Few of them are
1 2 |
Long.getLong(String) Long.decode(String) |
References
1. Long API