6 Replies Latest reply on Feb 17, 2008 11:50 PM by EJP

# How to get value of 7 bit number?

Hi,
I'm facing trouble in getting the value of 7 bit number.
It would be 128 base.
if 7 bits are like that
0000001 0000010 ----result would be 82

my code is -

public static String longTagVal(byte[] bytearr, int start) {
int k = 0;
int bits[] = new int[8];
while(true) {
for (int i=0; i<8; i++) {
bits[i] = (bytearr[start+k] & (0x01 << i)) >> i;
}
String sum = "";
for (int i = 0; i <7; i++) {
sum+=bits[6-i];
}
int res = 0;
byte tt = Byte.parseByte(sum,2);
for(int i = 0, j = 0 ; i <2 ; i++ , j++) {
res += Math.pow((double)128, (double)j) * (tt & 0xFF);
}
String str = Integer.toHexString(res);
System.out.println(str);
k++;
if(bits[7]== 0) {
break;}
}
return str;
}
• ###### 1. Re: How to get value of 7 bit number?
more_java wrote:
Hi,
I'm facing trouble in getting the value of 7 bit number.
It would be 128 base.
Are you that it is base 128?
if 7 bits are like that
0000001 0000010 ----result would be 82
That is 14 bits, and I don't see how you end up with 82. Care to explain?

Kaj
• ###### 2. Re: How to get value of 7 bit number?
sorry, the result is 82 in HEX
I don't know exactly how come 82 in HEX.
• ###### 3. Re: How to get value of 7 bit number?
maybe try Integer.parseInt("yourstring",2)
• ###### 4. Re: How to get value of 7 bit number?
more_java wrote:
sorry, the result is 82 in HEX
I don't know exactly how come 82 in HEX.
I'm still confused.
0000001 0000010 ----result would be 82
What you posted is 82 in hex (130 in decimal) if you count the 8 least significant bits in base 2.

Why do you think it's base 128, and why do you think it's 7 bits?

Kaj
• ###### 5. Re: How to get value of 7 bit number?
I've solved this , Thank you.
• ###### 6. Re: How to get value of 7 bit number?
if 7 bits are like that
0000001 0000010 ----result would be 82
That's 14 bits, and the value of the low-order 7 bits is 2, not 0x82.