【Java】7.0 进制转换
2024-10-19 20:46:35
【二进制转十进制】
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a binary number");
int num = Integer.parseInt(sc.nextLine());
System.out.println("Your number is: " + num);
int position = 0, sum = 0;
while(num > 0)
{
if(num%10 == 1)
{
sum = sum + (int)(Math.pow(2,position));
System.out.println("Sum: " + sum);
}
position++;
num = num / 10;
}
System.out.println("Decimal number: " + sum);
}
【二进制转十六进制】
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a binary number to convert to Hex: ");
int numBin = sc.nextInt();
sc.close();
int power = 0, res = 0;
while(numBin > 0)
{
res = res + ((numBin%2)*(int)(Math.pow(2,power))); // extract last digit and add to red
power++; // increase the power
numBin = numBin / 10; // get rid of last digit
}
System.out.println("Num in Decimal is: " + res);
String digits = new String("0123456789ABCDEF");
String number = new String("");
int digit = 0;
while(res > 0)
{
digit = res % 16;
number = digits.charAt(digit) + number;
res = res/16;
}
System.out.println(number);
}
【十进制转十六进制】
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a decimal number to convert to hex: ");
int num = Integer.parseInt(sc.nextLine());
String digits = new String("0123456789ABCDEF");
String number = new String("");
int position = 0;
while(num > 0)
{
position = num % 16;
number = digits.charAt(position) + number;
num = num / 16;
}
System.out.println("Hex: " + number);
}
【十进制转二进制】
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number to convert to binary:");
String number = new String("");
int num = Integer.parseInt(sc.nextLine());
System.out.println("Num: " + num);
while(num > 0)
{
number = num%2 + number;
num = num/2;
}
System.out.println("Binary number: " + number);
}
【十进制转十六进制】
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a decimal number to convert to hex:");
String number = new String(""); // string to store the hex representation
String digits = new String("0123456789ABCDEF");
int num = Integer.parseInt(sc.nextLine());
int position = 0;
System.out.println("Num: " + num);
while(num > 0)
{
position = num % 16;
number = digits.charAt(position) + number;
num = num/16;
}
System.out.println("Hexadecimal number: " + number);
}
最新文章
- GridView利用PagerTemplate做分页显示设置上一页下一页转到下拉转页
- opengles 矩阵计算
- MFC 对话框控件自动布局
- mysql分页原理和高效率的mysql分页查询语句
- Eclipse功能集合
- linux下history命令显示历史指令记录的使用方法
- 菜鸟学习WCF笔记-Address
- 同步推是如何给未越狱的IOS设备安装任意IPA的?
- Python实践:开篇
- SQL Server数据库学习笔记-设计表时应该考虑的因素
- mysql中取系统当前时间
- BZOJ 2243 SDOI 2011染色
- 时刻注意QT与Windows系统的不同(惨痛教训)
- 创建一个ROS包
- Python BeautifulSoup中文乱码问题的2种解决方法
- win7 64位系统装oracle11 提示环境变量path 值超过1023字符
- 一些重要 Docker 命令的简单介绍
- Batch_Size 详解
- 第38节:hashCode()与toString()与equals()函数的作用,内部类和匿名内部类
- spark基础知识