Hex To binary - Using CharAt() with digits and appending numbers in a loop -
i tasked manually turning hex codes binary pretty much have working except errors whenever tries turn integer binary illustration turning hex number 1, makes stop if have abcdefabc, everythings runs if have abcdef123, stops @ f , gives me 88 reason
any insight appreciated
this code:
string hex2="abcdef123"; system.out.println("newlooptest"); stringbuilder hexstring = new stringbuilder(); (int x = 0; x <= 8; x++) { if (hex2.charat(x) == 'a') { hexstring.append(1010); } else if (hex2.charat(x) == 'b') { hexstring.append(1011); } else if (hex2.charat(x) == 'c') { hexstring.append(1100); } else if (hex2.charat(x) == 'd') { hexstring.append(1101); } else if (hex2.charat(x) == 'e') { hexstring.append(1110); } else if (hex2.charat(x) == 'f') { hexstring.append(1111); } //works here else if (hex2.charat(x) == '0') { hexstring.append(0000); } else if (hex2.charat(x) == '1') { hexstring.append(0001); } else if (hex2.charat(x) == '2') { hexstring.append(0010); } else if (hex2.charat(x) == '3') { hexstring.append(0011); } else if (hex2.charat(x) == '4') { hexstring.append(0100); } else if (hex2.charat(x) == '5') { hexstring.append(0101); } else if (hex2.charat(x) == '6') { hexstring.append(0110); } else if (hex2.charat(x) == '7') { hexstring.append(0111); } else if (hex2.charat(x) == '8') { hexstring.append(1000); } else if (hex2.charat(x) == '9') { hexstring.append(1001); } else { system.out.println("error @ char" + x ); } } system.out.println("hex decimal " + hexstring.tostring());
3994433
it because appending numeric values instead of strings stringbuilder. quote binary value.
string hex2="abcdef123"; system.out.println("newlooptest"); stringbuilder hexstring = new stringbuilder(); (int x = 0; x <= 8; x++) { if (hex2.charat(x) == 'a') { hexstring.append("1010"); } else if (hex2.charat(x) == 'b') { hexstring.append("1011"); } else if (hex2.charat(x) == 'c') { hexstring.append("1100"); } else if (hex2.charat(x) == 'd') { hexstring.append("1101"); } else if (hex2.charat(x) == 'e') { hexstring.append("1110"); } else if (hex2.charat(x) == 'f') { hexstring.append("1111"); } else if (hex2.charat(x) == '0') { hexstring.append("0000"); } else if (hex2.charat(x) == '1') { hexstring.append("0001"); } else if (hex2.charat(x) == '2') { hexstring.append("0010"); } else if (hex2.charat(x) == '3') { hexstring.append("0011"); } else if (hex2.charat(x) == '4') { hexstring.append("0100"); } else if (hex2.charat(x) == '5') { hexstring.append("0101"); } else if (hex2.charat(x) == '6') { hexstring.append("0110"); } else if (hex2.charat(x) == '7') { hexstring.append("0111"); } else if (hex2.charat(x) == '8') { hexstring.append("1000"); } else if (hex2.charat(x) == '9') { hexstring.append("1001"); } else { system.out.println("error @ char" + x ); } } system.out.println("hex decimal " + hexstring.tostring());
loops binary append charat
No comments:
Post a Comment