Integer source code to binary to hexadecimal, integer source code
There is a private method, where toUnsignedString (int I, int shift) is
Public static String toHexString (int I ){
Return toUnsignedString (I, 4 );
}
Public static String toBinaryString (int I ){
Return toUnsignedString (I, 1 );
}
Two methods are provided.
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)); }
For example, convert 559 to hexadecimal:
559 binary bits 10 0010 1111;
ToUnsignedString (I, 4 );
1. Take the hexadecimal character corresponding to the 4-digit calculation;
2. Move four places to the right and add 0 to the front;
Repeat 1, 2;
----------
Mask = 15, corresponding to 1111;
10 0010 1111 & mask = 1111-> 15-> digits [15]-> f;
Shift four digits right after 00 0010 0010 & mask = 0010-> 2-> digits [2]-> 2;
Shift four digits right after 00 0000 0010 & mask = 0010-> 2-> digits [2]-> 2;
----> 22f
Similarly, if it is converted to an octal format, it is: toUnsignedString (I, 3 );
I feel that all my algorithms are weak.
In Java, how does one use Integer Conversion in binary and hexadecimal notation? The method is simple,
Check the Integer API:
Static String toBinaryString (int I) returns the String representation of an integer parameter in the form of a binary (base 2) unsigned integer.
Static String toHexString (int I) returns the String representation of an integer parameter in hexadecimal notation.
Static String toOctalString (int I) returns the String representation of an integer parameter in the form of an unsigned integer (base 8.
Who can give me the source code of a binary to a hexadecimal string?
Create two text boxes
Input a binary string in Text1.text
Output the corresponding hexadecimal string in Text2.text
Private Sub Form_Load ()
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub text=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 = ""
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 text=keypress (KeyAscii As Integer)
If KeyAscii <> 48 And KeyAscii <> 49 And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub... remaining full text>