java - Converting binary to decimals and characters -
i want convert binary decimals , characters this:
11010 --> 1101 + 0(parity bit) -->decimals= 11 --> char ";" 10101 --> 1010 + 1 -->decimals= 5 --> char "5" . . public class stringek { string bitek = "1101010101001000001000001"; string[] bits; string four; char par; int parity; string digits; int n = 0; int b; int kurens; int decimalis; int digit; public stringek() { this.kurens = 0; bits = new string[200]; (int = 0; < 25; += 5) { bits[n] = bitek.substring(i, + 5); n++; } (int = 0; < n; ++i) { int j = 0; 4 = bits[i].substring(j, j + 4); (int p = 0; p < 4; ++p) { b = integer.parseint(four.substring(p)); kurens += b; } par = bits[i].charat(j+4); //system.out.print(par); parity = par-'0'; decimalis = integer.parseint(four, 2); digit = decimalis + 48; if ((kurens + parity) % 2 == 0) { system.out.println("binarys: "+four+"-"+par+" = "+"'"+(char)digit+"'"+" decimalis:"+decimalis+" parity <invalid> "); } else{ system.out.println("binarys: "+four+"-"+par+" = "+"'"+(char)digit+"'"+" decimalis:"+decimalis+" parity <valid> "); } } } }
but program results this:
binarys: 1101-0 = '=' decimalis:13 parity <invalid> binarys: 1010-1 = ':' decimalis:10 parity <valid> binarys: 0010-0 = '2' decimalis:2 parity <invalid> binarys: 0001-0 = '1' decimalis:1 parity <invalid> binarys: 0000-1 = '0' decimalis:0 parity <valid>
can me resolve? have cause in case parity valid, don't know why here parity invalid (i know cause results if give me results, want know how resolve valid when valid , invalid when invalid). thanks
public string[] splitstringevery(string s, int interval) { int arraylength = (int) math.ceil(((s.length() / (double)interval))); string[] result = new string[arraylength]; int j = 0; int lastindex = result.length - 1; (int = 0; < lastindex; i++) { result[i] = s.substring(j, j + interval); j += interval; } //add last bit result[lastindex] = s.substring(j); return result; }
Comments
Post a Comment