I wrote a general algorithm that converts integers into strings very slowly. Haha

Source: Internet
Author: User

A general, slow integer that I wrote to convert to a stringAlgorithm, Haha

I was prompted by a netizen, but I couldn't understand him. I implemented it using a model and division algorithm based on his mocking explanation. in fact, it is very common and can be expanded, but the speed must not be more efficient than the library functions written by masters.

// Maximum compatible Integer Conversion // inefficient // Binary Conversion
STD: String uitoa_2 (unsigned _ int64 V)
{
STD: String r = "";
STD: string Z = "";

// Unsigned will not have negative numbers
// If (v <0)
//{
// V = 0-v;
// Z = '-';
//}

Char C;
Byte B;
Unsigned _ int64 d = 1; // denominator
Unsigned _ int64 I = 0; // Current Value

While (1)
{
B = (V % (D * 2)/d;
C = B + '0 ';
// If (B> 9) C = B + 'a'-10;
If (V/D = 0) break;

R = C + R;
D = D * 2;
}

Return Z + R;
}//

// Maximum compatible Integer Conversion // inefficient // hexadecimal
STD: String uitoa_hex (unsigned _ int64 V)
{
STD: String r = "";
STD: string Z = "";

// Unsigned will not have negative numbers
// If (v <0)
//{
// V = 0-v;
// Z = '-';
//}

Char C;
Byte B;
Unsigned _ int64 d = 1; // denominator
Unsigned _ int64 I = 0; // Current Value

While (1)
{
B = (V % (D * 16)/d;
C = B + '0 ';
If (B> 9) C = B + 'a'-10;
If (V/D = 0) break;

R = C + R;
D = D * 16;
}

Return Z + R;
}//

// Maximum compatible Integer Conversion // inefficient
STD: String uitoa (unsigned _ int64 V)
{
STD: String r = "";
STD: string Z = "";

// Unsigned will not have negative numbers
// If (v <0)
//{
// V = 0-v;
// Z = '-';
//}

Char C;
Byte B;
Unsigned _ int64 d = 1; // denominator
Unsigned _ int64 I = 0; // Current Value

While (1)
{
B = (V % (D * 10)/d;
C = B + '0 ';
If (V/D = 0) break;

R = C + R;
D = D * 10;
}

Return Z + R;
}//

// Maximum compatible Integer Conversion // inefficient
STD: String ITOA (signed _ int64 V)
{
STD: String r = "";
STD: string Z = "";

// Unsigned will not have negative numbers
If (v <0)
{
V = 0-v;
Z = '-';
}

Char C;
Byte B;
Unsigned _ int64 d = 1; // denominator
Unsigned _ int64 I = 0; // Current Value

While (1)
{
B = (V % (D * 10)/d;
C = B + '0 ';
If (V/D = 0) break;

R = C + R;
D = D * 10;
}

Return Z + R;
}//

STD: String string2hex (STD: String V)
{
STD: String r = "";
STD: string one = "";

For (size_t I = 0; I <v. Size (); I ++)
{
Char c = V [I];
One = uitoa_hex (C );
If (one. Size () = 2)
{
R + = one;
}
Else
{
R + = '0' + one;
}
}

Return R;
}//

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.