This question already has answers here:
java: convert binary string to int
(4 answers)
Closed 6 years ago.
there is a string fill with 0 and 1,like String s = "10000000", which length is 8.And how can i transform it to a byte.such as "10000000"===>-128.
I try to use Byte.parseByte(s, 2),but get the error "Value out of range. Value:"10000000" Radix:2".So,how can i solve it.
You need to parse it as an Integer and then cast it to byte:
...
String s = "10000000";
int val = Integer.parseInt(s, 2);
byte b = (byte) val;
System.err.println(b);
...
Output:
-128
Related
This question already has answers here:
How can I convert a char to int in Java? [duplicate]
(4 answers)
Java: parse int value from a char
(9 answers)
Closed 3 years ago.
I was experimenting with char arrays and integers, when I found something that confused me a lot. In Java, if you type the following code:
String inputs = "123";
char[] inputs2 = inputs.toCharArray(); // Turn String into Char Array
int a = inputs2[0];
println(a);
I would have expected for "a" to return 1 because 1 is the first element in the array. However, it returned 49. Why does this occur? Is there any way to get around this issue?
This question already has answers here:
Java: Arithmetic operation on byte
(3 answers)
Why can not I add two bytes and get an int and I can add two final bytes get a byte?
(3 answers)
Closed 5 years ago.
Can anybody please clarify the below:
byte a = 10 + 20; // b=30;
-------------
byte b=10,c=20;
byte a = b + c; //error. Casting required.
What was the data type of result of addition in the first line before assigning it to 'a'? Do literals have a data type. Or do arithmetic result assign them one ?
since b + c is an operation that can overflow you need to do
byte a = (byte)(b + c);
This question already has answers here:
How can I convert a byte array into a double and back?
(6 answers)
Closed 8 years ago.
Using the following code to convert Flow_Rate from a 'double' to a byte array I received the output: [B#6a2b8b42
How do I check that the output is correct?
private double Flow_Rate= 8;
OFVendor of_vendor = new OFVendor();
byte [] rate = ByteBuffer.allocate(8).putDouble(Flow_Rate).array();
of_vendor.setData(rate);
Logger.stderr("ZB---->> ClientChannelWatcher::handleConnectedEvent OFVendor setData() : "+rate);
I assume your goal is to put the "bits" of your double into a binary buffer.
If so, your best bet is probably to use a ByteBuffer
import java.nio.ByteBuffer;
...
private double Flow_Rate= 8.0;
...
byte[] rate_buffer = new byte[8];
ByteBuffer.wrap(rate_buffer).putDouble(Flow_Rate);
...
PS:
[B#6a2b8b42 is just how any object is printed - it has nothing directly to do with the contents of your byte array.
This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
How to do an Integer.parseInt() for a decimal number?
(10 answers)
Closed 9 years ago.
How to convert string value into int? I am getting number format exception.
String s = "20.00";
int i = (Integer.parseInt(s));
System.out.println(i);
Result should be like i=20.
What about:
int i = (int) Double.parseDouble(s);
Of course, "20.00" is not in a valid integer format.
String s = "20.00";
is not valid Integer value that is the reason its throwing NumberFormatException.
Format your number using either Double or Float then using narrow casting cast you number to int but you may loose precision if exists.
i.e. int I = (int) Double.parseDouble(str);
This question already has answers here:
Java code To convert byte to Hexadecimal
(23 answers)
Closed 9 years ago.
i m new to java. i want to convert a byte array of decimal value to hexadecimal string.
my input byte array is [0, 0, 0, 0, 0, 0, 1, -28]. i m getting 00000000000001e4 instead of 0000001e4. plz help me to solve this problem
public static String ConvetToHex(byte[] decValue)
{
String value = "";
for(int i = 0;i<decValue.length;i++)
{
value = value+ Integer.toString((decValue[i] & 0xff) + 0x100, 16).substring(1);
}
return value;
}
It looks correct to me. Eight bytes should turn into 16 hex characters. You can use
return new BigInteger(1, decValue).toString(16);
but it will produce the same output.