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;
}//