考虑小端或大端系统,将 uint32_t 转换为 4 个字节(块)并以十六进制表示法输出值

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

仅出于练习目的,我尝试将“int”反汇编为 char 并以十进制和十六进制格式打印出值。我在堆栈和其他几个地方尝试了其他问题的各种解决方案,例如:https://dev-notes.eu/2019/07/Display-char-as-Hexadecimal-String-in-C++/
但没有任何效果。

更新代码:

#include <iostream>
#include <iomanip> // std::setfill

unsigned char* intToByte(const unsigned int& N);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    std::ios_base::fmtflags f( std::cout.flags() );

    // Detecting endianness programmatically in a C++ program
    // https://stackoverflow.com/a/1001328
    int num = 1;
    if(*(char *)&num == 1)
    {
        std::cout << "\nLittle-Endian\n" << std::endl;
    }
    else
    {
        std::cout << "Big-Endian\n" << std::endl;
    }

    int arr[4] = {1,5,-3, 2200};
    int i = 0, j = 0; // automatisch NULLL;
    for(; i < 4; i++){
        unsigned char *result = intToByte(arr[i]);
        for(j = 0; j < 4; j++){
            //std::cout << std::hex << std::uppercase << (unsigned char)result[j] << std::endl;
            std::cout << std::setfill('0')
                      << std::setw(2)
                      << std::uppercase
                      << std::hex
                      << (0xFF & static_cast<unsigned>(result[j]))
                      << " "
                      << std::endl;
            std::cout.flags(f);
        }
        delete result;
    }

    return a.exec();
}

unsigned char* intToByte(const unsigned int& N) {

    std::ios_base::fmtflags f( std::cout.flags() );
    unsigned char* byte = new unsigned char[4];
    //if constexpr (std::endian::native != to_endianness)
    std::cout << "intToByte ";
    byte[0] = (N >> 24) & 0xFF;
    //std::cout << "byte[0] " << byte[0] << std::endl;
    std::cout << std::setfill('0')
              << std::setw(2)
              << std::uppercase
              << std::hex
              << (0xFF & static_cast<unsigned>(byte[0]))
              << " ";
              //<< std::endl;
    std::cout.flags(f);
    byte[1] = (N >> 16) & 0xFF;
    //std::cout << "byte[1] " << byte[1] << std::endl;
    std::cout << std::setfill('0')
              << std::setw(2)
              << std::uppercase
              << std::hex
              << (0xFF & static_cast<unsigned>(byte[1]))
              << " ";
              //<< std::endl;
    std::cout.flags(f);
    byte[2] = (N >> 8) & 0xFF;
    //std::cout << "byte[2] " << byte[2] << std::endl;
    std::cout << std::setfill('0')
              << std::setw(2)
              << std::uppercase
              << std::hex
              << (0xFF & static_cast<unsigned>(byte[2]))
              << " ";
              //<< std::endl;
    std::cout.flags(f);
    byte[3] = N & 0xFF;
    //std::cout << "byte[3] " << byte[3] << std::endl;
    std::cout << std::setfill('0')
              << std::setw(2)
              << std::uppercase
              << std::hex
              << (0xFF & static_cast<unsigned>(byte[3]))
              << " "
              << std::endl;
    std::cout.flags(f);

    return byte;
}

它似乎只正确“解码”了数组中的第一个值,而错过了其余索引,这是输出:

更新输出

Little-Endian

intToByte 00 00 00 01 
00 
00 
00 
01 
intToByte 00 00 00 05 
00 
00 
00 
05 
intToByte FF FF FF FD 
FF 
FF 
FF 
FD 
intToByte 00 00 08 98 
00 
00 
08 
98 
c++ qt ubuntu hex decimal
1个回答
0
投票

请这里的人帮忙,我要回答我自己的问题。 该代码考虑了小端和大端系统并相应地输出。

#include <QCoreApplication>

#include <iostream>
//#include <format>
#include <locale>

union foo
{
    uint32_t integer;
    unsigned char byte[4];
};

typedef foo value;

char* intToByte(const value& N);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    std::cout.imbue(std::locale("en_US.utf8"));
    std::ios_base::fmtflags f( std::cout.flags() );

    // Detecting endianness programmatically in a C++ program
    // https://stackoverflow.com/a/1001328
    int num = 1;
    if(*(char *)&num == 1)
    {
        std::cout << "\nLittle-Endian\n" << std::endl;
    }
    else
    {
        std::cout << "Big-Endian\n" << std::endl;
    }

    int arr[4] = {1,5,-3, 2200};
    int i = 0, j = 0;
    for(; i < 4; i++){
        char *result = intToByte(value{static_cast<uint32_t>(arr[i])});
        if(*(char *) & num == 1)
        {
            // Little-Endian System
            for(j = 4; j != 0; j--){
                std::cout << std::uppercase << std::hex << std::showbase << (0xFF & static_cast<char>(result[j - 1])) << " ";
                std::cout.flags(f);
                ;
                if((j-1) == 0) std::cout << std::endl;
            }
        }
        else
        {
            // Big-Endian System
            for(j = 0; j < 4; j++){
                std::cout << std::uppercase << std::hex << std::showbase << (0xFF & static_cast<char>(result[j])) << " ";
                std::cout.flags(f);
                if(j == 3) std::cout << std::endl;
            }
        }
        delete result;
    }

    return a.exec();
}

char* intToByte(const value& N) {
    char* byte = new char[4];
    std::cout << "intToByte " << std::uppercase;
    byte[0] = (N.byte[0] & 0xFF);
    std::cout  << std::hex << std::showbase << (0xFF & static_cast<char>(byte[0])) << " ";
    byte[1] = (N.byte[1] & 0xFF);
    std::cout << std::hex << std::showbase << (0xFF & static_cast<char>(byte[1])) << " ";
    byte[2] = (N.byte[2] & 0xFF);
    std::cout << std::hex << std::showbase << (0xFF & static_cast<char>(byte[2])) << " ";
    byte[3] = (N.byte[3] & 0xFF);
    std::cout << std::hex << std::showbase << (0xFF & static_cast<char>(byte[3])) << std::endl;
    std::cout.flags(f);
    return byte;
}

输出:

Little-Endian

intToByte 0X1 0 0 0
0 0 0 0X1 
intToByte 0X5 0 0 0
0 0 0 0X5 
intToByte 0XFD 0XFF 0XFF 0XFF
0XFF 0XFF 0XFF 0XFD 
intToByte 0X98 0X8 0 0
0 0 0X8 0X98 
© www.soinside.com 2019 - 2024. All rights reserved.