十六进制打印字符数组 - C++ [关闭]

问题描述 投票:0回答:0

我想打印这个字节数组

command
(十六进制格式),我需要它被声明为一个字符数组。

这是我的尝试,但我收到的只是一串零!

#include <iostream>
#include <sstream> 
using namespace std;
int main(){
    unsigned char command[10]  = {0, 0, 69, 68, 11, 0, 0, 18, 255, 7};
    stringstream stream;
    string byte;
    for (int i=0; i<10; i++){
        stream << std::hex << (unsigned int) command[i];
        stream >> byte;
        std::cout << byte << " ";
    
    }
    std::cout << "_cmd____________\n";
}

输出:

0 0 0 0 0 0 0 0 0 0 _cmd____________
我该如何解决这个问题。或者更准确地说,我在这段代码中做错了什么?!是什么让每个元素打印为 0 ?!

c++ arrays char hex sstream
© www.soinside.com 2019 - 2024. All rights reserved.