Integer源碼 轉2進位轉16進位,integer源碼

來源:互聯網
上載者:User

Integer源碼 轉2進位轉16進位,integer源碼


其中有一個私人方法,toUnsignedString(int i, int shift) 是為 
 public static String toHexString(int i) {
        return toUnsignedString(i, 4);
 }
public static String toBinaryString(int i) {
        return toUnsignedString(i, 1);
 }
著兩個方法服務的。

final static 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'    }; 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));    }
以559轉為16進位為例:

559 二進位位 10 0010 1111;

toUnsignedString(i, 4);
1.取4位計算對應的16進位字元;
2.然後右移4位,前面補0;
重複1 ,2;
----------
mask = 15,對應 1111;

          10 0010 1111 & mask = 1111 -> 15 -> digits[15] -> f;
右移4位後 00 0010 0010 & mask = 0010 -> 2 -> digits[2] -> 2;
右移4位後 00 0000 0010 & mask = 0010 -> 2 -> digits[2] -> 2; 

----> 22f

同樣如果轉為8進位則為:toUnsignedString(i, 3);

感覺自己寫的演算法都弱爆了


Java怎使用整數轉換二進位跟16進位,方法簡單點,

看Integer的API:
static String toBinaryString(int i) 以二進位(基數 2)不帶正負號的整數形式返回一個整數參數的字串表示形式。
static String toHexString(int i) 以十六進位的不帶正負號的整數形式返回一個整數參數的字串表示形式。
static String toOctalString(int i) 以八進位(基數 8)不帶正負號的整數形式返回一個整數參數的字串表示形式。
 
誰可以給個2進位轉16進位字串的源碼我

建兩個文字框
Text1.text中輸入2進位字串
Text2.text中輸出相應的16進位字串

Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
End Sub

Private Sub Text1_Change()

s = Text1.Text

If Len(s) Mod 4 <> 0 Then

Select Case Len(s) Mod 4
Case 1
s = "000" & s
Case 2
s = "00" & s
Case 3
s = "0" & s
End Select

End If

For i = 1 To Len(s) Step 4
w = w & z(Mid(s, i, 4))
Next

Text2.Text = w

End Sub

Private Function z(t As String) As String
Select Case t
Case "0000"
z = "0"
Case "0001"
z = "1"
Case "0010"
z = "2"
Case "0011"
z = "3"
Case "0100"
z = "4"
Case "0101"
z = "5"
Case "0110"
z = "6"
Case "0111"
z = "7"
Case "1000"
z = "8"
Case "1001"
z = "9"
Case "1010"
z = "A"
Case "1011"
z = "B"
Case "1100"
z = "C"
Case "1101"
z = "D"
Case "1110"
z = "E"
Case "1111"
z = "F"
End Select

End Function

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 48 And KeyAscii <> 49 And KeyAscii <> 8 Then

KeyAscii = 0

End If
End Sub...餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.