c++ - 将 Char 指针中的十六进制转换为十进制

标签 c++ visual-c++

我有一个 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...
}

我尝试了什么:

 string str;
    str = "";

    for (int i = 0; i < size; i++)
    {
       printf ("hex[i] = %x", i, hex[i]);
       str += hex[i];
    }

    //But when i print out string value it again comes out as....

    for (int i = 0; i < size; i++)
    {
       printf ("str[i] = %x", i, str[i]);
    }

    //str[0] = 56
    //str[1] = 7D

也试过,

   std::hex // this gives a junk "decimal" value and that's no where close to the 
              //real decimal value.

同样,我没有逐一获取每个数字以转换为十进制。

那么如何将这种包含十六进制的字符指针转换为十进制?

最佳答案

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

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

//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;
}

对于这样的东西,我通常使用 the fixed width integers in cstdint而不是像 longchar 这样的类型。它可以防止非常讨厌的意外。在这里我会重写

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
}

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

关于c++ - 将 Char 指针中的十六进制转换为十进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51073238/

相关文章:

c++ - 为什么我不能将 std::unique_ptr 用作 "template<class> class"参数?

c++ - 我如何学习 Visual C++?

c# - 在 C# 中计算二进制文件符号的频率不起作用但适用于等效的 C++ 代码

c++ - 内联函数返回静态创建的对象

c++ - 无法将着色器链接到 OpenGL 中的程序对象,无法调试

c++ - VC++ MFC 应用程序无法记录鼠标数值坐标

winapi - 如何将事件日志中的设备详细信息(例如\Device\Harddisk1\DR1)映射到驱动器号(例如C :?)

c++ - 定义内核大小(C++ OpenCV)

c++ - 传递指向变量的指针或指向变量的指针的地址会导致相同的行为

c++ - Const temporary from template type 以及为什么使用 std::add_const?