StringToInt.cpp revision baa3858d3f5d128a5c8466b700098109edcad5f2
1// Common/StringToInt.cpp
2
3#include "StdAfx.h"
4
5#include "StringToInt.h"
6
7UInt64 ConvertStringToUInt64(const char *s, const char **end)
8{
9  UInt64 result = 0;
10  for (;;)
11  {
12    char c = *s;
13    if (c < '0' || c > '9')
14    {
15      if (end != NULL)
16        *end = s;
17      return result;
18    }
19    result *= 10;
20    result += (c - '0');
21    s++;
22  }
23}
24
25UInt64 ConvertOctStringToUInt64(const char *s, const char **end)
26{
27  UInt64 result = 0;
28  for (;;)
29  {
30    char c = *s;
31    if (c < '0' || c > '7')
32    {
33      if (end != NULL)
34        *end = s;
35      return result;
36    }
37    result <<= 3;
38    result += (c - '0');
39    s++;
40  }
41}
42
43UInt64 ConvertHexStringToUInt64(const char *s, const char **end)
44{
45  UInt64 result = 0;
46  for (;;)
47  {
48    char c = *s;
49    UInt32 v;
50    if (c >= '0' && c <= '9') v = (c - '0');
51    else if (c >= 'A' && c <= 'F') v = 10 + (c - 'A');
52    else if (c >= 'a' && c <= 'f') v = 10 + (c - 'a');
53    else
54    {
55      if (end != NULL)
56        *end = s;
57      return result;
58    }
59    result <<= 4;
60    result |= v;
61    s++;
62  }
63}
64
65
66UInt64 ConvertStringToUInt64(const wchar_t *s, const wchar_t **end)
67{
68  UInt64 result = 0;
69  for (;;)
70  {
71    wchar_t c = *s;
72    if (c < '0' || c > '9')
73    {
74      if (end != NULL)
75        *end = s;
76      return result;
77    }
78    result *= 10;
79    result += (c - '0');
80    s++;
81  }
82}
83
84
85Int64 ConvertStringToInt64(const char *s, const char **end)
86{
87  if (*s == '-')
88    return -(Int64)ConvertStringToUInt64(s + 1, end);
89  return ConvertStringToUInt64(s, end);
90}
91