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.

67 lines
1.8 KiB

  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 vector<uint8_t>& byteTable) { return bytesToString(byteTable.data(), byteTable.size()); }
  18. /**
  19. * @brief new_value="",
  20. *
  21. * @param str
  22. * @param old_value
  23. * @param new_value
  24. * @return string&
  25. */
  26. string& replaceAllDistinct(string& str, const string& old_value, const string& new_value);
  27. /**
  28. * @brief HEX字符串成Bytes数组例如
  29. *
  30. * // string("12,0a,2a,ab") ===> char buf[] = {0x12,0x0a,0x2a,0xab}
  31. * vector<uint8_t> tobytes;
  32. * hexStringToBytes("12,0a,2a,ab",",",tobytes);
  33. *
  34. * // string("120a2aab") ===> char buf[] = {0x12,0x0a,0x2a,0xab}
  35. * vector<uint8_t> tobytes;
  36. * hexStringToBytes("120a2aab","",tobytes);
  37. *
  38. * @param in
  39. * @param delims
  40. * @param byteTable byte数组
  41. * @return true
  42. * @return false
  43. */
  44. bool hexStringToBytes(string in, string delims, vector<uint8_t>& byteTable);
  45. /**
  46. * @brief
  47. *
  48. * @param value 0x55
  49. * @param bitCount 8
  50. * @param remoteZero true/false
  51. * @return string 01010101
  52. *
  53. * Demo:
  54. * StringUtils::bytet2Binary(0x55,8,true) ===> 1010101
  55. * StringUtils::bytet2Binary(0x55,8,false) ===> 01010101
  56. *
  57. */
  58. string bytet2Binary(uint32_t value, int bitCount, bool remoteZero = true);
  59. };
  60. } // namespace core
  61. } // namespace iflytop