简体   繁体   English

c ++将整数转换为8个字符的十六进制,删除前两个字符,使其仅为6个字符的十六进制,然后转换回整数

[英]c++ convert integer to 8 char hex, drop the first two char so that it is only a 6 char hex, and convert back to an integer

I have been searching and experimenting for many, many, hours now, and so far I have not been able to adapt any of the solutions I have come across to do what I want. 我已经进行了许多小时的搜索和试验,到目前为止,我还无法适应我遇到的任何解决方案来做自己想做的事情。

My goal is to take an integer (538214658) and convert it into an 8 character hex string (020148102). 我的目标是采用整数(538214658)并将其转换为8个字符的十六进制字符串(020148102)。 Then I want to drop the first two characters (0148102) and convert it back into an integer(1343746) which I am using as a key in a map array. 然后,我想删除前两个字符(0148102),并将其转换回一个整数(1343746),我将其用作映射数组中的键。

The solutions I've seen so far just convert an integer into hex string, but don't take into account the desired digit length. 到目前为止,我所看到的解决方案只是将整数转换为十六进制字符串,但没有考虑所需的数字长度。

I am able to print out just the first 6 characters using the following code: 我可以使用以下代码仅打印前6个字符:

Console_Print("%06X", form ? form->refID : 0)

So I thought that maybe I could use that technique to store it into a string, and then use iostream or sstream to convert it back to an integer, but none of my searches turned up anything I could use. 因此,我认为也许可以使用该技术将其存储到字符串中,然后使用iostream或sstream将其转换回整数,但是我的搜索都找不到我可以使用的任何东西。 And all of my experiments have failed. 而且我所有的实验都失败了。

Some help would be greatly appreciated. 一些帮助将不胜感激。

EDIT: Below is my solution based on Klaus' suggestion: 编辑:以下是基于克劳斯的建议的解决方案:

uint32_t GetCoreRefID(TESForm* form)
{
    uint32_t iCoreRefID = 0;
    if (form)
    {
        uint32_t iRefID = (uint32_t)(form->refID);
        iCoreRefID = iRefID & 0x00ffffff;
    }
    return iCoreRefID;
}

There is no need to convert to a string representation. 无需转换为字符串表示形式。

Look the following example: 看下面的例子:

int main()
{
    uint32_t val = 538214658 & 0x00ffffff;
    std::cout << std::hex << val << std::endl;
    std::cout << std::dec << val << std::endl;

}

You have to learn that a value is still only a value and is not dependent on the representation like decimal or hex. 您必须了解一个值仍然只是一个值,并不依赖于十进制或十六进制等表示形式。 The value stored in a memory area or a register is still the same. 存储在存储区或寄存器中的值仍然相同。

As you can see in the given example I wrote your decimal value representation and remove the first two hexadecimal digits simply by do a bitwise and operation with the hexadecimal representation of a mask. 正如您在给定示例中看到的那样,我编写了十进制值表示形式,并通过对掩码的十六进制表示进行bitwise and删除了前两个十六进制数字。

Furthermore you have to understand that the printing with cout in two different "modes" did not change the value at all and also not the internal representation. 此外,您还必须了解,使用cout在两种不同的“模式”下进行打印根本不会更改值,也不会更改内部表示。 With std::dec and std::hex you tell the ostream object how to create a string from an int representation. 使用std::decstd::hex您可以告诉ostream对象如何从int表示形式创建字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM