7zip/CPP/Common/IntToString.cpp

216 lines
4 KiB
C++
Raw Normal View History

2021-12-27 01:00:00 +01:00
// Common/IntToString.cpp
#include "StdAfx.h"
#include "../../C/CpuArch.h"
#include "IntToString.h"
#define CONVERT_INT_TO_STR(charType, tempSize) \
2024-05-14 02:00:00 +02:00
if (val < 10) \
*s++ = (charType)('0' + (unsigned)val); \
else { \
Byte temp[tempSize]; \
size_t i = 0; \
do { \
temp[++i] = (Byte)('0' + (unsigned)(val % 10)); \
val /= 10; } \
while (val >= 10); \
*s++ = (charType)('0' + (unsigned)val); \
do { *s++ = (charType)temp[i]; } \
while (--i); \
} \
2021-12-27 01:00:00 +01:00
*s = 0; \
return s;
char * ConvertUInt32ToString(UInt32 val, char *s) throw()
{
2023-06-21 02:00:00 +02:00
CONVERT_INT_TO_STR(char, 16)
2021-12-27 01:00:00 +01:00
}
char * ConvertUInt64ToString(UInt64 val, char *s) throw()
{
if (val <= (UInt32)0xFFFFFFFF)
return ConvertUInt32ToString((UInt32)val, s);
2023-06-21 02:00:00 +02:00
CONVERT_INT_TO_STR(char, 24)
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
wchar_t * ConvertUInt32ToString(UInt32 val, wchar_t *s) throw()
2021-12-27 01:00:00 +01:00
{
2024-05-14 02:00:00 +02:00
CONVERT_INT_TO_STR(wchar_t, 16)
}
wchar_t * ConvertUInt64ToString(UInt64 val, wchar_t *s) throw()
{
if (val <= (UInt32)0xFFFFFFFF)
return ConvertUInt32ToString((UInt32)val, s);
CONVERT_INT_TO_STR(wchar_t, 24)
}
void ConvertInt64ToString(Int64 val, char *s) throw()
{
if (val < 0)
2021-12-27 01:00:00 +01:00
{
2024-05-14 02:00:00 +02:00
*s++ = '-';
val = -val;
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
ConvertUInt64ToString((UInt64)val, s);
}
void ConvertInt64ToString(Int64 val, wchar_t *s) throw()
{
if (val < 0)
2021-12-27 01:00:00 +01:00
{
2024-05-14 02:00:00 +02:00
*s++ = L'-';
val = -val;
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
ConvertUInt64ToString((UInt64)val, s);
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
void ConvertUInt64ToOct(UInt64 val, char *s) throw()
{
{
UInt64 v = val;
do
s++;
while (v >>= 3);
}
*s = 0;
do
{
const unsigned t = (unsigned)val & 7;
*--s = (char)('0' + t);
}
while (val >>= 3);
}
2021-12-27 01:00:00 +01:00
2024-05-14 02:00:00 +02:00
MY_ALIGN(16) const char k_Hex_Upper[16] =
{ '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
MY_ALIGN(16) const char k_Hex_Lower[16] =
{ '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
2021-12-27 01:00:00 +01:00
void ConvertUInt32ToHex(UInt32 val, char *s) throw()
{
{
2024-05-14 02:00:00 +02:00
UInt32 v = val;
do
s++;
while (v >>= 4);
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
*s = 0;
2021-12-27 01:00:00 +01:00
do
{
2024-05-14 02:00:00 +02:00
const unsigned t = (unsigned)val & 0xF;
*--s = GET_HEX_CHAR_UPPER(t);
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
while (val >>= 4);
2021-12-27 01:00:00 +01:00
}
void ConvertUInt64ToHex(UInt64 val, char *s) throw()
{
{
2024-05-14 02:00:00 +02:00
UInt64 v = val;
do
s++;
while (v >>= 4);
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
*s = 0;
2021-12-27 01:00:00 +01:00
do
{
2024-05-14 02:00:00 +02:00
const unsigned t = (unsigned)val & 0xF;
*--s = GET_HEX_CHAR_UPPER(t);
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
while (val >>= 4);
2021-12-27 01:00:00 +01:00
}
void ConvertUInt32ToHex8Digits(UInt32 val, char *s) throw()
{
s[8] = 0;
2024-05-14 02:00:00 +02:00
int i = 7;
do
2021-12-27 01:00:00 +01:00
{
2024-05-14 02:00:00 +02:00
{ const unsigned t = (unsigned)val & 0xF; s[i--] = GET_HEX_CHAR_UPPER(t); }
{ const unsigned t = (Byte)val >> 4; val >>= 8; s[i--] = GET_HEX_CHAR_UPPER(t); }
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
while (i >= 0);
2021-12-27 01:00:00 +01:00
}
/*
void ConvertUInt32ToHex8Digits(UInt32 val, wchar_t *s)
{
s[8] = 0;
for (int i = 7; i >= 0; i--)
{
2024-05-14 02:00:00 +02:00
const unsigned t = (unsigned)val & 0xF;
2021-12-27 01:00:00 +01:00
val >>= 4;
2024-05-14 02:00:00 +02:00
s[i] = GET_HEX_CHAR(t);
2021-12-27 01:00:00 +01:00
}
}
*/
2024-05-14 02:00:00 +02:00
MY_ALIGN(16) static const Byte k_Guid_Pos[] =
{ 6,4,2,0, 11,9, 16,14, 19,21, 24,26,28,30,32,34 };
2021-12-27 01:00:00 +01:00
2024-05-14 02:00:00 +02:00
char *RawLeGuidToString(const Byte *g, char *s) throw()
2021-12-27 01:00:00 +01:00
{
2024-05-14 02:00:00 +02:00
s[ 8] = '-';
s[13] = '-';
s[18] = '-';
s[23] = '-';
s[36] = 0;
for (unsigned i = 0; i < 16; i++)
2021-12-27 01:00:00 +01:00
{
2024-05-14 02:00:00 +02:00
char *s2 = s + k_Guid_Pos[i];
const unsigned v = g[i];
s2[0] = GET_HEX_CHAR_UPPER(v >> 4);
s2[1] = GET_HEX_CHAR_UPPER(v & 0xF);
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
return s + 36;
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
char *RawLeGuidToString_Braced(const Byte *g, char *s) throw()
2021-12-27 01:00:00 +01:00
{
2024-05-14 02:00:00 +02:00
*s++ = '{';
s = RawLeGuidToString(g, s);
*s++ = '}';
*s = 0;
return s;
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
void ConvertDataToHex_Lower(char *dest, const Byte *src, size_t size) throw()
2021-12-27 01:00:00 +01:00
{
2024-05-14 02:00:00 +02:00
if (size)
2021-12-27 01:00:00 +01:00
{
2024-05-14 02:00:00 +02:00
const Byte *lim = src + size;
do
{
const unsigned b = *src++;
dest[0] = GET_HEX_CHAR_LOWER(b >> 4);
dest[1] = GET_HEX_CHAR_LOWER(b & 0xF);
dest += 2;
}
while (src != lim);
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
*dest = 0;
2021-12-27 01:00:00 +01:00
}
2024-05-14 02:00:00 +02:00
void ConvertDataToHex_Upper(char *dest, const Byte *src, size_t size) throw()
2021-12-27 01:00:00 +01:00
{
2024-05-14 02:00:00 +02:00
if (size)
{
const Byte *lim = src + size;
do
{
const unsigned b = *src++;
dest[0] = GET_HEX_CHAR_UPPER(b >> 4);
dest[1] = GET_HEX_CHAR_UPPER(b & 0xF);
dest += 2;
}
while (src != lim);
}
*dest = 0;
2021-12-27 01:00:00 +01:00
}