Commit 054007cf authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Adding ordinal and itoa for round-trip conversion of character to integer and back.

parent 521219ba
Pipeline #14000 failed with stages
in 5 minutes and 13 seconds
...@@ -142,4 +142,6 @@ struct std::tm utc_to_time(const std::string &time, int &zone, int &daylight) ...@@ -142,4 +142,6 @@ struct std::tm utc_to_time(const std::string &time, int &zone, int &daylight)
return res; return res;
} }
int ordinal(char c) { return (unsigned char)c; }
} // namespace radix } // namespace radix
...@@ -17,6 +17,21 @@ ...@@ -17,6 +17,21 @@
namespace radix namespace radix
{ {
/**
* @brief ordinal convert character to its ordinal value
* @param c char
* @return integer
*/
int ordinal(char c);
/**
* @brief itoa converts integer into character
* @param number integer must be within character bounds
* @return char
*/
template <typename type = int>
char itoa(type number);
/** /**
* @brief utc_to_time UTC time to * @brief utc_to_time UTC time to
* @param time UTC formatted date and time * @param time UTC formatted date and time
......
...@@ -33,4 +33,16 @@ std::string join(const std::string &delim, const T &x) ...@@ -33,4 +33,16 @@ std::string join(const std::string &delim, const T &x)
} }
return ss.str(); return ss.str();
} }
template <typename type>
char itoa(type number)
{
char c[2];
c[0] = '0';
c[1] = '\0'; // null
if (number > type(255)) return c[0];
if (number < type(0)) return c[0];
sprintf(c, "%c", number);
return c[0];
}
} // namespace radix } // namespace radix
...@@ -104,3 +104,36 @@ TEST(Split, NoDelim) ...@@ -104,3 +104,36 @@ TEST(Split, NoDelim)
ASSERT_EQ(1, x.size()); ASSERT_EQ(1, x.size());
EXPECT_EQ("nope", x[0]); EXPECT_EQ("nope", x[0]);
} }
TEST(Radix, OrdinalItoa)
{
{
char c = itoa(127);
EXPECT_EQ('\x7F', c);
int i = ordinal(c);
EXPECT_EQ(127, i);
}
{
char c = itoa(0); // null
EXPECT_EQ('\0', c);
int i = ordinal(c);
EXPECT_EQ(0, i);
}
{
char c = itoa(99);
EXPECT_EQ('c', c);
int i = ordinal(c);
EXPECT_EQ(99, i);
}
{
char c = itoa(48);
EXPECT_EQ('0', c);
int i = ordinal(c);
EXPECT_EQ(48, i);
}
{
char c = itoa(140);
EXPECT_EQ('\x8C', c);
int i = ordinal(c);
EXPECT_EQ(140, i);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment