Java source--integer

Source: Internet
Author: User

  

Recently in the Java source code, but because of their own limited English, so want to use Chinese annotation method of the source of all the methods to rewrite

Once again, the following is a small part of the landlord finishing out. I've managed to put the whole project on GitHub, and you are welcome to exchange and learn.

Github:https://github.com/15128928804/yuanma

  

/**
* @Author: Zhuangfei
* @Description: Returns the first argument specified by the second argument to the base conversion to a string format
* If the cardinality exceeds the minimum (-2) or maximum (36) interval of character, it is specified as 10
* If the first parameter is negative, then the corresponding converted ASCII parameter will be preceded by a '-'
* I: Parameters that need to be converted
* Radix: The specified cardinality
* @Date: 11:13 2017/11/29
*/
public static String toString (int i, int radix) {
if (Radix < Character.min_radix | | Radix > Character.max_radix) {
Radix = 10;
}

if (radix = = 10)
return toString (i);

Char buf[] = new CHAR[33];
Boolean negative = (i < 0);
int charpos = 32;

if (!negative)
i =-I.;

while (i <=-radix) {
buf[charpos--] = digits[-(i% radix)];
i = I/radix;
}
Buf[charpos] = digits[-i];
if (negative) {
Buf[--charpos] = '-';
}

return new String (BUF, Charpos, (33-charpos));
}

/**
* @Author: Zhuangfei
* @Description: Returns the string format of the specified argument, converting the specified integer argument to a signed decimal return
* I: Specify parameters
* @Date: 11:28 2017/11/29
*/
public static String toString (int i) {
if (i = = Integer.min_value) {
Return "-2147483648";
}
int size = (I < 0)? Stringsize (-i) + 1:stringsize (i);
char[] buf = new Char[size];
GetChars (i, size, buf);
return new String (buf, true);
}

/**
* @Author: Zhuangfei
* @Description: Put an integer into the array, the character is placed in the buffer, and then the least important from the specified index
* The number begins to traverse backwards
* I: Integer
* Index: The specified index
* BUF: Character array
* @Date: 11:33 2017/11/29
*/
static void GetChars (int i, int index, char[] buf) {
int Q, r;
int charpos = index;
char sign = 0;

if (I < 0) {
Sign = '-';
i =-I.;
}

while (i >= 65536) {
Q = i/100;
R = i-((Q << 6) + (q << 5) + (Q << 2));
i = q;
Buf[--charpos] = Digitones[r];
Buf[--charpos] = Digittens[r];
}

for (;;) {
Q = (i * 52429) >>> (16 + 3);
R = i-((Q << 3) + (q << 1));
Buf[--charpos] = Digits[r];
i = q;
if (i = = 0) break;
}
if (sign! = 0) {
Buf[--charpos] = sign;
}
}
/**
* @Author: Zhuangfei
* @Description: Parameters that require a positive number
* x: Positive Reference
* @Date: 11:42 2017/11/29
*/
static int stringsize (int x) {
for (int i = 0;; i++) {
if (x <= sizetable[i]) {
return i + 1;
}
}
}

Java source--integer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.