正數轉二進制很簡單,轉十六進制也很簡單。
那么負數的情況下呢?在計算機中無法識別你給的符號“+”,"-",計算機只認識0和1 那么在二進制中如何表示負數。
先簡單介紹一下負數如何轉二進制,八進制,十六進制:
比如給的是-4
那么先算出+4的二進制表示:
1 0 0
但是請記住我們需要在前面補0,因為在計算機中一個Int32為的數字是一個長度為32的內存空間,計算機眼里
0000 0000 0000 0000 0000 0000 0000 0100 才是4,這是源碼
接下來進行反碼,結果是
1111 1111 1111 1111 1111 1111 1011
反碼之后,再+1便是4的補碼
1111 1111 1111 1111 1111 1111 1100
得到4的補碼之后,其實這個補碼就是-4的二進制表示,那么-4的八進制就是將每3位換算成數字結果是:
37777777774
那么16進制就是每4位換算成數字
FFFFFFFC
說到這里就有個疑問了,按照上面的規則,-1二進制里表示 1111 1111 1111 1111 1111 1111 1111 1111
那么4294967295 數字轉換成二進制也是1111 1111 1111 1111 1111 1111 1111 1111。
那么1111 1111 1111 1111 1111 1111 1111 1111到底表示哪個呢?
其實:
一段內存是連續32個1,你把它按照一個int來解讀就是-1,按照unsigned int來解讀就是4294967295
我們可以在c++程序中實現一下:
int aa = 4294967295; cout<<aa;
結果是:
當你把int aa 變成 unsigned int aa unsigned表示無符號
那么結果就是4294967295
在c++中:
unsigned int: 4294967295(2^32-1)
signed int: 2^31-1
因為,計算機需要留一個最高位來判斷你這個數字是正的還是負的。
所以Int一個數字32位下最大數字是2^31-1 你定義的超過了這數字那么計算機就會把你計算出負數了。
下面附上java寫的十進制轉十六進制的代碼
package com.company; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.HashMap; import java.util.Map; import java.math.*; import java.util.*; public class Main { static int Bin[] = new int[1000]; static char Hex[] = new char[1000]; static int _pos=0; static int Change(char x) { if(x>='0'&&x<='9') return x-'0'; else if(x=='A') return 10; else if(x=='B') return 11; else if(x=='C') return 12; else if(x=='D') return 13; else if(x=='E') return 14; else if(x=='F') return 15; else return 16; } //十六進制轉二進制 static void HextoBin() { for(int i=0;i<Hex.length;i++) { int x= Change(Hex[i]); int j=0; _pos+=4; while(x>0) { Bin[--_pos]=x%2; x/=2; j++; } _pos+=j; } //是負數要減1再進行反碼 if(Hex.length==8&&Hex[0]>='8') { int xx =-1; for(int i=_pos-1;i>=0;i--) { Bin[i]+=xx; if(Bin[i]==-1) { Bin[i]=1; xx =-1; } else xx=0; } for(int i=0;i<_pos;i++) { Bin[i]=(Bin[i]==1?0:1); } } } static int BintoDem() { int x=0; for(int i=_pos-1;i>=0;i--) { x+=(Bin[i]*((int)Math.pow(2.0,_pos-1-i))); } return x; } public static void main(String[] args) { // write your code here // HashMap<String,HashMap<String,String>> m = new HashMap<>(); System.out.println("**********輸入數字,輸出16進制數"); Scanner input = new Scanner(System.in); x=input.nextInt(); DemtoBin(x); BintoHex(); for(int i=pos2-1;i>=0;i--) System.out.print(hex[i]); System.out.println(); //16進制為負數,必須是8位,且第一位是大於等於8的 System.out.println("***********輸入16進制數,輸入數字"); Hex=input.next().toCharArray(); HextoBin(); int x= BintoDem(); if(Hex.length==8&&Hex[0]>='8') { System.out.println("-"+x); } else System.out.println(x); } static int bin[] = new int[10000]; static char hex[] = new char [10000]; static int pos2=0; static int pos =32; static int pos3 =0; static int x; static void DemtoBin(int x) { //先轉換為二進制 int y = Math.abs(x); pos3=0; while(y>0) { bin[pos3++]=y%2; y/=2; } //如果為負數,要進行反碼 if (x < 0) { for (int i = 0; i < pos; i++) { bin[i] = (bin[i] == 1 ? 0 : 1); } //加1 int xx = 1; for (int i = 0; i < pos; i++) { bin[i] += xx; if (bin[i] == 2) { bin[i] = 0; xx = 1; } else xx = 0; } if(xx==1) bin[pos++]=xx; } } static char change(int x) { if(x>=0&&x<=9) { return (char)(x+48); } else if(x==10) return 'A'; else if(x==11) return 'B'; else if(x==12) return 'C'; else if(x==13) return 'D'; else if(x==14) return 'E'; else if(x==15) return 'F'; else return 'G'; } //二進制轉16進制 static void BintoHex() { int len; if(x<0) len = pos; else len =pos3; int j=0;int res=0; for(int i=0;i<len;i++) { res+=Math.pow(2.0,j)*bin[i]; j++; if(j==4) { hex[pos2++]=change(res); res =0; j=0; } } if(j!=0) { hex[pos2++]=change(res); } } }
---------------------------------------更新---------------------------------
一個數字給你轉換成二進制,其實不用上面那么麻煩,判斷符號啥的,無論正數還是負數
都可以用位運算來解決,&1 表示判斷這個數字最后一位的是0還是1,<<1 表示左移一位:
public byte[] getIntBit(int b) { byte[] array = new byte[32]; for (int i = 31; i >= 0; i--) { array[i] = (byte)(b & 1); b = (byte) (b >> 1); } return array; }