Code Simulation to convert hexadecimal to binary.
Public class TowAdecimal {
Private int sum; // used for conversion
Private static String s = "11"; // hexadecimal value
Public static void main (String [] args ){
TowAdecimal a = new TowAdecimal ();
A. char_value (s );
}
//------------------------------------------------------------
Public void char_value (String s ){
If (s = null) {// determines if the input is null. if the input is null, there is no need to perform the next operation, and the function is directly terminated.
System. out. println ("enter a hexadecimal string. null is not a string ");
Return;
}
For (int I = 0; I <s. length (); ++ I ){
Char c = s. charAt (s. length ()-1-i); // obtain the string from right to left.
If (int_value (c, I) {// This is based on the input character. if one of the characters is incorrect, the function is terminated and no error results are output.
Return;
}
}
If (sum = 0 ){
System. out. println (0 );
} Else {
Sum_2 (sum );
}
}
// ------------------------------------ Parses the string into an integer value ----------------
Public boolean int_value (char c, int N_Ci_Fang) {// convert the character to an integer value.
Int I = 0;
If (c = 'A '){
I = 10;
} Else if (c = 'B '){
I = 11;
} Else if (c = 'C '){
I = 12;
} Else if (c = 'D '){
I = 13;
} Else if (c = 'E '){
I = 14;
} Else if (c = 'F '){
I = 15;
} Else if (c = '0 '){
I = 0;
} Else if (c = '1 '){
I = 1;
} Else if (c = '2 '){
I = 2;
} Else if (c = '3 '){
I = 3;
} Else if (c = '4 '){
I = 4;
} Else if (c = '5 '){
I = 5;
} Else if (c = '6 '){
I = 6;
} Else if (c = '7 '){
I = 7;
} Else if (c = '8 '){
I = 8;
} Else if (c = '9 '){
I = 9;
} Else {
System. out. println ("the maximum value of each user in hexadecimal format is f. Enter 0 ~ Value Between f ");
Return true;
}
Sum_value (I, N_Ci_Fang );
Return false;
}
// -------; ---------------------------- Calculate the integer value and obtain the decimal value ----------------
Public void sum_value (int value, int N_Ci_Fang ){
// System. out. println (Math. pow (16, 0); // the two parameters in the power 0 of 16 are of the double type.
This. sum + = value * (int) Math. pow (16, N_Ci_Fang );
}
// ------------------------------------ Calculate the binary value ---------------------
Public void sum_2 (int sum ){
Int sum2 = sum; // It is used to hold the value of 2 in each division.
StringBuffer s = new StringBuffer ();
For (int I = 1; I <999999999; ++ I ){
If (sum2 = 1 ){
S. append (1 );
Break;
}
S. append (sum2% 2 );
Sum2 = sum2/2;
}
String_Buffer (s );
}
// ---------------------- StringBuffer --------------------------------------
Public void String_Buffer (StringBuffer s ){
StringBuffer value = new StringBuffer ();
For (int I = 0; I <s. length (); ++ I ){
Value. append (s. charAt (s. length ()-1-i ));
}
System. out. println (value );
}
//------------------------------------------------------------
//------------------------------------------------------------
}