将字符指针中的十六进制转换为十进制 我有一个C++应用程序,它有很多API被不同的应用程序调用。 在C++应用程序中的一个功能是, long void ConvertHexToDec (char* hex, int size) { // hex - Hex value passed in as char pointer // size - size in bytes //Now for e.g., if the values are ... // hex = 567D & size = 2 for (int i = 0; i < size; i++) { printf ("hex[i] = %x", i, hex[i]); } // the above FOR loop will print // hex[0] = 56 // hex[1] = 7D // I was hoping to get each digit in a separate index like, hex[0] = 5, hex[1] = 6, hex[2] = 7, hex[3] = D //the application that calls this C++ API is reading values from a hardware //device and get the values in hex, and then call this API to convert it to //decimal. //so in above example it reads memory location 0xB10A and get a 2 byte value //of 567D //I see many examples of hex to decimal conversion in C++, but all of them //uses logic to convert by taking one value at a time. //so from above example, it will start at D and then convert that to decimal //and then take 7 and convert that and then next and so on...... //Here there's no way i can do that, as every byte has 2 digits in it. //And this is my challenge and i have no idea... }

将字符指针中的十六进制转换为十进制 我有一个C++应用程序,它有很多API被不同的应用程序调用。 在C++应用程序中的一个功能是, long void ConvertHexToDec (char* hex, int size) { // hex - Hex value passed in as char pointer // size - size in bytes //Now for e.g., if the values are ... // hex = 567D & size = 2 for (int i = 0; i < size; i++) { printf ("hex[i] = %x", i, hex[i]); } // the above FOR loop will print // hex[0] = 56 // hex[1] = 7D // I was hoping to get each digit in a separate index like, hex[0] = 5, hex[1] = 6, hex[2] = 7, hex[3] = D //the application that calls this C++ API is reading values from a hardware //device and get the values in hex, and then call this API to convert it to //decimal. //so in above example it reads memory location 0xB10A and get a 2 byte value //of 567D //I see many examples of hex to decimal conversion in C++, but all of them //uses logic to convert by taking one value at a time. //so from above example, it will start at D and then convert that to decimal //and then take 7 and convert that and then next and so on...... //Here there's no way i can do that, as every byte has 2 digits in it. //And this is my challenge and i have no idea... },c++,visual-c++,C++,Visual C++,再一次,我并没有一个接一个地得到每个数字来转换成十进制 那么我能做些什么来将这种包含十六进制的字符指针转换成十进制呢?我假设param char*hex包含可以直接读取的物理地址 然后使用 long ConvertHexToDec2Bytes(char* hex) { const auto n = *(short*)hex; #ifdef WANNA_COUT std::cout << n << std::endl; #endif #ifdef WANN

再一次,我并没有一个接一个地得到每个数字来转换成十进制


那么我能做些什么来将这种包含十六进制的字符指针转换成十进制呢?

我假设param char*hex包含可以直接读取的物理地址

然后使用

long ConvertHexToDec2Bytes(char* hex)
{
    const auto n = *(short*)hex;

#ifdef WANNA_COUT
    std::cout << n << std::endl;
#endif
#ifdef WANNA_STRING
    const auto str = std::to_string(n);
#endif

    return n;
}
long ConvertHexToDec2Bytes(字符*hex)
{
常量自动n=*(短*)十六进制;
#我想去

我假设param char*hex包含可以直接读取的物理地址

然后使用

long ConvertHexToDec2Bytes(char* hex)
{
    const auto n = *(short*)hex;

#ifdef WANNA_COUT
    std::cout << n << std::endl;
#endif
#ifdef WANNA_STRING
    const auto str = std::to_string(n);
#endif

    return n;
}
long ConvertHexToDec2Bytes(字符*hex)
{
常量自动n=*(短*)十六进制;
#我想去

从代码块中的描述来看,似乎不需要字符串或复杂的转换。他们似乎只想将大端字节数组转换为本机端字节数

代码中嵌入的注释,其中似乎需要更多解释或警告

//long void ConvertHexToDec (char* hex, int size) has been changed to
long ConvertHexToDec (const char* hex, int size)
// const char * much more versatile than char * and since we aren't changing hex
// might as well make it const. And what the heck is a long void? A big nothing?
{
    long result = hex[0]; // assuming hex not NULL and size > 0
    for (int i = 1; i < size; i++) // loop until out of bytes. Note: long might only
                                   // hold 4 bytes.
    {
        result <<= 8; // shift current data over one byte
        result += (unsigned char)hex[i]; // add in new byte. Cast required to avoid sign 
                                         // extension during the math if char happens to
                                         // be signed. Note that overflow of the long 
                                         // can bring nasty surprises of its own
    }
    return result;
}

请注意,您可能必须将返回的
uint32\t
转换为有符号类型。通常最好在调用后执行此操作,并且您已经测试并确认所读取的内容有效且可用。

从代码块中的描述来看,似乎不需要字符串或复杂的转换。他们似乎只需要将大端字节数组转换为本机端字节数

代码中嵌入的注释,其中似乎需要更多解释或警告

//long void ConvertHexToDec (char* hex, int size) has been changed to
long ConvertHexToDec (const char* hex, int size)
// const char * much more versatile than char * and since we aren't changing hex
// might as well make it const. And what the heck is a long void? A big nothing?
{
    long result = hex[0]; // assuming hex not NULL and size > 0
    for (int i = 1; i < size; i++) // loop until out of bytes. Note: long might only
                                   // hold 4 bytes.
    {
        result <<= 8; // shift current data over one byte
        result += (unsigned char)hex[i]; // add in new byte. Cast required to avoid sign 
                                         // extension during the math if char happens to
                                         // be signed. Note that overflow of the long 
                                         // can bring nasty surprises of its own
    }
    return result;
}

请注意,您可能必须将返回的
uint32\u t
转换为有符号类型。通常最好在调用后执行此操作,并且您已经测试并确认所读取的内容有效且可用。

给定567D的输入,您期望的最终输出是什么?22141?32086?根据endian,我将循环
result@user4581301-我期望22141。给定567D的输入,你期望的最终输出是什么?22141?32086?根据endian,我将循环
result@user4581301-我期望22141。你的语句
if(size>0 | | size No)你得到了我。谢谢。修复。你的语句
if(size>0 | | size No)你得到了我。谢谢。修复。
uint32_t ConvertHexToDec (const uint8_t* hex, size_t size)
{
    if (size > 0 && size <= sizeof(uint32_t)) // no surprises. Up to 4 bytes regardless 
                                              // of target, and no signed overflow.
    {
        uint32_t result = hex[0];
        for (size_t i = 1; i < size; i++)
        {
            result <<= 8;
            result += hex[i];
        }
        return result;
    }
    throw std::out_of_range("Invalid size"); // can't convert = no result
}