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

C++將字符串轉換為十六進制,反之亦然

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

問題描述

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

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

示例:

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

推薦答案

像Hello World"這樣的字符串到十六進制格式: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;
}

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

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

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

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

相關文檔推薦

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++ 中獲得控制臺輸出?)
How do I calculate the week number given a date?(如何計算給定日期的周數?)
OpenCV with Network Cameras(帶有網絡攝像機的 OpenCV)
Export all symbols when creating a DLL(創建 DLL 時導出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 亚洲国产精品一区二区久久 | 在线一区二区三区 | 亚洲国产精品一区二区三区 | 手机看片169 | 国产资源在线播放 | 国产欧美日韩一区二区三区在线 | 国产精品成人久久久久a级 久久蜜桃av一区二区天堂 | 国内在线视频 | 亚洲精品视频在线观看视频 | 亚洲在线观看视频 | 二区中文 | 综合精品久久久 | 亚洲欧美日韩中文在线 | 91在线免费视频 | 久久精品国产一区二区三区不卡 | 一区二区三区欧美在线观看 | 九九综合九九 | 国产黄色网址在线观看 | 在线观看av不卡 | 日韩欧美一二三区 | 中文字幕av在线 | 欧美一二三四成人免费视频 | 涩涩鲁亚洲精品一区二区 | 久久精品国产免费 | 亚洲成人av在线播放 | 久草新视频 | 在线色网| 欧美亚洲一级 | 欧美一区二区三区精品免费 | 日本一本在线 | 亚洲色图第一页 | 精品视频一区二区在线观看 | 97在线观视频免费观看 | 中文字幕 视频一区 | 人妖一区 | 国产成人精品999在线观看 | 成人午夜激情 | 在线一区二区三区 | 深夜福利亚洲 | 国产在线高清 | 欧美激情欧美激情在线五月 |