[英]How can I get the hex value of an input string using C++?
我剛開始使用 C++,幾周后我發現 C++ 不支持將字符串轉換為 Hexa 值的方法或庫。 目前,我正在研究一種方法,該方法將返回以 UTF16 編碼的輸入字符串的十六進制值。 為了更容易理解我想要做什么,我將展示我在 Java 中所做的事情。
Charset charset = Charset.forName("UTF16");
String str = "Ồ";
try {
ByteBuffer buffer = charset.newEncoder().encode(CharBuffer.wrap(str.toCharArray()));
byte[] bytes = new byte[buffer.limit()];
buffer.get(bytes, 0, buffer.limit());
System.out.println("Hex value : " + bytes); // 1ED2
}
catch (CharacterCodingException ex) {
ex.printStackTrace();
}
我在 C++ 中嘗試做的事情:
std::string convertBinToHex(std::string temp) {
long long binaryValue = atoll(temp.c_str());
long long dec_value = 0;
int base = 1;
int i = 0;
while (binaryValue) {
long long last_digit = binaryValue % 10;
binaryValue = binaryValue / 10;
dec_value += last_digit * base;
base = base * 2;
}
char hexaDeciNum[10];
while (dec_value != 0)
{
int temp = 0;
temp = dec_value % 16;
if (temp < 10)
{
hexaDeciNum[i] = temp + 48;
i++;
}
else
{
hexaDeciNum[i] = temp + 55;
i++;
}
dec_value = dec_value / 16;
}
std::string str;
for (int j = i - 1; j >= 0; j--) {
str = str + hexaDeciNum[j];
}
return str;
}
void strToBinary(wstring s, string* result)
{
int n = s.length();
for (int i = 0; i < n; i++)
{
wchar_t c = s[i];
long val = long(c);
std::string bin = "";
while (val > 0)
{
(val % 2) ? bin.push_back('1') :
bin.push_back('0');
val /= 2;
}
reverse(bin.begin(), bin.end());
result->append(convertBinToHex(bin));
}
}
我的主function:
int main()
{
std::string result;
std::wstring input = L"Ồ";
strToBinary(input, &result);
cout << result << endl;// 1ED2
return 0;
}
雖然我得到了預期值,但還有其他方法嗎? 任何幫助將非常感激。 提前致謝。
這真的很難看,可以簡化,但至少是一種改進。 如果我不在手機上,我會提供更好的東西。
auto buf = reinterpret_cast<uint8_t*>(input.data());
auto sz = (input.size() * sizeof(wchar_t));
for (size_t i = 0; i < input.size() * sizeof(wchar_t); ++i)
{
char p[8] = {0};
sprintf(p, "%02X", buf[i]);
output += p;
}
那是一個字節數組,並不重要,但如果你想迭代為 wchar_t 那就更容易了。
for (const auto& i : input)
{
char p[8] = {0};
sprintf(p, "%04X", i);
output += p;
}
您可以使用std::stringstream
以十六進制格式write
數字,然后將 output 即 stream 寫入 std::string。 這是一個工作示例:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
int main()
{
int i = 0x03AD;
std::stringstream ss;
// The following line sets up ss to use FIXED 8-digit output with leading zeros...
ss << std::setw(8) << std::setfill('0'); // Comment out or adjust as required
s << std::hex << i; // The std::hex tells the stream to use HEX format
std::string ans;
ss >> ans; // Put the formatted output into our 'answer' string
std::cout << ans << std::endl; // For demo, write the string to the console
return 0;
}
或者,將字符串轉換為十六進制數字字符串:
int main()
{
std::string ins;
std::cout << "Enter String: ";
std::cin >> ins;
std::stringstream ss;
ss << std::setfill('0') << std::hex; // Set up format
for (auto c : ins) ss << std::setw(4) << int(c); // Need to set width for each value!
std::string ans;
ss >> ans;
std::cout << ans << std::endl;
return 0;
}
有關(字符串)流格式選項的更多信息,請參見此處。
隨時要求進一步澄清和/或解釋。
只需使用提升:
using namespace std::literals;
auto u16stringg = u"你好,世界"s;
std::string result;
boost::algorithm::hex_lower(u16stringg.begin(), u16stringg.end(), std::back_inserter(result));
解釋:
u
表示創建 UTF-16字符串文字。s
表示將文字轉換為相應的std::basic_string
,在這種情況下是std::u16string
,這是通過using namespace std::literals;
見文檔。這是現場演示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.