public class towadecimal{
private int sum;//for converting values
private static String s = "11";//16 Binary Value
public static void Main (string[] args) {
Towadecimal a = new Towadecimal ();
A.char_value (s);
}
//------------------------------------------------------------
public void Char_value (String s) {
if (s = = null) {//judgment.) is not NULL if it is null there is no need to go down the operation, directly terminate the function run
System.out.println ("Please 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);//Get string from right to left
if (Int_value (c,i)) {//This is based on the input character, if there is an incorrect, terminate this function to run, do not output the wrong result
Return
}
}
if (sum = = 0) {
SYSTEM.OUT.PRINTLN (0);
}else{
Sum_2 (sum);
}
}
------------------------------------used to parse a string into an integer value----------------
public boolean int_value (char c,int N_ci_fang) {//This method converts a 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 ("hexadecimal value on each digit is maximum F, please enter a value between 0~f");
return true;
}
Sum_value (I,n_ci_fang);
return false;
}
-------;----------------------------is used to calculate the integer value, resulting in a decimal----------------
public void Sum_value (int value,int n_ci_fang) {
System.out.println (Math.pow (16,0)); is 16 of the 0-time square inside the two parameters are double type
This.sum + = value* (int) Math.pow (16,n_ci_fang);
}
----------------------------------computes the value of the binary---------------------
public void sum_2 (int sum) {
The int sum2= sum;//is used to host the value of each addition of 2
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);
}
//------------------------------------------------------------
//------------------------------------------------------------
}
Code emulation for hexadecimal to binary binary