Does number.charAt() work on hexadecimals too? - java

Currently working with Java and wondering how to convert a string that represents hexadecimals into integers? Will number.charAt() suffice for this?

You could use Integer.parseInt(String, int) where the second argument is the radix.
int v = Integer.parseInt("A1", 16);
if you actually need to parse hex digits you could use Character.digit(char, int) where the second argument is (again) a radix
int v = Character.digit('a', 16);

Related

Java : Need to get the same functionality of the Integer.toHexString() with a String parameter and not an Int parameter due to NumberFormat Exception

I have this line of Java code that will throw NumberFormatException if the number represented as a String is above 2,147,483,647.
Because:
The int data type is a 32-bit signed two's complement integer. It has
a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647
Code Throwing the NumberFormatException:
String largeNumberAsAString = "9999999999";
Integer.toHexString(Integer.parseInt(largeNumberAsAString)); // NumberFormatException
How can I get the same functionality of theInteger.toHexString() with a String parameter and not an int parameter due to NumberFormatException?
Use BigInteger to avoid numeric limits of primitive int and long:
BigInteger x = new BigInteger("9999999999999999999999"); // Default radix is 10
String x16 = x.toString(16); // Radix 16 indicates hex
System.out.println(x16);
The class conveniently exposes a constructor that takes a String, which gets interpreted as a decimal representation of a number.
Demo.
If your input value can be arbitrarily large, then #dasblinkenlight's answer involving BigInteger is your best bet.
However, if your value is less than 263, then you can just use Long instead of Integer:
String dec = "9999999999";
String hex = Long.toHexString(Long.parseLong(dec));
System.out.println(hex); // 2540be3ff
Live demo.
Use Integer.parseUnsignedInt
When the number is above 2^31 but below 2^32, thus in the negative int range,
you can do:
int n = Integer.parseUnsignedInt("CAFEBABE", 16);
(I used hexadecimal here, as it is easier to see that above we are just in that range.)
However 9_999_999_999 is above the unsigned int range too.
Try this way:
String largeNumberAsAString = "9999999999";
System.out.println(Integer.toHexString(BigDecimal.valueOf(Double.valueOf(largeNumberAsAString)).intValue()));

Convert hexadecimal string to hexadecimal integer in java

I have hexadecimal String eg. "0x103E" , I want to convert it into integer.
Means String no = "0x103E";
to int hexNo = 0x103E;
I tried Integer.parseInt("0x103E",16); but it gives number format exception.
How do I achieve this ?
You just need to leave out the "0x" part (since it's not actually part of the number).
You also need to make sure that the number you are parsing actually fits into an integer which is 4 bytes long in Java, so it needs to be shorter than 8 digits as a hex number.
No need to remove the "0x" prefix; just use Integer.decode instead of Integer.parseInt:
int x = Integer.decode("0x103E");
System.out.printf("%X%n", x);

How to convert a byte in binary representation into int in java

I have a String[] with byte values
String[] s = {"110","101","100","11","10","1","0"};
Looping through s, I want to get int values out of it.
I am currently using this
Byte b = new Byte(s[0]); // s[0] = 110
int result = b.intValue(); // b.intValue() is returning 110 instead of 6
From that, I am trying to get the results, {6, 5, 4, 3, 2, 1}
I am not sure of where to go from here. What can I do?
Thanks guys. Question answered.
You can use the overloaded Integer.parseInt(String s, int radix) method for such a conversion. This way you can just skip the Byte b = new Byte(s[0]); piece of code.
int result = Integer.parseInt(s[0], 2); // radix 2 for binary
You're using the Byte constructor which just takes a String and parses it as a decimal value. I think you actually want Byte.parseByte(String, int) which allows you to specify the radix:
for (String text : s) {
byte value = Byte.parseByte(text, 2);
// Use value
}
Note that I've used the primitive Byte value (as returned by Byte.parseByte) instead of the Byte wrapper (as returned by Byte.valueOf).
Of course, you could equally use Integer.parseInt or Short.parseShort instead of Byte.parseByte. Don't forget that bytes in Java are signed, so you've only got a range of [-128, 127]. In particular, you can't parse "10000000" with the code above. If you need a range of [0, 255] you might want to use short or int instead.
You can directly convert String bindery to decimal representation using Integer#parseInt() method. No need to convert to Byte then to decimal
int decimalValue = Integer.parseInt(s[0], 2);
You should be using Byte b = Byte.valueof(s[i], 2). Right now it parse the string treating it as decimal value. You should use valueOf and pass 2 as radix.
Skip the Byte step. Just parse it into an int with Integer.parseInt(String s, int radix):
int result = Integer.parseInt(s[0], 2);
The 2 specifies base 2, whereas the code you're using treats the input strings as decimal.

How do you convert a binary string like "10010010101" to a BigInteger decimal representation?

I know that integers have Integer.parseInt(string, 2); but when the value of the decimal is very large, how can we use BigInteger in this case?
BigInteger(giantBinaryString, 2);
There's a constructor for that.
Use the constructor BigInteger(String s)
documentation: http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html
For a binary integer represented as a string, use
BigInteger(binaryString, 2);
In this case you can construct a BigInteger from a string which
denotes a very very large integer. This is the purpose of BigInteger.
If you don't use BigInteger it will overflow your int variable.
You can do this by calling.
BigInteger b = new BigInteger(str, radix);
where you pass in radix = 2 or radix = 10 or whatever
you need (seems you need 2 in your case) and str is the value.
Then you can use
b.toString(radix);
to output your BigInteger value in whatever radix you want.
I think you are looking for this BigInteger(binaryString, 2);

Parsing big hexadecimal numbers in Java

I'm trying to parse into int a String which is hexadecimal number in my code (FF00FF00, for example) using Integer.parseInt(String string, int radix), but always get NumberFormatException. If I parse the number without last two numbers (FF00FF) it works well.
Is there any method to parse such big numbers in Java?
If Integer is too small, use Long:
Long.parseLong(string, 16)
If Long is still too small, use BigInteger:
new BigInteger(string, 16)
I would use Long.parseLong(x, 16) BigInteger is overkill for a 32-bit value.
If you expect this value to be an int value you can cast the result.
int x = (int) Long.parseLong("FF00FF00", 16);

Categories