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.

68 lines
1.9 KiB

2 years ago
  1. #pragma once
  2. #include <algorithm>
  3. #include <regex>
  4. #include <sstream>
  5. #include <string>
  6. #include <vector>
  7. namespace iflytop {
  8. namespace core {
  9. using namespace std;
  10. class StringUtils {
  11. char byteToChar(uint8_t byte);
  12. bool isLegalHex(char c);
  13. public:
  14. string upper(const string& value);
  15. string lower(const string& value);
  16. string bytesToString(const uint8_t* data, size_t size);
  17. string bytesToString(const char* data, size_t size) { return bytesToString((const uint8_t*)data, size); }
  18. string bytesToString(const vector<uint8_t>& byteTable) { return bytesToString(byteTable.data(), byteTable.size()); }
  19. /**
  20. * @brief new_value="",
  21. *
  22. * @param str
  23. * @param old_value
  24. * @param new_value
  25. * @return string&
  26. */
  27. string& replaceAllDistinct(string& str, const string& old_value, const string& new_value);
  28. /**
  29. * @brief HEX字符串成Bytes数组例如
  30. *
  31. * // string("12,0a,2a,ab") ===> char buf[] = {0x12,0x0a,0x2a,0xab}
  32. * vector<uint8_t> tobytes;
  33. * hexStringToBytes("12,0a,2a,ab",",",tobytes);
  34. *
  35. * // string("120a2aab") ===> char buf[] = {0x12,0x0a,0x2a,0xab}
  36. * vector<uint8_t> tobytes;
  37. * hexStringToBytes("120a2aab","",tobytes);
  38. *
  39. * @param in
  40. * @param delims
  41. * @param byteTable byte数组
  42. * @return true
  43. * @return false
  44. */
  45. bool hexStringToBytes(string in, string delims, vector<uint8_t>& byteTable);
  46. /**
  47. * @brief
  48. *
  49. * @param value 0x55
  50. * @param bitCount 8
  51. * @param remoteZero true/false
  52. * @return string 01010101
  53. *
  54. * Demo:
  55. * StringUtils::bytet2Binary(0x55,8,true) ===> 1010101
  56. * StringUtils::bytet2Binary(0x55,8,false) ===> 01010101
  57. *
  58. */
  59. string bytet2Binary(uint32_t value, int bitCount, bool remoteZero = true);
  60. };
  61. } // namespace core
  62. } // namespace iflytop