java二进制、八进制、十六进制间转换详细
生活随笔
收集整理的這篇文章主要介紹了
java二进制、八进制、十六进制间转换详细
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載自:http://blog.csdn.net/u010137760/article/details/52610442
1.各進制表示
java里不能使用前置表示2進制,只能是 8,10,16進制?
8: 前置 0?
10: 不需前置?
16: 前置 0x 或者 0X
輸出結果
8進制012->10進制10 16進制0x12->10進制182.轉換-Integer.toBinaryString
測試代碼
public class test {public static void main(String[] args) {//8進制、10進制、16進制轉為2進制System.out.println("Integer.toBinaryString(01)="+Integer.toBinaryString(01));System.out.println("Integer.toBinaryString(012)="+Integer.toBinaryString(012));System.out.println("Integer.toBinaryString(10)="+Integer.toBinaryString(10));System.out.println("Integer.toBinaryString(0xa)="+Integer.toBinaryString(0xa));System.out.println("Integer.toOctalString(0x12)="+Integer.toOctalString(0x12));System.out.println("Integer.toOctalString(18)="+Integer.toOctalString(18));System.out.println("Integer.toHexString(012)="+Integer.toHexString(012));System.out.println("Integer.toHexString(10)="+Integer.toHexString(10));} }測試結果
Integer.toBinaryString(01)=1 Integer.toBinaryString(012)=1010 Integer.toBinaryString(10)=1010 Integer.toBinaryString(0xa)=1010 Integer.toOctalString(0x12)=22 Integer.toOctalString(18)=22 Integer.toHexString(012)=a Integer.toHexString(10)=a源碼
toBinaryString、toOctalString、toHexString都類似,則以toBinaryString為例講解?
Integer.java
IntegralToString.java
/*** The digits for every supported radix.*/ private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j','k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't','u', 'v', 'w', 'x', 'y', 'z' };public static String intToBinaryString(int i) {int bufLen = 32; // 整型是4個字節32位char[] buf = new char[bufLen];int cursor = bufLen;do {buf[--cursor] = DIGITS[i & 1];} while ((i >>>= 1) != 0);return new String(cursor, bufLen - cursor, buf); }運行過程
當i=1時i = 1, cursor = 32doi&1=1,buf[31] = DIGITS[1] = 1whilei = 1>>>1 = 0,i == 0終止00000000 00000000 00000000 00000001----------------------------------------- 當i=2時i = 2(10), cursor = 32doi&1=0,buf[31] = DIGITS[0] = 0whilei = 2>>>1 = 1,i != 0成立doi&1=1,buf[30] = DIGITS[1] = 1whilei = 1>>>1 = 0,i == 0終止00000000 00000000 00000000 000000103.轉換-Integer.valueOf
測試代碼
public class test {public static void main(String[] args) {//十六進制轉成十進制System.out.println(Integer.valueOf("FFFF",16));System.out.println(Integer.valueOf("-FFFF",16));//八進制轉成十進制System.out.println(Integer.valueOf("776",8));System.out.println(Integer.valueOf("-776",8));//二進制轉十進制System.out.println(Integer.valueOf("0101",2));System.out.println(Integer.valueOf("-0101",2));} }測試結果
65535 -65535 510 -510 5 -5源碼
Integer.java
方法:valueOf
/*** Parses the specified string as a signed integer value using the specified radix.* 將制定的字符串轉換為有符號的整數,參考指定的基數* @param string the string representation of an integer value.* 字符串表示的整型值* @param radix the radix to use when parsing.* 轉換時指定基數,比如2代表2進制,8代表8進制,16代表16進制* @return an {@code Integer} instance containing the integer value* represented by {@code string} using {@code radix}.* 整形值被轉換為指定的基數的字符串* @throws NumberFormatException* if {@code string} cannot be parsed as an integer value, or* {@code radix < Character.MIN_RADIX ||* radix > Character.MAX_RADIX}.* string不是整形值;radix< Character.MIN_RADIX或radix > Character.MAX_RADIX都會報轉換異常* @see #parseInt(String, int)*/public static Integer valueOf(String string, int radix) throws NumberFormatException {return valueOf(parseInt(string, radix));}方法:parseInt
/*** The minimum radix used for conversions between characters and integers.*/ public static final int MIN_RADIX = 2;/*** The maximum radix used for conversions between characters(字符) and integers.(整型)*/ public static final int MAX_RADIX = 36;/*** Parses the specified string as a signed integer value using the specified radix. * The ASCII characters \u002d ('-') and \u002b ('+') are recognized as the minus and plus signs.* ASCII字符"-"和"+"被作為正負號*/public static int parseInt(String string, int radix) throws NumberFormatException {if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {throw new NumberFormatException("Invalid radix: " + radix);}if (string == null || string.isEmpty()) {throw invalidInt(string);//返回的是一個NumberFormatException異常}char firstChar = string.charAt(0);//如果字符串以'-'或'+'開頭,則firstDigitIndex = 1作為后續操作的標識int firstDigitIndex = (firstChar == '-' || firstChar == '+') ? 1 : 0;//字符串只有'-'或'+'拋異常if (firstDigitIndex == string.length()) {throw invalidInt(string);}return parse(string, firstDigitIndex, radix, firstChar == '-');}方法:parse
/*** Constant for the minimum {@code int} value, -2<sup>31</sup>.*/ public static final int MIN_VALUE = 0x80000000; private static int parse(String string, int offset, int radix, boolean negative) throws NumberFormatException {int max = Integer.MIN_VALUE / radix;int result = 0;int length = string.length();while (offset < length) {int digit = Character.digit(string.charAt(offset++), radix);if (digit == -1) {throw invalidInt(string);}if (max > result) {throw invalidInt(string);}int next = result * radix - digit;if (next > result) {throw invalidInt(string);}result = next;}//negative以'-'開頭則為trueif (!negative) {//以'+'開頭result = -result;if (result < 0) {throw invalidInt(string);}}return result; }Character.java?
方法:digit
運行過程
Integer.valueOf("11",2)//string, radix parseInt("11", 2)//string, radix parse("11",0,2,false)//string, offset, radix, negative while(offset < string.length){0 < 2成立拿二進制的第1位1(從左到右)Character.digit('1',2)//string.charAt(offset++), radixCharacter.digit(49,2),return 1//(int) c, radixdigit('1',2),return 1next = 0*2-1=-1//result * radix - digitresult = -1拿二進制的第2位1(從左到右) Character.digit('1',2)//string.charAt(offset++), radixCharacter.digit(49,2),return 1//(int) c, radixdigit('1',2),return 1next = -1*2 -1 = -3result = -3 } //negative以'-'開頭則為true if (!negative) {result = -result = 3; } return result = 3;總結
以上是生活随笔為你收集整理的java二进制、八进制、十六进制间转换详细的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三种常见字符编码简介:ASCII、Uni
- 下一篇: 静态链接库与动态链接库的区别(Sqlit