C++-十六进制反转位
C++- Reverse bits in hex
我希望能够反转存储在内存地址(十六进制)中的位。例如,
0xABCD -> 0xDCBA
我在网上查看了其他解决方案,它们都涉及复杂的位操作,我想看看是否有一种方法可以使用字符串表示来实现。
所以我想将 0xABCD 转换为“0xABCD”。反转此字符串(我知道该怎么做),然后将“0xDCBA”转换为 0xDCBA。
完全是 C++ 的新手,所以我有点迷茫,其他类似的问题也没有真正帮助。
So I want to convert 0xABCD to "0xABCD". Reverse this string (which I know how to do) and then convert "0xDCBA" to 0xDCBA.
并不难。您可以使用 sprintf
将其保存为字符串,并使用 sscanf
从字符串中读取。
#include <iostream>
#include <cstdio>
#include <cstring>
int main()
{
char str[20];
int i = 0xABCD;
// Write the number to the string
sprintf(str, "0x%X", i);
std::cout << str << std::endl;
// Reverse string.
// Pretend that I did.
strcpy(str, "0xDCBA");
// Read the number from the string.
sscanf(str, "0x%X", &i);
// Make sure you get the expected output.
std::cout << std::hex << i << std::endl;
}
输出:
0xABCD
dcba
您可以从格式中删除“0x”,以便更容易反转字符串。
sprintf(str, "%X", i);
和
sscanf(str, "%X", &i);
您要反转位(二进制数字)还是反转十六进制数字?你自相矛盾。
0xABCD 是二进制 1010101111001101,因此将位反转为 1011001111010101,即 0xB3D5。
一般来说,要反转数字(在任何基数中),您需要提取数字、反转它们,然后重新打包。计算机在内部使用二进制,因此反转任何 2 的幂基数中的数字都相对容易 - 您只需移动并屏蔽它们以提取和移动以及按位 - 或者它们重新组合。
unsigned reverse16binary(unsigned val) {
/* reverse bits in a 16-bit binary number */
unsigned rv = 0;
for (int bit = 0; bit < 16; bit++) {
int digit = (val >> bit) & 1; // extract a digit
rv |= digit << (15 - bit); // stick it in the result
}
return rv;
}
unsigned reverse16hex(unsigned val) {
/* reverse hex digits in a 16-bit binary number */
unsigned rv = 0;
for (int bit = 0; bit < 16; bit += 4) {
int digit = (val >> bit) & 0xf; // extract a digit
rv |= digit << (12 - bit); // stick it in the result
}
return rv;
}
unsigned reverse_general(unsigned val, int bits, int base) {
/* reverse base 2**"base" digits in a "bits"-bit binary number
bits must be <= sizeof(unsigned) * CHAR_BIT and a multiple of base
base must be < sizeof(unsigned) * CHAR_BIT */
unsigned rv = 0;
for (int bit = 0; bit < bits; bit += base) {
int digit = (val >> bit) & ((1 << base) - 1); // extract a digit
rv |= digit << (bits - base - bit); // stick it in the result
}
return rv;
}
我希望能够反转存储在内存地址(十六进制)中的位。例如, 0xABCD -> 0xDCBA
我在网上查看了其他解决方案,它们都涉及复杂的位操作,我想看看是否有一种方法可以使用字符串表示来实现。
所以我想将 0xABCD 转换为“0xABCD”。反转此字符串(我知道该怎么做),然后将“0xDCBA”转换为 0xDCBA。
完全是 C++ 的新手,所以我有点迷茫,其他类似的问题也没有真正帮助。
So I want to convert 0xABCD to "0xABCD". Reverse this string (which I know how to do) and then convert "0xDCBA" to 0xDCBA.
并不难。您可以使用 sprintf
将其保存为字符串,并使用 sscanf
从字符串中读取。
#include <iostream>
#include <cstdio>
#include <cstring>
int main()
{
char str[20];
int i = 0xABCD;
// Write the number to the string
sprintf(str, "0x%X", i);
std::cout << str << std::endl;
// Reverse string.
// Pretend that I did.
strcpy(str, "0xDCBA");
// Read the number from the string.
sscanf(str, "0x%X", &i);
// Make sure you get the expected output.
std::cout << std::hex << i << std::endl;
}
输出:
0xABCD
dcba
您可以从格式中删除“0x”,以便更容易反转字符串。
sprintf(str, "%X", i);
和
sscanf(str, "%X", &i);
您要反转位(二进制数字)还是反转十六进制数字?你自相矛盾。
0xABCD 是二进制 1010101111001101,因此将位反转为 1011001111010101,即 0xB3D5。
一般来说,要反转数字(在任何基数中),您需要提取数字、反转它们,然后重新打包。计算机在内部使用二进制,因此反转任何 2 的幂基数中的数字都相对容易 - 您只需移动并屏蔽它们以提取和移动以及按位 - 或者它们重新组合。
unsigned reverse16binary(unsigned val) {
/* reverse bits in a 16-bit binary number */
unsigned rv = 0;
for (int bit = 0; bit < 16; bit++) {
int digit = (val >> bit) & 1; // extract a digit
rv |= digit << (15 - bit); // stick it in the result
}
return rv;
}
unsigned reverse16hex(unsigned val) {
/* reverse hex digits in a 16-bit binary number */
unsigned rv = 0;
for (int bit = 0; bit < 16; bit += 4) {
int digit = (val >> bit) & 0xf; // extract a digit
rv |= digit << (12 - bit); // stick it in the result
}
return rv;
}
unsigned reverse_general(unsigned val, int bits, int base) {
/* reverse base 2**"base" digits in a "bits"-bit binary number
bits must be <= sizeof(unsigned) * CHAR_BIT and a multiple of base
base must be < sizeof(unsigned) * CHAR_BIT */
unsigned rv = 0;
for (int bit = 0; bit < bits; bit += base) {
int digit = (val >> bit) & ((1 << base) - 1); // extract a digit
rv |= digit << (bits - base - bit); // stick it in the result
}
return rv;
}