Java checksum method [closed] - java

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)

Related

How to convert int to byte in Java? [closed]

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

How to build an int value with bytes in Java? [closed]

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);

Put point in string value (android java) [closed]

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

Function floor of BigInteger [closed]

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.

Print without the excess 0's in a floating point? [closed]

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
I understand that you can set the decimal places being printed for a float by doing this %.2f but I want to print only significant figures:
1.33443
1.3
2.00006
Use this
double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}
public static void main(String [] args) {
float f1 = 1.3344300f;
float f2 = 1.3000f;
float f3 = 2.010f;
System.out.println(f1);;
System.out.println(f2);;
System.out.println(f3);;
}
Always prints...
1.33443
1.3
2.01

Categories

Resources