用JAVA怎样实现十进制,八进制,二进制,十六进制的相互转换,求解
发布网友
发布时间:2022-04-29 16:40
我来回答
共2个回答
热心网友
时间:2023-10-20 05:39
/*
比较原始的方案,可供学习原理
*/
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class Test_math {
public static void main(String args[]) {
Scanner in =new Scanner(System.in);
int m,n;//m进制转n进制
System.out.println("输入原始数据进制:");
m=in.nextInt();
System.out.println("请输入目标数据进制");
n=in.nextInt();
System.out.println("输入要转换的数据");
in.nextLine();
String s=in.nextLine();
int x10=0;
if(m!=10)
{
x10=Test_math.m_to_ten(s, m);
}
else
{
x10=Integer.parseInt(s);
}
if(n==10)
{
System.out.println("结果:"+x10);
return;
}
String ans=Test_math.十进制转m进制(x10, n);
System.out.println("结果:"+ans);
// int d = 1458;
// String s2 = Test_math.十进制转m进制(d, 16);
// System.out.println(s2);
// s2 = "100100";
// d = Test_math.bin_to_ten(s2);
// System.out.println(d);
// System.out.println(Test_math.m_to_ten("AA", 16));
}
public static int m_to_ten(String s, int m) {
int d = 0;
int len = s.length();
for (int i = 0; i < len; i++) {
String t = String.valueOf(s.charAt(i));
switch(s.charAt(i))
{
case 'A': case 'a': t="10"; break;
case 'B': case 'b': t="11"; break;
case 'C': case 'c': t="12"; break;
case 'D': case 'd': t="13"; break;
case 'E': case 'e': t="14"; break;
case 'F': case 'f': t="15"; break;
}
int ds = Integer.parseInt(t) * (int) Math.pow(m, (len - i - 1));
d += ds;
}
return d;
}
public static String 十进制转m进制(int s, int m) {
String ans = "";
int n = (int) (Math.log(s) / Math.log(m)) + 1;
int[] c = new int[n];
c = Test_math.十进制转m进制(s, m, n);
for (int i = 0; i < n; i++) {
if (m == 16 && c[i] >= 10) {
switch (c[i]) {
case 10:
ans += "A";
break;
case 11:
ans += "B";
break;
case 12:
ans += "C";
break;
case 13:
ans += "D";
break;
case 14:
ans += "E";
break;
case 15:
ans += "F";
break;
}
} else {
ans += c[i];
}
}
return ans;
}
public static int[] 十进制转m进制(int s, int m, int n) {
//System.out.println(" \n s="+s+" m="+m+ " n="+n);
//十进制转m进制,并凑出n位数,存到一个数组中返回
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[i] = 0;
}
int 当前位 = n - 1;
while (s >= m) {
//System.out.println("s="+s+" 当前位="+当前位);
ans[当前位] = s % m;
当前位--;
s = s / m;
}
ans[当前位] = s;
return ans;
}
}
热心网友
时间:2023-10-20 05:40
Integer.toHexString(int i)转化为16进制,返回String类型.
Integer.toOctalString(int i)转化为8进制,返回String类型.
Integer.toBinaryString(int i)转化为2进制,返回String类型.
原理:
private static String toUnsignedString(int i, int shift) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << shift;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= shift;
} while (i != 0);
return new String(buf, charPos, (32 - charPos));
}
热心网友
时间:2023-10-20 05:39
/*
比较原始的方案,可供学习原理
*/
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class Test_math {
public static void main(String args[]) {
Scanner in =new Scanner(System.in);
int m,n;//m进制转n进制
System.out.println("输入原始数据进制:");
m=in.nextInt();
System.out.println("请输入目标数据进制");
n=in.nextInt();
System.out.println("输入要转换的数据");
in.nextLine();
String s=in.nextLine();
int x10=0;
if(m!=10)
{
x10=Test_math.m_to_ten(s, m);
}
else
{
x10=Integer.parseInt(s);
}
if(n==10)
{
System.out.println("结果:"+x10);
return;
}
String ans=Test_math.十进制转m进制(x10, n);
System.out.println("结果:"+ans);
// int d = 1458;
// String s2 = Test_math.十进制转m进制(d, 16);
// System.out.println(s2);
// s2 = "100100";
// d = Test_math.bin_to_ten(s2);
// System.out.println(d);
// System.out.println(Test_math.m_to_ten("AA", 16));
}
public static int m_to_ten(String s, int m) {
int d = 0;
int len = s.length();
for (int i = 0; i < len; i++) {
String t = String.valueOf(s.charAt(i));
switch(s.charAt(i))
{
case 'A': case 'a': t="10"; break;
case 'B': case 'b': t="11"; break;
case 'C': case 'c': t="12"; break;
case 'D': case 'd': t="13"; break;
case 'E': case 'e': t="14"; break;
case 'F': case 'f': t="15"; break;
}
int ds = Integer.parseInt(t) * (int) Math.pow(m, (len - i - 1));
d += ds;
}
return d;
}
public static String 十进制转m进制(int s, int m) {
String ans = "";
int n = (int) (Math.log(s) / Math.log(m)) + 1;
int[] c = new int[n];
c = Test_math.十进制转m进制(s, m, n);
for (int i = 0; i < n; i++) {
if (m == 16 && c[i] >= 10) {
switch (c[i]) {
case 10:
ans += "A";
break;
case 11:
ans += "B";
break;
case 12:
ans += "C";
break;
case 13:
ans += "D";
break;
case 14:
ans += "E";
break;
case 15:
ans += "F";
break;
}
} else {
ans += c[i];
}
}
return ans;
}
public static int[] 十进制转m进制(int s, int m, int n) {
//System.out.println(" \n s="+s+" m="+m+ " n="+n);
//十进制转m进制,并凑出n位数,存到一个数组中返回
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[i] = 0;
}
int 当前位 = n - 1;
while (s >= m) {
//System.out.println("s="+s+" 当前位="+当前位);
ans[当前位] = s % m;
当前位--;
s = s / m;
}
ans[当前位] = s;
return ans;
}
}
热心网友
时间:2023-10-20 05:40
Integer.toHexString(int i)转化为16进制,返回String类型.
Integer.toOctalString(int i)转化为8进制,返回String类型.
Integer.toBinaryString(int i)转化为2进制,返回String类型.
原理:
private static String toUnsignedString(int i, int shift) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << shift;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= shift;
} while (i != 0);
return new String(buf, charPos, (32 - charPos));
}
热心网友
时间:2023-10-20 05:39
/*
比较原始的方案,可供学习原理
*/
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class Test_math {
public static void main(String args[]) {
Scanner in =new Scanner(System.in);
int m,n;//m进制转n进制
System.out.println("输入原始数据进制:");
m=in.nextInt();
System.out.println("请输入目标数据进制");
n=in.nextInt();
System.out.println("输入要转换的数据");
in.nextLine();
String s=in.nextLine();
int x10=0;
if(m!=10)
{
x10=Test_math.m_to_ten(s, m);
}
else
{
x10=Integer.parseInt(s);
}
if(n==10)
{
System.out.println("结果:"+x10);
return;
}
String ans=Test_math.十进制转m进制(x10, n);
System.out.println("结果:"+ans);
// int d = 1458;
// String s2 = Test_math.十进制转m进制(d, 16);
// System.out.println(s2);
// s2 = "100100";
// d = Test_math.bin_to_ten(s2);
// System.out.println(d);
// System.out.println(Test_math.m_to_ten("AA", 16));
}
public static int m_to_ten(String s, int m) {
int d = 0;
int len = s.length();
for (int i = 0; i < len; i++) {
String t = String.valueOf(s.charAt(i));
switch(s.charAt(i))
{
case 'A': case 'a': t="10"; break;
case 'B': case 'b': t="11"; break;
case 'C': case 'c': t="12"; break;
case 'D': case 'd': t="13"; break;
case 'E': case 'e': t="14"; break;
case 'F': case 'f': t="15"; break;
}
int ds = Integer.parseInt(t) * (int) Math.pow(m, (len - i - 1));
d += ds;
}
return d;
}
public static String 十进制转m进制(int s, int m) {
String ans = "";
int n = (int) (Math.log(s) / Math.log(m)) + 1;
int[] c = new int[n];
c = Test_math.十进制转m进制(s, m, n);
for (int i = 0; i < n; i++) {
if (m == 16 && c[i] >= 10) {
switch (c[i]) {
case 10:
ans += "A";
break;
case 11:
ans += "B";
break;
case 12:
ans += "C";
break;
case 13:
ans += "D";
break;
case 14:
ans += "E";
break;
case 15:
ans += "F";
break;
}
} else {
ans += c[i];
}
}
return ans;
}
public static int[] 十进制转m进制(int s, int m, int n) {
//System.out.println(" \n s="+s+" m="+m+ " n="+n);
//十进制转m进制,并凑出n位数,存到一个数组中返回
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[i] = 0;
}
int 当前位 = n - 1;
while (s >= m) {
//System.out.println("s="+s+" 当前位="+当前位);
ans[当前位] = s % m;
当前位--;
s = s / m;
}
ans[当前位] = s;
return ans;
}
}
热心网友
时间:2023-10-20 05:40
Integer.toHexString(int i)转化为16进制,返回String类型.
Integer.toOctalString(int i)转化为8进制,返回String类型.
Integer.toBinaryString(int i)转化为2进制,返回String类型.
原理:
private static String toUnsignedString(int i, int shift) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << shift;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= shift;
} while (i != 0);
return new String(buf, charPos, (32 - charPos));
}
热心网友
时间:2023-10-20 05:39
/*
比较原始的方案,可供学习原理
*/
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class Test_math {
public static void main(String args[]) {
Scanner in =new Scanner(System.in);
int m,n;//m进制转n进制
System.out.println("输入原始数据进制:");
m=in.nextInt();
System.out.println("请输入目标数据进制");
n=in.nextInt();
System.out.println("输入要转换的数据");
in.nextLine();
String s=in.nextLine();
int x10=0;
if(m!=10)
{
x10=Test_math.m_to_ten(s, m);
}
else
{
x10=Integer.parseInt(s);
}
if(n==10)
{
System.out.println("结果:"+x10);
return;
}
String ans=Test_math.十进制转m进制(x10, n);
System.out.println("结果:"+ans);
// int d = 1458;
// String s2 = Test_math.十进制转m进制(d, 16);
// System.out.println(s2);
// s2 = "100100";
// d = Test_math.bin_to_ten(s2);
// System.out.println(d);
// System.out.println(Test_math.m_to_ten("AA", 16));
}
public static int m_to_ten(String s, int m) {
int d = 0;
int len = s.length();
for (int i = 0; i < len; i++) {
String t = String.valueOf(s.charAt(i));
switch(s.charAt(i))
{
case 'A': case 'a': t="10"; break;
case 'B': case 'b': t="11"; break;
case 'C': case 'c': t="12"; break;
case 'D': case 'd': t="13"; break;
case 'E': case 'e': t="14"; break;
case 'F': case 'f': t="15"; break;
}
int ds = Integer.parseInt(t) * (int) Math.pow(m, (len - i - 1));
d += ds;
}
return d;
}
public static String 十进制转m进制(int s, int m) {
String ans = "";
int n = (int) (Math.log(s) / Math.log(m)) + 1;
int[] c = new int[n];
c = Test_math.十进制转m进制(s, m, n);
for (int i = 0; i < n; i++) {
if (m == 16 && c[i] >= 10) {
switch (c[i]) {
case 10:
ans += "A";
break;
case 11:
ans += "B";
break;
case 12:
ans += "C";
break;
case 13:
ans += "D";
break;
case 14:
ans += "E";
break;
case 15:
ans += "F";
break;
}
} else {
ans += c[i];
}
}
return ans;
}
public static int[] 十进制转m进制(int s, int m, int n) {
//System.out.println(" \n s="+s+" m="+m+ " n="+n);
//十进制转m进制,并凑出n位数,存到一个数组中返回
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[i] = 0;
}
int 当前位 = n - 1;
while (s >= m) {
//System.out.println("s="+s+" 当前位="+当前位);
ans[当前位] = s % m;
当前位--;
s = s / m;
}
ans[当前位] = s;
return ans;
}
}
热心网友
时间:2023-10-20 05:40
Integer.toHexString(int i)转化为16进制,返回String类型.
Integer.toOctalString(int i)转化为8进制,返回String类型.
Integer.toBinaryString(int i)转化为2进制,返回String类型.
原理:
private static String toUnsignedString(int i, int shift) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << shift;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= shift;
} while (i != 0);
return new String(buf, charPos, (32 - charPos));
}
热心网友
时间:2023-10-20 05:39
/*
比较原始的方案,可供学习原理
*/
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class Test_math {
public static void main(String args[]) {
Scanner in =new Scanner(System.in);
int m,n;//m进制转n进制
System.out.println("输入原始数据进制:");
m=in.nextInt();
System.out.println("请输入目标数据进制");
n=in.nextInt();
System.out.println("输入要转换的数据");
in.nextLine();
String s=in.nextLine();
int x10=0;
if(m!=10)
{
x10=Test_math.m_to_ten(s, m);
}
else
{
x10=Integer.parseInt(s);
}
if(n==10)
{
System.out.println("结果:"+x10);
return;
}
String ans=Test_math.十进制转m进制(x10, n);
System.out.println("结果:"+ans);
// int d = 1458;
// String s2 = Test_math.十进制转m进制(d, 16);
// System.out.println(s2);
// s2 = "100100";
// d = Test_math.bin_to_ten(s2);
// System.out.println(d);
// System.out.println(Test_math.m_to_ten("AA", 16));
}
public static int m_to_ten(String s, int m) {
int d = 0;
int len = s.length();
for (int i = 0; i < len; i++) {
String t = String.valueOf(s.charAt(i));
switch(s.charAt(i))
{
case 'A': case 'a': t="10"; break;
case 'B': case 'b': t="11"; break;
case 'C': case 'c': t="12"; break;
case 'D': case 'd': t="13"; break;
case 'E': case 'e': t="14"; break;
case 'F': case 'f': t="15"; break;
}
int ds = Integer.parseInt(t) * (int) Math.pow(m, (len - i - 1));
d += ds;
}
return d;
}
public static String 十进制转m进制(int s, int m) {
String ans = "";
int n = (int) (Math.log(s) / Math.log(m)) + 1;
int[] c = new int[n];
c = Test_math.十进制转m进制(s, m, n);
for (int i = 0; i < n; i++) {
if (m == 16 && c[i] >= 10) {
switch (c[i]) {
case 10:
ans += "A";
break;
case 11:
ans += "B";
break;
case 12:
ans += "C";
break;
case 13:
ans += "D";
break;
case 14:
ans += "E";
break;
case 15:
ans += "F";
break;
}
} else {
ans += c[i];
}
}
return ans;
}
public static int[] 十进制转m进制(int s, int m, int n) {
//System.out.println(" \n s="+s+" m="+m+ " n="+n);
//十进制转m进制,并凑出n位数,存到一个数组中返回
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[i] = 0;
}
int 当前位 = n - 1;
while (s >= m) {
//System.out.println("s="+s+" 当前位="+当前位);
ans[当前位] = s % m;
当前位--;
s = s / m;
}
ans[当前位] = s;
return ans;
}
}
热心网友
时间:2023-10-20 05:40
Integer.toHexString(int i)转化为16进制,返回String类型.
Integer.toOctalString(int i)转化为8进制,返回String类型.
Integer.toBinaryString(int i)转化为2进制,返回String类型.
原理:
private static String toUnsignedString(int i, int shift) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << shift;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= shift;
} while (i != 0);
return new String(buf, charPos, (32 - charPos));
}
热心网友
时间:2023-10-20 05:39
/*
比较原始的方案,可供学习原理
*/
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class Test_math {
public static void main(String args[]) {
Scanner in =new Scanner(System.in);
int m,n;//m进制转n进制
System.out.println("输入原始数据进制:");
m=in.nextInt();
System.out.println("请输入目标数据进制");
n=in.nextInt();
System.out.println("输入要转换的数据");
in.nextLine();
String s=in.nextLine();
int x10=0;
if(m!=10)
{
x10=Test_math.m_to_ten(s, m);
}
else
{
x10=Integer.parseInt(s);
}
if(n==10)
{
System.out.println("结果:"+x10);
return;
}
String ans=Test_math.十进制转m进制(x10, n);
System.out.println("结果:"+ans);
// int d = 1458;
// String s2 = Test_math.十进制转m进制(d, 16);
// System.out.println(s2);
// s2 = "100100";
// d = Test_math.bin_to_ten(s2);
// System.out.println(d);
// System.out.println(Test_math.m_to_ten("AA", 16));
}
public static int m_to_ten(String s, int m) {
int d = 0;
int len = s.length();
for (int i = 0; i < len; i++) {
String t = String.valueOf(s.charAt(i));
switch(s.charAt(i))
{
case 'A': case 'a': t="10"; break;
case 'B': case 'b': t="11"; break;
case 'C': case 'c': t="12"; break;
case 'D': case 'd': t="13"; break;
case 'E': case 'e': t="14"; break;
case 'F': case 'f': t="15"; break;
}
int ds = Integer.parseInt(t) * (int) Math.pow(m, (len - i - 1));
d += ds;
}
return d;
}
public static String 十进制转m进制(int s, int m) {
String ans = "";
int n = (int) (Math.log(s) / Math.log(m)) + 1;
int[] c = new int[n];
c = Test_math.十进制转m进制(s, m, n);
for (int i = 0; i < n; i++) {
if (m == 16 && c[i] >= 10) {
switch (c[i]) {
case 10:
ans += "A";
break;
case 11:
ans += "B";
break;
case 12:
ans += "C";
break;
case 13:
ans += "D";
break;
case 14:
ans += "E";
break;
case 15:
ans += "F";
break;
}
} else {
ans += c[i];
}
}
return ans;
}
public static int[] 十进制转m进制(int s, int m, int n) {
//System.out.println(" \n s="+s+" m="+m+ " n="+n);
//十进制转m进制,并凑出n位数,存到一个数组中返回
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[i] = 0;
}
int 当前位 = n - 1;
while (s >= m) {
//System.out.println("s="+s+" 当前位="+当前位);
ans[当前位] = s % m;
当前位--;
s = s / m;
}
ans[当前位] = s;
return ans;
}
}
热心网友
时间:2023-10-20 05:40
Integer.toHexString(int i)转化为16进制,返回String类型.
Integer.toOctalString(int i)转化为8进制,返回String类型.
Integer.toBinaryString(int i)转化为2进制,返回String类型.
原理:
private static String toUnsignedString(int i, int shift) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << shift;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= shift;
} while (i != 0);
return new String(buf, charPos, (32 - charPos));
}