简体   繁体   English

在C ++中用前导零更改十六进制数的位,(C)

[英]Change bit of hex number with leading zeros in C++,(C)

I have this number in hex string: 我在十六进制字符串中有这个数字:

002A05.

I need to set 7-th bit of this number to 1, so after conversion I will get 我需要将此数字的第7位设置为1,所以转换后我将得到

022A05

But it has to work with every 6 chars hex number. 但是它必须与每6个字符的十六进制数一起使用。

I tried converting hex string to integer via strtol, but that function strip leading zeros. 我尝试通过strtol将十六进制字符串转换为整数,但是该功能删除了前导零。

Please help me how can I solve it. 请帮助我如何解决。

int hex=0x002A05;
int mask = 0x020000;
printf ("%06X",hex | mask);

hope this helps 希望这可以帮助

In a 24-bit number bit #7 (counting from the left, as you did in your example, not from the right, as is done conventionally) is always going to be in the second byte from the left. 在24位数字中,第7位(从左开始,如您在示例中那样,而不是从右开始,如通常那样)始终位于左数第二个字节中。 You can solve your problem without converting the entire number to integer by taking that second hex digit, converting it to a number 0..15, setting its bit #3 (again counting from the left), and converting the result back to a hex digit. 通过采用第二个十六进制数字,将其转换为数字0..15,将其位#3(再次从左开始计数)并将结果转换回十六进制,可以解决问题而无需将整数转换为整数。数字。

int fromHex(char c) {
    c = toupper(c);
    if (c >= '0' && c <= '9') {
        return c-'0';
    } else {
        return c-'A'+10;
    }
}
char toHexDigit(int n) {
    return n < 10 ? '0'+n : 'A'+n-10;
}

char myNum[] = "002A05";
myNum[1] = toHexDigit(fromHex(myNum[1]) | 2);
printf("%s\n", myNum);

This prints '022A05' ( link to ideone ). 这将打印“ 022A05”( 指向 ideone的链接 )。

It sounds to me like you have a string, not a hex constant, that you want to manipulate. 在我看来,您想要操作一个字符串,而不是十六进制常量。 You can do it pretty easily by bit twiddling the ascii value of the hex character. 您可以通过稍微旋转十六进制字符的ascii值来轻松完成此操作。 If you have char representing a hex character like char h = '6'; 如果您有代表十六进制字符的char h = '6';例如char h = '6'; , char h = 'C'; char h = 'C'; , or char h = ''; char h = ''; , you can set the 3rd from the left (2nd from the right) bit in the number that the character represents using: ,您可以使用以下命令将字符代表的数字从左数第3位(从右数第2位)设置:

 h = h > '7' ? h <= '9' ? h + 9 : ((h + 1) | 2) - 1 : h | 2;

So you can do this to the second character (4 + 3 bits) in your string. 因此,您可以对字符串中的第二个字符(4 + 3位)执行此操作。 This works for any hex string with 2 or more characters. 这适用于任何具有2个或更多字符的十六进制字符串。 Here is your example: 这是您的示例:

char hex_string[] = "002A05";

// Get the second character from the string
char h = hex_string[1];

// Calculate the new character
h = h > '7' ? h <= '9' ? h + 9 : ((h + 1) | 2) - 1 : h | 2;

// Set the second character in the string to the result
hex_string[1] = h;

printf("%s", hex_string); // 022A05

You asked about strtol specifically, so to answer your question, just add padding after you convert the number with strtol : 您是专门问strtol ,所以要回答您的问题,只需在将数字转换为strtol后添加padding即可:

const char *s = "002A05";
int x = strtol(s, NULL, 16);
x |= (1<<17);
printf("%.6X\n",x);

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

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