Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this integer in java:
int i = 1067030938;
byte b = (byte) i;
which gives me:
-102
How could I go from b back to i ?
I tried :
b = b & 0xff;
but this gives 154
If you have
int i = 1067030938;
And just want the low order 8 bits, then do
i &= 0xff;
System.out.println(i);
Prints
154
If you want the signed value of converted int then do this
i = (byte)i; // will be -102
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
My question is about Java int data structure.
If I have a buffer of 4 bytes
byte[] buffer = {(byte)A, (byte)B, (byte)C, (byte)D};
How to build a positive and a negative int values using this array and logic operators (&, |, <<, >>)?
Say int = 4 and int = 130; and -4 and -130.
to construct 32 bit value from four 8 bit values you need to know Endianness
if we assume that high bits are stored into first byte, then you can do:
int v = 0;
for (int i=0; i<4; i++)
v = (v<<8) | (buffer[i]&0xff);
if high bits are stored in last byte, then you need to reverse loop
int v = 0;
for (int i=3; i>=0; i--)
v = (v<<8) | (buffer[i]&0xff);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm new to programming. I have a task to make a division of two arbitrary numbers, and to set arbitrary number of decimals. I was searching on the internet, but not really sure how to set it. If I could get some help, would much appreciate!
Here's the code so far:
int a,b, decimala;
System.out.println("first number: ");
a = unos.nextInt();
System.out.println("second number: ");
b = unos.nextInt();
System.out.println("amount of decimals: ");
decimala = unos.nextInt();
double c;
System.out.println(a);
System.out.println(b);
System.out.println("--------------");
c = (double)a/b;
System.out.println(%.decimala+ c);
If you just want to output them you could try using format
String format = "%" + decimala + "f";
System.out.format(format,a);
Here's a cheat sheet with all the stuff you can do.
https://alvinalexander.com/programming/printf-format-cheat-sheet
Thanks to #AndrewGuerra for pointing out how to format a variable amount of decimals
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have BMI calculator app that takes some input and puts it in "EditText". Here is what I am trying to do:
If the input is 170, it will become 1.70.
If the input is 1.70, it will not change.
This is the code I have:
String weight = editText.getText().toString();
Cant you convert the string to an integer and take modulus 100 to the cms and divide by 100 to get in meters?
You can convert the String weight to int like this
int wgt = Integer.parseInt(weight);
Then separating meters and centimetres.
int mtrs = wgt / 100;
int cms = wgt % 100;
Then combining both
String result = mtrs + "." + cms;
try something like this
float value = Float.parse("170");
editText.setText(String.Format(Locale.ENGLISH,"%,02f", value/100f))
I found this way works: I take the number that the user put, and then "170/100" + "0" gives me 1.70
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm looking for a function such as Math.floor(int x) but for a variable of type BigInteger. Does anyone know how to do it?
Let's suppose a and b are int variables. In that case a/b is an int. Test case:
int a = 5;
int b = 2;
System.out.println(a / b); //expected output is 2
Let's suppose a and b are integers, but we intend to convert the result of the division into a float. The result depends on the way of conversion. Test case:
int a = 5;
int b = 2;
System.out.println((float)(a / b)); //expected output is 0.0
System.out.println(((float)a) / b); //expected output is 2.5
Let's suppose a and b are BigInteger variables. a.divide(b) will return a BigInteger value (so floor is not needed) unless b is 0 in which case it will throw an exception. Source.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
so I've been working on a checksum method where i take 4 binary numbers and convert them to decimal, add them together, covert the sum to binary again and calculate the checksum. Here is my code, i didn't know how to do the last part using java which is calculating the checksum
public static void main(String[] args) throws Exception{
String a = "1010000001000011";
String b = "1111100101001111";
String c = "1111111111101110";
String d = "1110101011111110";
int aa = Integer.parseInt(a, 2);
int bb = Integer.parseInt(b,2);
int cc = Integer.parseInt(c,2);
int dd = Integer.parseInt(d,2);
int f = aa + bb + cc + dd;
StringBuffer buf = new StringBuffer();
while (f!=0){
int digit = f%2;
buf.append(digit);
f=f/2;
}
buf.reverse();
}
To convert the number to a string again, you can use Integer.toString(number, 2). That results in a string on which you can call .hashCode() to get it's hash code / check sum.
EDIT:
Instead of Integer.toString(number, 2) you can use Integer.toBinaryString(number), which is a bit faster and also reads easier.
Try method:
//your code
...
int f = aa + bb + cc + dd;
Integer.toBinaryString(f)