久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++將字符串轉(zhuǎn)換為十六進(jìn)制,反之亦然

C++ convert string to hexadecimal and vice versa(C++將字符串轉(zhuǎn)換為十六進(jìn)制,反之亦然)
本文介紹了C++將字符串轉(zhuǎn)換為十六進(jìn)制,反之亦然的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

在 C++ 中將字符串轉(zhuǎn)換為十六進(jìn)制以及反之的最佳方法是什么?

What is the best way to convert a string to hex and vice versa in C++?

示例:

  • "Hello World" 這樣的字符串轉(zhuǎn)成十六進(jìn)制格式:48656C6C6F20576F726C64
  • 從十六進(jìn)制 48656C6C6F20576F726C64 到字符串:"Hello World"
  • A string like "Hello World" to hex format: 48656C6C6F20576F726C64
  • And from hex 48656C6C6F20576F726C64 to string: "Hello World"

推薦答案

像Hello World"這樣的字符串到十六進(jìn)制格式:48656C6C6F20576F726C64.

A string like "Hello World" to hex format: 48656C6C6F20576F726C64.

啊,給你:

#include <string>

std::string string_to_hex(const std::string& input)
{
    static const char hex_digits[] = "0123456789ABCDEF";

    std::string output;
    output.reserve(input.length() * 2);
    for (unsigned char c : input)
    {
        output.push_back(hex_digits[c >> 4]);
        output.push_back(hex_digits[c & 15]);
    }
    return output;
}

#include <stdexcept>

int hex_value(unsigned char hex_digit)
{
    static const signed char hex_values[256] = {
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
         0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
        -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    };
    int value = hex_values[hex_digit];
    if (value == -1) throw std::invalid_argument("invalid hex digit");
    return value;
}

std::string hex_to_string(const std::string& input)
{
    const auto len = input.length();
    if (len & 1) throw std::invalid_argument("odd length");

    std::string output;
    output.reserve(len / 2);
    for (auto it = input.begin(); it != input.end(); )
    {
        int hi = hex_value(*it++);
        int lo = hex_value(*it++);
        output.push_back(hi << 4 | lo);
    }
    return output;
}

(這里假設(shè)一個(gè)字符有 8 位,所以它不是很便攜,但你可以從這里獲取它.)

(This assumes that a char has 8 bits, so it's not very portable, but you can take it from here.)

這篇關(guān)于C++將字符串轉(zhuǎn)換為十六進(jìn)制,反之亦然的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

boost_1_60_0 .zip installation in windows(Windows 中的 boost_1_60_0 .zip 安裝)
How do I get console output in C++ with a Windows program?(如何使用 Windows 程序在 C++ 中獲得控制臺(tái)輸出?)
How do I calculate the week number given a date?(如何計(jì)算給定日期的周數(shù)?)
OpenCV with Network Cameras(帶有網(wǎng)絡(luò)攝像機(jī)的 OpenCV)
Export all symbols when creating a DLL(創(chuàng)建 DLL 時(shí)導(dǎo)出所有符號(hào))
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 国产一区不卡 | 成人国产在线视频 | 最新国产精品精品视频 | 日日天天 | 在线播放亚洲 | 午夜免费福利片 | 欧美日韩在线视频一区二区 | 免费在线观看91 | 91精品一区二区三区久久久久久 | 欧美精品日韩精品国产精品 | 欧美第一区 | 欧美国产亚洲一区二区 | 黑人巨大精品欧美一区二区免费 | 欧美婷婷 | 日韩成人在线观看 | 精品一区二区视频 | 久久久久亚洲精品国产 | 久久久久久久97 | 亚洲成人免费视频在线 | 四虎伊人| 免费在线看黄视频 | 亚洲精品久久久久久久久久久久久 | 国产日韩欧美一区二区 | 亚洲中午字幕 | 中文字幕亚洲一区二区三区 | 久久99视频| 国产成人av在线 | 午夜影院在线观看 | 伊人一区 | 国产日韩精品视频 | 请别相信他免费喜剧电影在线观看 | 欧美日一区 | 久久久久久免费看 | 国产精品一级在线观看 | 自拍第1页 | 水蜜桃久久夜色精品一区 | www.99re| 午夜av电影| 欧美成人一区二区 | 少妇一级淫片免费播放 | 精品一区电影 |