chore: add string::fromHexChar
This commit is contained in:
@ -187,6 +187,38 @@ std::string toHex(const std::string &subject, int width, const char fill)
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
int fromHexChar(char c)
|
||||
{
|
||||
switch (c) {
|
||||
case '0': return 0;
|
||||
case '1': return 1;
|
||||
case '2': return 2;
|
||||
case '3': return 3;
|
||||
case '4': return 4;
|
||||
case '5': return 5;
|
||||
case '6': return 6;
|
||||
case '7': return 7;
|
||||
case '8': return 8;
|
||||
case '9': return 9;
|
||||
case 'a':
|
||||
case 'A': return 10;
|
||||
case 'b':
|
||||
case 'B': return 11;
|
||||
case 'c':
|
||||
case 'C': return 12;
|
||||
case 'd':
|
||||
case 'D': return 13;
|
||||
case 'e':
|
||||
case 'E': return 14;
|
||||
case 'f':
|
||||
case 'F': return 15;
|
||||
default: return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
// clang-format on
|
||||
|
||||
void uppercase(std::string &subject)
|
||||
{
|
||||
std::transform(subject.begin(), subject.end(), subject.begin(), ::toupper);
|
||||
|
||||
@ -75,6 +75,13 @@ Return a new hexString
|
||||
*/
|
||||
std::string toHex(const std::string &subject, int width, const char fill = '0');
|
||||
|
||||
/**
|
||||
* @brief fromHexChar Convert a single char to its hexidecmal value
|
||||
* @param c input char 0-F
|
||||
* @return The value of c in Hex or -1 for invalid hex chars
|
||||
*/
|
||||
int fromHexChar(char c);
|
||||
|
||||
//! Convert to all uppercase
|
||||
/*!
|
||||
Convert each character in \c subject to uppercase
|
||||
|
||||
@ -59,6 +59,33 @@ TEST(StringTests, toHex_plaintext_hexString)
|
||||
EXPECT_EQ("666f6f626172", string::toHex("foobar", 2));
|
||||
}
|
||||
|
||||
TEST(StringTests, fromHexChar_plaintext_hexString)
|
||||
{
|
||||
EXPECT_EQ(-1, string::fromHexChar('z'));
|
||||
EXPECT_EQ(0, string::fromHexChar('0'));
|
||||
EXPECT_EQ(1, string::fromHexChar('1'));
|
||||
EXPECT_EQ(2, string::fromHexChar('2'));
|
||||
EXPECT_EQ(3, string::fromHexChar('3'));
|
||||
EXPECT_EQ(4, string::fromHexChar('4'));
|
||||
EXPECT_EQ(5, string::fromHexChar('5'));
|
||||
EXPECT_EQ(6, string::fromHexChar('6'));
|
||||
EXPECT_EQ(7, string::fromHexChar('7'));
|
||||
EXPECT_EQ(8, string::fromHexChar('8'));
|
||||
EXPECT_EQ(9, string::fromHexChar('9'));
|
||||
EXPECT_EQ(10, string::fromHexChar('a'));
|
||||
EXPECT_EQ(10, string::fromHexChar('A'));
|
||||
EXPECT_EQ(11, string::fromHexChar('b'));
|
||||
EXPECT_EQ(11, string::fromHexChar('B'));
|
||||
EXPECT_EQ(12, string::fromHexChar('c'));
|
||||
EXPECT_EQ(12, string::fromHexChar('C'));
|
||||
EXPECT_EQ(13, string::fromHexChar('d'));
|
||||
EXPECT_EQ(13, string::fromHexChar('D'));
|
||||
EXPECT_EQ(14, string::fromHexChar('e'));
|
||||
EXPECT_EQ(14, string::fromHexChar('E'));
|
||||
EXPECT_EQ(15, string::fromHexChar('f'));
|
||||
EXPECT_EQ(15, string::fromHexChar('F'));
|
||||
}
|
||||
|
||||
TEST(StringTests, uppercase_lowercaseInput_uppercaseOutput)
|
||||
{
|
||||
std::string subject = "12foo3BaR";
|
||||
|
||||
Reference in New Issue
Block a user