See in the integer there is such a method to convert int to unsigned type string, but a few points are not very clear, after the query data to understand, read as follows:
Here's the parameter shift is the representation of the system, if it is binary, shift is 2, octal is 8, the corresponding mask is calculated as 1 and 7. The corresponding characters in the digits array are constantly removed by mask and I phase.
In the case where I do the logical right shift every time, the highest bit is supplemented by 0, so that I will eventually move to the right after the continuous logical shift to 0.
In addition, the use of do-while is to prevent I itself is 0, the BUF array cannot get its value.
This array represents the 10-bit portion of the number, which is used below. Final static char [] Digittens = {' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 1 ', ' 1 ', ' 1 ', ' 1 ', ' 1, ' 1 ', ' 1 ', ' 1 ', ' 1 ', ' 1 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 2 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ', ' 3 ' ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 4 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5 ', ' 5, ' 5 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 6 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 7 ', ' 8 ', ' 8 ', ' 8 ', ' 8, ' 8 ', '
8 ', ' 8 ', ' 8 ', ' 8 ', ' 8 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ' 9 ', ', ' 9 ',}; This array represents the single-digit part of the number, and the array is used below.
If you combine each part of an array, you can get two-bit integers of all the cases within 100. Final static char [] digitones = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4, ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ' ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1, ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4, ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ',}
;
public static String toString (int i) {if (i = = integer.min_value)//Here's Add 1, start not very clear what meaning, later found negative words You need to add a minus sign to the front, so the size of the string is 1.//The portion of the incoming stringsize is positive, in the following array//map int size = (I < 0)?
Stringsize (i) + 1:stringsize (i);
char[] buf = new Char[size];
GetChars (i, size, buf);
return new String (0, size, buf);
} static void GetChars (int i, int index, char[] buf) {int q, r;
int charpos = index;
char sign = 0;
if (I < 0) {sign = '-';
i =-I; ///More than 65536 integers, start with one of the following processing,//This processing in 100 units, that is, the remainder control in two bits//so exactly mapped to the above 10-bit and single-digit array, a one-time write//buf array of two bits, so there is no doubt than
Out every bit is going to be a lot faster while (I >= 65536) {q = i/100;
Really:r = i-(q * 100);R = i-((Q << 6) + (q << 5) + (Q << 2));
i = q;
BUF [--charpos] = digitones[r];
BUF [--charpos] = digittens[r];
}//Fall thru to fast mode for smaller numbers//assert (I <= 65536, i); For integers less than or equal to 65536, you can go directly to the following section//And this place is divided by 10, but the implementation is not directly in addition to//But first to find a 52429/2^19 about 0.1000 ...//I divided by 10, but I It is not clear why this is not directly//divided by 10, perhaps because the precision is not enough, division produces floating-point numbers,//perhaps inaccurate, and then the divisor multiplied by 10, to get more than 10 bits/part of the number, through I-this part of the 10 digits, get a single-digit number for (;;)
{q = (i * 52429) >>> (16+3); R = i-((Q << 3) + (q << 1));
R = I (q*10) ... buf [--charpos] = digits [R];
i = q;
if (i = = 0) break;
} if (sign!= 0) {buf [--charpos] = sign; final static int [] sizetable = {9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 99999999
9, Integer.max_value}; This should be optimized by sizetable storing the bits//of integer data, from one to 10 digits: 2147483647,//This is a clever way of handling the static int stringsize (iNT X) {for (int i=0;; i++) if (x <= sizetable[i)) return i+1;
}
This method is very interesting, I calculate, and then understand his essence, the function of this method is to form an integer of the largest bit represented by the value of the integer. This function is achieved by means of displacement. Next, for a simple example, 128来 the binary is 1000 0000. The following is an example of him:
The final result, as you can see, is that all the bits in the back are filled with 1, and the rest of the bits are all cut off to get the highest number of digits represented by the integers.
This method has really wasted half a day of Kung Fu Research, and later to understand a probably:
The first line is to make the integer bits two two, and then count the number of 1 in the two digits, I don't know how the formula came about, but it does.
In the second line, the bits of the integral type is divided into four four groups, and then the calculation of the shift is added, which is 1001-> 10 + 01 = 11 equals three 1.
The third line is to add the bits of the integral type to the eight, and then similar to the above, and then, of course, this is done with some specific shifts and operations.
Then there are 16 groups, and 32 groups eventually merge the statistics into the last few indicated statistics.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.