package HuaWei;
/**
* 进制之间的转换
*@author yg
*@date 2016-7-20
*/
public class NumChange {
/**
* 十进制转化为二进制
* @param h
*/
public void TenToTwo(int h) {
boolean flag = true;
String str = "";
int yushu = 0;// 求余数
int shang = 0;// 求shang
flag = true;
while (flag) {
yushu = h % 2;// 求余数
shang = h / 2;// 求shang
str = yushu + str;
System.out.println(str);
if (shang >= 2) {// 继续迭代
h = shang;
flag = true;
} else {
if (shang == 0)
break;
flag = false;
str = shang + str;
}
}
System.out.println(" " + str);
}
/**
* 十进制转换为八进制
*/
public void TenToEight() {
int n = 8;
int shang = 0;
int yushu = 0;
String str = "";
do {
shang = n / 8;
yushu = n % 8;
str = "" + yushu + str;
System.out.println("shang=" + shang + " yushu=" + yushu
+ " str=" + str);
if (yushu == 0 || shang < 8) {
str = shang + "" + str;
break;
}
n = shang;
} while (shang > 8);
str = "0" + str;
System.out.println("str=" + str);
}
/**
* 十进制转换为十六进制
*/
public void TenToSixteen() {
int n = 160;
int shang = 0;
int yushu = 0;
String str = "";
do {
shang = n / 16;
yushu = n % 16;
if (yushu >= 10) {
char ch1 = (char) (yushu + 55);
System.out.println("ch1=" + ch1);
str = "" + String.valueOf(ch1) + str;
} else {
str = "" + yushu + str;
}
if (shang < 16 || yushu == 0) {
if (shang >= 10) {
char ch2 = (char) (shang + 55);
str = String.valueOf(ch2) + str;
break;
} else {
str = shang + str;
}
}
} while (shang > 16);
str = "0x" + str;
System.out.println("Str=" + str);
}
/**
* 二进制转换为十进制
*/
public void TwoToTen() {
String s2 = "1110";
String s[] = s2.split("");
for (int i = 0; i < s.length; i++) {
System.out.println(" " + s[i]);
}
System.out.println("------------------" + s.length);
int value = 0;
for (int i = 1; i < s.length; i++) {
int a = Integer.parseInt(s[i]);
value = (int) (value + Integer.valueOf(s[i])
* Math.pow(2, (double) (s.length - i - 1)));
}
System.out.println("value=" + value);
}
/**
* 十六进制转换为十进制
*/
public void SixteenToTen() {
String s16 = "oxA12";
String s2 = s16.substring(2);
char[] cArray = s2.toCharArray();
int value = 0;
for (int i = 0; i < cArray.length; i++) {
char ch = cArray[i];
int n = 0;
System.out.println(ch);
if ((ch >= 65) && (ch <= 90)) {
n = (int) (ch - 55);
value = (int) (value + n
* Math.pow((double) 16,
(double) (cArray.length - 1 - i)));
System.out.println("cccccccc" + value);
continue;
}
n = Integer.parseInt(String.valueOf(ch));
System.out.println("n=" + n);
value = (int) (value + n
* Math.pow((double) 16, (double) (cArray.length - 1 - i)));
}
System.out.println("value " + value);
}
/**
* 八进制转换为十进制
*/
public void EightToTen() {
String str = "041";
String s8 = str.substring(1);
int value = 0;
char[] cArray = s8.toCharArray();
for (int i = 0; i < cArray.length; i++) {
char ch = cArray[i];
System.out.println(ch);
int n = Integer.parseInt(String.valueOf(ch));
value = (int) (value + n * Math.pow((8), cArray.length - 1 - i));
}
System.out.println("value=" + value);
}
}
/**
* 进制之间的转换
*@author yg
*@date 2016-7-20
*/
public class NumChange {
/**
* 十进制转化为二进制
* @param h
*/
public void TenToTwo(int h) {
boolean flag = true;
String str = "";
int yushu = 0;// 求余数
int shang = 0;// 求shang
flag = true;
while (flag) {
yushu = h % 2;// 求余数
shang = h / 2;// 求shang
str = yushu + str;
System.out.println(str);
if (shang >= 2) {// 继续迭代
h = shang;
flag = true;
} else {
if (shang == 0)
break;
flag = false;
str = shang + str;
}
}
System.out.println(" " + str);
}
/**
* 十进制转换为八进制
*/
public void TenToEight() {
int n = 8;
int shang = 0;
int yushu = 0;
String str = "";
do {
shang = n / 8;
yushu = n % 8;
str = "" + yushu + str;
System.out.println("shang=" + shang + " yushu=" + yushu
+ " str=" + str);
if (yushu == 0 || shang < 8) {
str = shang + "" + str;
break;
}
n = shang;
} while (shang > 8);
str = "0" + str;
System.out.println("str=" + str);
}
/**
* 十进制转换为十六进制
*/
public void TenToSixteen() {
int n = 160;
int shang = 0;
int yushu = 0;
String str = "";
do {
shang = n / 16;
yushu = n % 16;
if (yushu >= 10) {
char ch1 = (char) (yushu + 55);
System.out.println("ch1=" + ch1);
str = "" + String.valueOf(ch1) + str;
} else {
str = "" + yushu + str;
}
if (shang < 16 || yushu == 0) {
if (shang >= 10) {
char ch2 = (char) (shang + 55);
str = String.valueOf(ch2) + str;
break;
} else {
str = shang + str;
}
}
} while (shang > 16);
str = "0x" + str;
System.out.println("Str=" + str);
}
/**
* 二进制转换为十进制
*/
public void TwoToTen() {
String s2 = "1110";
String s[] = s2.split("");
for (int i = 0; i < s.length; i++) {
System.out.println(" " + s[i]);
}
System.out.println("------------------" + s.length);
int value = 0;
for (int i = 1; i < s.length; i++) {
int a = Integer.parseInt(s[i]);
value = (int) (value + Integer.valueOf(s[i])
* Math.pow(2, (double) (s.length - i - 1)));
}
System.out.println("value=" + value);
}
/**
* 十六进制转换为十进制
*/
public void SixteenToTen() {
String s16 = "oxA12";
String s2 = s16.substring(2);
char[] cArray = s2.toCharArray();
int value = 0;
for (int i = 0; i < cArray.length; i++) {
char ch = cArray[i];
int n = 0;
System.out.println(ch);
if ((ch >= 65) && (ch <= 90)) {
n = (int) (ch - 55);
value = (int) (value + n
* Math.pow((double) 16,
(double) (cArray.length - 1 - i)));
System.out.println("cccccccc" + value);
continue;
}
n = Integer.parseInt(String.valueOf(ch));
System.out.println("n=" + n);
value = (int) (value + n
* Math.pow((double) 16, (double) (cArray.length - 1 - i)));
}
System.out.println("value " + value);
}
/**
* 八进制转换为十进制
*/
public void EightToTen() {
String str = "041";
String s8 = str.substring(1);
int value = 0;
char[] cArray = s8.toCharArray();
for (int i = 0; i < cArray.length; i++) {
char ch = cArray[i];
System.out.println(ch);
int n = Integer.parseInt(String.valueOf(ch));
value = (int) (value + n * Math.pow((8), cArray.length - 1 - i));
}
System.out.println("value=" + value);
}
}