You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringUtils.cpp 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Created by robin on 6/20/16.
  3. //
  4. #include "StringUtils.h"
  5. const std::string StringUtils::hex = "0123456789abcdef";
  6. std::string StringUtils::rawToHuman(unsigned char c)
  7. {
  8. return std::string(1, hex[c >> 4]) + std::string(1, hex[c & 0x0f]);
  9. }
  10. std::string StringUtils::rawToHuman(char c)
  11. {
  12. return rawToHuman((unsigned char)c);
  13. }
  14. std::string StringUtils::rawToHuman(unsigned short c)
  15. {
  16. return rawToHuman((unsigned char)(c >> 8)) + rawToHuman((unsigned char)c);
  17. }
  18. std::string StringUtils::rawToHuman(short c)
  19. {
  20. return rawToHuman((unsigned short)c);
  21. }
  22. std::string StringUtils::rawToHuman(unsigned int c)
  23. {
  24. return rawToHuman((unsigned short)(c >> 16)) + rawToHuman((unsigned short)c);
  25. }
  26. std::string StringUtils::rawToHuman(int c)
  27. {
  28. return rawToHuman((unsigned int)c);
  29. }
  30. std::string StringUtils::rawToHuman(unsigned long c)
  31. {
  32. return rawToHuman((unsigned int)(c >> 32)) + rawToHuman((unsigned int)c);
  33. }
  34. std::string StringUtils::rawToHuman(long c)
  35. {
  36. return rawToHuman((unsigned long)c);
  37. }
  38. std::string StringUtils::rawToHuman(std::string raw)
  39. {
  40. std::string human;
  41. for (size_t i = 0; i < raw.size(); ++i) {
  42. human += rawToHuman(raw[i]);
  43. }
  44. return human;
  45. }
  46. ResultString StringUtils::humanToRaw(std::string human)
  47. {
  48. if (human.size() % 2 != 0) {
  49. return ResultString::error("Malformed hex data: invalid length");
  50. }
  51. std::string raw;
  52. for (size_t i = 0; i < human.size(); i += 2) {
  53. char c1 = toLower(human[i]);
  54. char c2 = toLower(human[i + 1]);
  55. size_t i1 = hex.find(c1);
  56. size_t i2 = hex.find(c2);
  57. if (i1 == std::string::npos || i2 == std::string::npos) {
  58. return ResultString::error("Malformed hex data at char " + std::to_string(i));
  59. }
  60. raw += std::string(1, (char)((i1 << 4) + i2));
  61. }
  62. return ResultString::ok(raw);
  63. }
  64. std::string StringUtils::toLower(std::string str)
  65. {
  66. std::string lower;
  67. for (size_t i = 0; i < str.size(); ++i) {
  68. lower += tolower(str[i]);
  69. }
  70. return lower;
  71. }
  72. char StringUtils::toLower(char c)
  73. {
  74. if (c >= 'A' && c <= 'Z') {
  75. return c + ('a' - 'A');
  76. }
  77. return c;
  78. }
  79. std::string StringUtils::toUpper(std::string str)
  80. {
  81. std::string upper;
  82. for (size_t i = 0; i < str.size(); ++i) {
  83. upper += toUpper(str[i]);
  84. }
  85. return upper;
  86. }
  87. char StringUtils::toUpper(char c)
  88. {
  89. if (c >= 'a' && c <= 'z') {
  90. return c - ('a' - 'A');
  91. }
  92. return c;
  93. }