Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added HexToInt function and unit tests for it.
authorSteve Brokenshire <sbrokenshire@xestia.co.uk>
Sun, 17 Apr 2016 13:09:08 +0000 (14:09 +0100)
committerSteve Brokenshire <sbrokenshire@xestia.co.uk>
Sun, 17 Apr 2016 13:09:08 +0000 (14:09 +0100)
HexToInt converts a value from a string to a integer. Pointers are used
to optimise the memory usage.

source/common/text.cpp
source/common/text.h
source/tests/xestiacalendar_commonfunctions.h

index df64411..d223993 100644 (file)
@@ -185,4 +185,75 @@ PropertyNameValue SplitNameValue(string InputData){
        
        return FinalNameValue;
        
+}
+
+bool HexToInt(std::string *HexString, int *Number){
+       
+       // Check that each character in the string is a number
+       // or a letter (a-f/A-F).
+       
+       char Char = 0;
+       int CharNum = 0;
+       
+       for (int CharSeek = 0; 
+               CharSeek < HexString->size(); CharSeek++){
+               
+               // Check if character is a number (0-9).
+                       
+               Char = HexString->at(CharSeek);
+               CharNum = Char;
+                       
+               if (CharNum >= 48 &&
+                       CharNum <= 57){
+               
+                       continue;
+                               
+               }
+               
+               // Check if character is a letter (A-F)
+               
+               if (CharNum >= 65 &&
+                       CharNum <= 70){
+               
+                       continue;
+                               
+               }
+               
+               // Check if character is a letter (a-f).
+
+               if (CharNum >= 97 &&
+                       CharNum <= 102){
+               
+                       continue;
+                               
+               }
+               
+               // Exit from subroutine as character is invalid.
+               
+               return false;
+               
+       }
+       
+       // Convert a Hex value that is in string to integer.
+       
+       try {
+       
+               *Number = stoi((*HexString), nullptr, 16);
+       
+       }
+       
+       catch (const std::invalid_argument &err){
+               
+               return false;
+               
+       }
+       
+       catch (const std::out_of_range &err){
+               
+               return false;
+               
+       }
+       
+       return true;
+       
 }
\ No newline at end of file
index 0cc513a..f50113e 100644 (file)
@@ -2,9 +2,11 @@
 #define __COMMON_TEXT_H__
 
 #include <string>
+#include <sstream>
 #include <vector>
 #include <map>
 #include <iostream>
+#include <stdexcept>
 
 struct PropertyNameValue{
        std::string Name;
@@ -17,5 +19,6 @@ std::multimap<std::string, std::string> ProcessTextVectors(std::vector<std::stri
        std::string Property);
 std::map<std::string, std::string> SplitValues(std::string InputData);
 PropertyNameValue SplitNameValue(std::string InputData);
+bool HexToInt(std::string *HexString, int *Number);
 
 #endif
\ No newline at end of file
index 85ef33e..ce4fc7c 100644 (file)
@@ -197,4 +197,65 @@ TEST(CommonFunctions, SplitNameValueTests){
        ASSERT_EQ(PropertyName, "TEST");
        ASSERT_EQ(PropertyValue, "OK");
        
+}
+
+TEST(CommonFunctions, HexToInt){
+       
+       string Value1 = "10";           // 16
+       string Value2 = "50";           // 80
+       string Value3 = "4F";           // 79
+       string Value4 = "FF";           // 255
+       string Value5 = "FFF";          // 4095
+       string Value6 = "FFFF";         // 65535
+       string Value7 = "75AB";         // 30123
+       string Value8 = "2AC";          // 684
+       string Value9 = "!";            // Fail
+       string Value10 = "4BZ";         // Fail
+       string Value11 = "Z?!$";        // Fail
+       
+       int OutputValue = 0;
+       bool Result = false;
+       
+       Result = HexToInt(&Value1, &OutputValue);
+       
+       ASSERT_EQ(Result, true);
+       ASSERT_EQ(OutputValue, 16);
+
+       Result = HexToInt(&Value2, &OutputValue);       
+       ASSERT_EQ(Result, true);
+       ASSERT_EQ(OutputValue, 80);
+
+       Result = HexToInt(&Value3, &OutputValue);       
+       ASSERT_EQ(Result, true);
+       ASSERT_EQ(OutputValue, 79);
+       
+       Result = HexToInt(&Value4, &OutputValue);       
+       ASSERT_EQ(Result, true);
+       ASSERT_EQ(OutputValue, 255);
+       
+       Result = HexToInt(&Value5, &OutputValue);       
+       ASSERT_EQ(Result, true);
+       ASSERT_EQ(OutputValue, 4095);
+
+       Result = HexToInt(&Value6, &OutputValue);       
+       ASSERT_EQ(Result, true);
+       ASSERT_EQ(OutputValue, 65535);
+       
+       Result = HexToInt(&Value7, &OutputValue);       
+       ASSERT_EQ(Result, true);
+       ASSERT_EQ(OutputValue, 30123);
+       
+       Result = HexToInt(&Value8, &OutputValue);       
+       ASSERT_EQ(Result, true);
+       ASSERT_EQ(OutputValue, 684);
+       
+       Result = HexToInt(&Value9, &OutputValue);
+       ASSERT_EQ(Result, false);
+
+       Result = HexToInt(&Value10, &OutputValue);      
+       ASSERT_EQ(Result, false);
+       
+       Result = HexToInt(&Value11, &OutputValue);
+       ASSERT_EQ(Result, false);
+
 }
\ No newline at end of file
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy