1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "string_utils.h"
17
18 #include <sstream>
19 #include <string_ex.h>
20
21 namespace OHOS {
22 namespace Telephony {
StringUtils()23 StringUtils::StringUtils() {}
24
~StringUtils()25 StringUtils::~StringUtils() {}
26
HexCharToInt(char c)27 uint16_t StringUtils::HexCharToInt(char c)
28 {
29 const uint8_t decimal = 10;
30 if (c >= '0' && c <= '9') {
31 return (c - '0');
32 }
33 if (c >= 'A' && c <= 'F') {
34 return (c - 'A' + decimal);
35 }
36 if (c >= 'a' && c <= 'f') {
37 return (c - 'a' + decimal);
38 }
39 return 0;
40 }
41
StringToHex(const std::string & data)42 std::string StringUtils::StringToHex(const std::string &data)
43 {
44 std::stringstream ss;
45 for (std::string::size_type i = 0; i < data.size(); ++i) {
46 unsigned char temp = static_cast<unsigned char>(data[i]) >> HEX_OFFSET;
47 ss << HEX_TABLE[temp] << HEX_TABLE[static_cast<unsigned char>(data[i]) & 0xf];
48 }
49 return ss.str();
50 }
51
StringToHex(const char * data,int byteLength)52 std::string StringUtils::StringToHex(const char *data, int byteLength)
53 {
54 std::string str("");
55 std::stringstream ss;
56 for (int i = 0; i < byteLength; ++i) {
57 unsigned char temp = static_cast<unsigned char>(data[i]) >> HEX_OFFSET;
58 ss << HEX_TABLE[temp] << HEX_TABLE[static_cast<unsigned char>(data[i]) & 0xf];
59 }
60 return ss.str();
61 }
62
StringToHex(const std::vector<uint8_t> & data)63 std::string StringUtils::StringToHex(const std::vector<uint8_t> &data)
64 {
65 std::string str("");
66 std::stringstream ss;
67 for (std::size_t i = 0; i < data.size(); ++i) {
68 unsigned char temp = static_cast<unsigned char>(data[i]) >> HEX_OFFSET;
69 ss << HEX_TABLE[temp] << HEX_TABLE[static_cast<unsigned char>(data[i]) & 0xf];
70 }
71 return ss.str();
72 }
73
HexToString(const std::string & str)74 std::string StringUtils::HexToString(const std::string &str)
75 {
76 std::string result;
77 uint8_t hexDecimal = 16;
78 uint8_t hexStep = 2;
79 if (str.length() <= 0) {
80 return result;
81 }
82 for (size_t i = 0; i < str.length() - 1; i += STEP_2BIT) {
83 std::string byte = str.substr(i, hexStep);
84 char chr = 0;
85 long strTemp = strtol(byte.c_str(), nullptr, hexDecimal);
86 if (strTemp > 0) {
87 chr = static_cast<char>(strTemp);
88 }
89 result.push_back(chr);
90 }
91 return result;
92 }
93
HexToByteVector(const std::string & str)94 std::vector<uint8_t> StringUtils::HexToByteVector(const std::string &str)
95 {
96 std::vector<uint8_t> ret;
97 int sz = str.length();
98 if (sz <= 0) {
99 return ret;
100 }
101 for (int i = 0; i < (sz - 1); i += STEP_2BIT) {
102 auto temp = static_cast<uint8_t>((HexCharToInt(str.at(i)) << HEX_OFFSET) | HexCharToInt(str.at(i + 1)));
103 ret.push_back(temp);
104 }
105 return ret;
106 }
107
ToUtf8(const std::u16string & str16)108 std::string StringUtils::ToUtf8(const std::u16string &str16)
109 {
110 std::string ret;
111 if (str16.empty()) {
112 return ret;
113 }
114 return Str16ToStr8(str16);
115 }
116
ToUtf16(const std::string & str)117 std::u16string StringUtils::ToUtf16(const std::string &str)
118 {
119 std::u16string ret;
120 if (str.empty()) {
121 return ret;
122 }
123 return Str8ToStr16(str);
124 }
125 } // namespace Telephony
126 } // namespace OHOS