One or two binary and decimal conversions
/** * Conversion between decimal and binary */public class Inttobinary {public static void main (string[] args) {int a =-23422; String str; System.out.println ("====================== uses his own method of writing ============================="); str = inttobinarystring (a); System.out.println (str); SYSTEM.OUT.PRINTLN (Binarystringtoint (str)); System.out.println ("====================== uses the method ============================= in integer"); str = integer.tobinarystring (a); System.out.println (str);//System.out.println (Integer.parseint (binarystring, 2)); Can only convert positive numbers, convert negative error System.out.println (Integer.parseunsignedint (str, 2)); }/** * int to binary string * * algorithm one: except 2 * positive number: minus 2 and reverse * negative (complement): First plus 1--> converted to positive number--and 2 to remove, and to take back----high-1 --Reverse * @param num integer */public static String inttobinarystring (int num) {StringBuilder StringBuilder = new StringBuilder (); Boolean flag = false; Flag: TRUE indicates a negative int rem; if (num= = 0) {return "0"; }else if (num<0) {flag= true; Num= Math.Abs(num + 1); } while (num! )= 0) { //positive number, remain unchanged, negative, take anti-REM= (!flag&& num% 2== 0) | | (Flag && num% 2== 1)? 0:1; Num= num/2; Stringbuilder.append (REM); }//To determine if it is a negative number, and if it is negative, all preceding bits are 1 if (flag) {num= Stringbuilder.length (); for (int i= 1;I <=-num; i++) {Stringbuilder.append ("1"); }} return Stringbuilder.reverse (). toString (); }/** * int to binary string * * Algorithm two: Each bit of the original data and 1 is performed and calculated, judging 1 and 0 * @param num integer */public static string Inttobin aryString2 (int num) {StringBuilder StringBuilder= newStringBuilder (); Boolean flag= false; //Flag: TRUE indicates negative for (Int J=31;j>=0;j--) {if ((1<< J) & num)!= 0) { Flag= true; stringbuilder.append ("1"); } else{if (flag) {Stringbuilder.append ("0"); }}} return Stringbuilder.tostring (); }/** * binary string to int * * Algorithm one: each digit * 2 n-1, and add * @param binarystr binary String */public static int Bina Rystringtoint (String binarystr) {int result=0,rem;////positive sequence traversal//for (int i= 0;i < binarystr.length (); i++) {//char c= Binarystr.charat (i);//REM= C-' 0 ';//////Use Math.pow () method to calculate the n-1////result + of 2= REM* (int) Math.pow (2, (Binarystr.length ()-1-i))//////Use displacement to calculate the n-1 of 2 per//result += REM<< (Binarystr.length ()-1-i);//}//Reverse traversal int _pow= 1; for (int i= Binarystr.length ()-1; I>= 0; i--) {Char c = binarystr.charat (i); rem = C-' 0 '; Result + = Rem * _pow;//_pow = (int) Math.pow (2, (Binarystr.length ()-i)); Use the Math.pow () method to calculate the n-1 _pow = _pow of 2<< 1; Use displacement to calculate 2 n-1} return result; }/** * binary string to int * * Algorithm two: Use binary to decimal inverse: divisor= Divisor* quotient + remainder * @param binarystr binary String */public static int BinaryStringToInt2 (String binarystr) {int RE Sult=0,REM, temp; for (int i= 0;i < binarystr.length (); i++) {char c= Binarystr.charat (i); REM= C-' 0 '; Calculates a binary value of 0 or 1//using binary to decimal inverse: divisor= Divisor* quotient + remainder result= result* 2 + rem; } return result; }}
26 or 16 binary and decimal conversions
Package Com.wslook.algorithm.radix;public class Inttohexstring {public static void main (string[] args) {int a = 23422; String str; System.out.println ("====================== uses his own method of writing ============================="); str = inttohexstring (a); System.out.println (str);//str = "ffffa482"; SYSTEM.OUT.PRINTLN (HexStringToInt2 (str)); System.out.println ("====================== uses the method ============================= in integer"); str = integer.tohexstring (a); System.out.println (str);//System.out.println (Integer.parseint (binarystring, 16)); Can only convert positive numbers, convert negative error System.out.println (Integer.parseunsignedint (str, 16)); }/** * int to hexadecimal string * * Algorithm one: first find binary string, then convert to hexadecimal string * @param num integer */public static string Inttoh exstring (int num) {String binarystr = inttobinary.inttobinarystring (num); binary string to hexadecimal string int count = Binarystr.length (); StringBuilder sb = new StringBuilder (); String subStr; while (Count > 0) {if (count<= 4) {subStr= binarystr.substring (0,count); }else {subStr= binarystr.substring (Count-4, count); } Count-= 4; int C= Inttobinary.binarystringtoint2 (SUBSTR); Sb.append (trans (c)); } return Sb.reverse (). toString (); }/** * Integer to hexadecimal string * @param deci less than 16 positive integer * @return */private static char trans (int deci) {i F (Deci <) {return (char) (Deci + 48); }//return (char) (Deci + 55); Turn into uppercase A-f return (char) (Deci + 87); Convert to lowercase a-f}/** * Hex string to int * * Algorithm one: n-1 of each digit * 2 and add * @param str hexadecimal string */public static int Hexstringtoint (String str) {int result= 0,REM= 0,_pow= 1; //positive sequence traversal string for (int i= 0;i < str.length (); i++) {Char hex= Str.charat (i); if (hex>= ' 0 ' && hex<= ' 9') {REM= Hex-48; Convert to Digital 0-9}else if (hex>= ' A ' && hex<= ' F') {REM= Hex-87; Convert to Digital 10-15, ' a '=}else if (hex>= ' A ' && hex<= ' F') {REM= Hex-55; Convert to Digital 10-15, ' A '=}//Use displacement to calculate 16 of the n-1-----"16 of the N-th-square= 1<< 4n "_pow= (int)Math.pow (Str.length ()-1-i));//_pow= 1<< (4 * (Str.length () -1-i)); Result += REM* _POW; }////Reverse traversal string//for (int i= Str.length ()-1; I>= 0; i--) {//char hex = Str.charat (i);//if (hex >= ' 0 ' && hex<= ' 9') {//REM= Hex-48;//}else if (hex>= ' A ' && hex<= ' F') {//REM= Hex-87;//}else if (hex>= ' A ' && hex<= ' F') {//REM= Hex-55;//}//////Use displacement to calculate 16 of the n-1-----"16 of the N-th-square= 2<< (4n-1) "//result += REM* _pow;////_pow= (int)Math.pow (Str.length ()-i));//_pow= _pow<< 4;//} return result; }/** * Hex string to int * * Algorithm two: use hexadecimal to decimal inverse: divisor= Divisor* quotient + remainder * @param str hexadecimal string */public static int HexStringToInt2 (String str) {int result=0,REM= 0,temp; for (int i= 0;i < str.length (); i++) {Char hex= Str.charat (i); if (hex>= ' 0 ' && hex<= ' 9') {REM= Hex-48; Convert to Digital 0-9}else if (hex>= ' A ' && hex<= ' F') {REM= Hex-87; Convert to Digital 10-15, ' a '=}else if (hex>= ' A ' && hex<= ' F') {REM= Hex-55; Convert to Digital 10-15, ' A '=}//inverse with hexadecimal to decimal: divisor= Divisor* quotient + remainder result= result* + rem; } return result; }}
binary, hexadecimal, and decimal conversion algorithms