本文主要是介绍Java 二进制,八进制,十进制,十六进制之间的相互转换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package com.sjd.JinzhiZhuanhuan;public class JinzhiZhuanhuan {//二进制转八,十,十六进制---开始public void fromBinaryToOctalSting(String str1) {String result=Integer.toOctalString(Integer.parseInt(str1, 2));System.out.println(str1+"的八进制:"+result);}public void fromBinaryToInt(String str1) {String result=Integer.valueOf(str1,2).toString();System.out.println(str1+"的十进制:"+result);}public void fromBinaryToHexString(String str1) {String result=Integer.toHexString(Integer.parseInt(str1, 2));System.out.println(str1+"的十六进制:"+result);}//二进制转八,十,十六进制---结束//八进制转二,十,十六进制---开始public void fromOctalToBinaryString(String str2) {String result=Integer.toBinaryString(Integer.valueOf(str2,8));System.out.println(str2+"的二进制:"+result);}public void fromOctalToInt(String str2) {String result=Integer.valueOf(str2,8).toString();System.out.println(str2+"的十进制:"+result);}public void fromOctalToHexString(String str2) {String result=Integer.toHexString(Integer.valueOf(str2,8));System.out.println(str2+"的十六进制:"+result);}//八进制转二,十,十六进制---结束//十进制转二,八,十六进制---开始public void fromIntToBinaryString(int num) {String result = Integer.toBinaryString(num);System.out.println(num+"的二进制:"+result);}public void fromIntToOctalString(int num) {String result = Integer.toOctalString(num);System.out.println(num+"的八进制:"+result);}public void fromIntToHexString(int num) {String result = Integer.toHexString(num);System.out.println(num+"的十六进制:"+result);}//十进制转二,八,十六进制---结束//十六进制转二,八,十进制---开始public void fromHexToBinaryString(String str3) {String result=Integer.toBinaryString(Integer.valueOf(str3,16));System.out.println(str3+"的十六进制:"+result);}public void fromHexToOctalString(String str3) {String result=Integer.toOctalString(Integer.valueOf(str3,16));System.out.println(str3+"的十六进制:"+result);}public void fromHexToInt(String str3) {String result=Integer.valueOf(str3,16).toString();System.out.println(str3+"的十六进制:"+result);}//十六进制转二,八,十进制---结束public static void main(String[] args) {// TODO Auto-generated method stubJinzhiZhuanhuan jzzh = new JinzhiZhuanhuan();System.out.println("二进制转八,十,十六进制");String str1 = "1010";jzzh.fromBinaryToOctalSting(str1);jzzh.fromBinaryToInt(str1);jzzh.fromBinaryToHexString(str1);System.out.println();System.out.println("八进制转二,十,十六进制");String str2 = "234";jzzh.fromOctalToBinaryString(str2);jzzh.fromOctalToInt(str2);jzzh.fromOctalToHexString(str2);System.out.println();System.out.println("十进制转二,八,十六进制");int num=10;jzzh.fromIntToBinaryString(num);jzzh.fromIntToOctalString(num);jzzh.fromIntToHexString(num);System.out.println();System.out.println("十六进制转二,八,十进制");String str3 = "FFFF";jzzh.fromHexToBinaryString(str3);jzzh.fromHexToOctalString(str3);jzzh.fromHexToInt(str3);System.out.println();}}
这篇关于Java 二进制,八进制,十进制,十六进制之间的相互转换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!