第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將十六進(jìn)制字符串轉(zhuǎn)換為字節(jié)數(shù)組

將十六進(jìn)制字符串轉(zhuǎn)換為字節(jié)數(shù)組

將十六進(jìn)制字符串轉(zhuǎn)換為字節(jié)數(shù)組轉(zhuǎn)換可變長(zhǎng)度十六進(jìn)制字符串的最佳方法是什么?"01A1"到包含該數(shù)據(jù)的字節(jié)數(shù)組。即轉(zhuǎn)換為:std::string = "01A1";變成這樣char* hexArray;int hexLength;或者這個(gè)std::vector<char> hexArray;所以當(dāng)我把這個(gè)寫(xiě)到一個(gè)文件hexdump -C我得到的二進(jìn)制數(shù)據(jù)包含01A1.
查看完整描述

4 回答

?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊

所以為了好玩,我很好奇我是否能在編譯時(shí)完成這種轉(zhuǎn)換。它沒(méi)有太多的錯(cuò)誤檢查,并且是在VS 2015中完成的,它還不支持C+14 Conexpr函數(shù)(這就是HexCharToInt的樣子)。它采用c-字符串?dāng)?shù)組,將一對(duì)字符轉(zhuǎn)換為單個(gè)字節(jié),并將這些字節(jié)展開(kāi)為一個(gè)統(tǒng)一的初始化列表,用于初始化作為模板參數(shù)提供的T類(lèi)型。T可以替換為像std:Array這樣的東西來(lái)自動(dòng)返回?cái)?shù)組。

#include <cstdint>#include <initializer_list>#include <stdexcept>#include <utility>/* Quick and dirty conversion from a single character to its hex equivelent */constexpr std::uint8_t HexCharToInt(char Input){
    return
    ((Input >= 'a') && (Input <= 'f'))
    ? (Input - 87)
    : ((Input >= 'A') && (Input <= 'F'))
    ? (Input - 55)
    : ((Input >= '0') && (Input <= '9'))
    ? (Input - 48)
    : throw std::exception{};}/* Position the characters into the appropriate nibble */constexpr std::uint8_t HexChar(char High, char Low){
    return (HexCharToInt(High) << 4) | (HexCharToInt(Low));}/* Adapter that performs sets of 2 characters into a single byte and combine the results into a uniform initialization list used to initialize T */template <typename T, std::size_t Length, std::size_t ... Index>constexpr T HexString(const char (&Input)[Length], const std::index_sequence<Index...>&){
    return T{HexChar(Input[(Index * 2)], Input[((Index * 2) + 1)])...};}/* Entry function */template <typename T, std::size_t Length>constexpr T HexString(const char (&Input)[Length]){
    return HexString<T>(Input, std::make_index_sequence<(Length / 2)>{});}constexpr auto Y = KS::Utility::HexString<std::array<std::uint8_t, 3>>("ABCDEF");


查看完整回答
反對(duì) 回復(fù) 2019-08-04
?
蝴蝶刀刀

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果您想使用OpenSSL來(lái)完成它,我發(fā)現(xiàn)了一個(gè)巧妙的技巧:

BIGNUM *input = BN_new();int input_length = BN_hex2bn(&input, argv[2]);input_length = (input_length + 1) / 2; // BN_hex2bn() returns number of hex digitsunsigned char *input_buffer = (unsigned char*)malloc(input_length);retval = BN_bn2bin(input, input_buffer);

只需確保去掉任何引導(dǎo)‘0x’到字符串。



查看完整回答
反對(duì) 回復(fù) 2019-08-04
  • 4 回答
  • 0 關(guān)注
  • 376 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)