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#include "cstddef" 22#include "cstdint" 23#include "cstdlib" 24#include "ostream" 25#include "string" 26#include "telephony_log_wrapper.h" 27#include "vector" 28 29namespace OHOS { 30namespace Telephony { 31static constexpr uint8_t HEX_OFFSET = 4; 32static constexpr uint8_t STEP_2BIT = 2; 33static constexpr char HEX_TABLE[] = "0123456789ABCDEF"; 34 35StringUtils::StringUtils() {} 36 37StringUtils::~StringUtils() {} 38 39uint16_t StringUtils::HexCharToInt(char c) 40{ 41 const uint8_t decimal = 10; 42 if (c >= '0' && c <= '9') { 43 return (c - '0'); 44 } 45 if (c >= 'A' && c <= 'F') { 46 return (c - 'A' + decimal); 47 } 48 if (c >= 'a' && c <= 'f') { 49 return (c - 'a' + decimal); 50 } 51 return 0; 52} 53 54std::string StringUtils::StringToHex(const std::string &data) 55{ 56 std::stringstream ss; 57 for (std::string::size_type i = 0; i < data.size(); ++i) { 58 unsigned char temp = static_cast<unsigned char>(data[i]) >> HEX_OFFSET; 59 ss << HEX_TABLE[temp] << HEX_TABLE[static_cast<unsigned char>(data[i]) & 0xf]; 60 } 61 return ss.str(); 62} 63 64std::string StringUtils::StringToHex(const char *data, int byteLength) 65{ 66 std::stringstream ss; 67 for (int i = 0; i < byteLength; ++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 74std::string StringUtils::StringToHex(const std::vector<uint8_t> &data) 75{ 76 std::stringstream ss; 77 for (std::size_t i = 0; i < data.size(); ++i) { 78 unsigned char temp = static_cast<unsigned char>(data[i]) >> HEX_OFFSET; 79 ss << HEX_TABLE[temp] << HEX_TABLE[static_cast<unsigned char>(data[i]) & 0xf]; 80 } 81 return ss.str(); 82} 83 84std::string StringUtils::HexToString(const std::string &str) 85{ 86 std::string result; 87 uint8_t hexDecimal = 16; 88 uint8_t hexStep = 2; 89 if (str.length() <= 0) { 90 return result; 91 } 92 for (size_t i = 0; i < str.length() - 1; i += STEP_2BIT) { 93 std::string byte = str.substr(i, hexStep); 94 char chr = 0; 95 long strTemp = strtol(byte.c_str(), nullptr, hexDecimal); 96 if (strTemp > 0) { 97 chr = static_cast<char>(strTemp); 98 } 99 result.push_back(chr); 100 } 101 return result; 102} 103 104std::vector<uint8_t> StringUtils::HexToByteVector(const std::string &str) 105{ 106 std::vector<uint8_t> ret; 107 int sz = static_cast<int>(str.length()); 108 if (sz <= 0) { 109 return ret; 110 } 111 for (int i = 0; i < (sz - 1); i += STEP_2BIT) { 112 auto temp = static_cast<uint8_t>((HexCharToInt(str.at(i)) << HEX_OFFSET) | HexCharToInt(str.at(i + 1))); 113 ret.push_back(temp); 114 } 115 return ret; 116} 117 118std::string StringUtils::ToUtf8(const std::u16string &str16) 119{ 120 if (str16.empty()) { 121 std::string ret; 122 return ret; 123 } 124 return Str16ToStr8(str16); 125} 126 127std::u16string StringUtils::ToUtf16(const std::string &str) 128{ 129 if (str.empty()) { 130 std::u16string ret; 131 return ret; 132 } 133 return Str8ToStr16(str); 134} 135 136bool StringUtils::IsEmpty(const std::string &str) 137{ 138 if (str.empty()) { 139 return true; 140 } 141 std::string strTemp = TrimStr(str); 142 return strTemp.empty() || strlen(strTemp.c_str()) == 0; 143} 144} // namespace Telephony 145} // namespace OHOS