我需要使用整数调用byteSwap(),然后将其转换为十六进制并反转十六进制。例如。十六进制的byteSwap(2030)或7ee应返回ee7。这个程序还做了一些其他的事情,我想在byteSwap()中使用reverse()进行反向操作。我已经尝试在byteSwap()中使用了几个不同的循环,但似乎无法使十六进制值真正反转。
#include <iostream>
#include <array>
using namespace std;
void putInOrder(string *s1, string *s2);
void copy(int *f, int *t, int n);
void reverse(char *x, int n);
int byteSwap(int i);
int main() {
string a = "A";
string z = "Z";
auto *s1 = (string *) "A";
auto *s2 = (string *) "Z";
putInOrder(&z, &a);
int arr1 [3] = {3,4,5};
int arr2 [3] = {6,7,8};
int n;
cout << "Enter a number of values to be copied: ";
cin >> n;
copy(arr1,arr2,n);
char x = 0;
string name = "Computer";
reverse(&name[0],9);
byteSwap(2030);
return 0;
}
void putInOrder(string *s1, string *s2)
{
cout << s1 << " " << s2 << endl;
if(s2 > s1)
{
swap(s1,s2);
}
else if (s1 > s2)
{
cout << s1 << " " << s2 << endl;
}
cout << s1 << " " << s2 << endl;
}
void copy(int *f, int *t, int n)
{
for(int i = 0; i <= n + 2; ++i)
{
t[i+3] = f[i];
cout << t[i] << " ";
}
cout << "\n";
}
void reverse(char *x, int n)
{
for(int i = n - 1; i >=0; --i)
{
cout << x[i] << "";
}
cout << "\n";
}
int byteSwap(int i)
{
}发布于 2018-02-16 04:34:47
我建议使用std::bitset,因为它为这种数据操作提供了一些有用的方法。在这里,我采用二进制表示法,并每4位反转一次,以获得所需的输出(每8位反转一次,以获得ee07)。你也可以调整位集的大小来满足你的需要:
#include <iostream>
#include <bitset>
int main()
{
std::bitset<16> num(2030);
std::string numstr(num.to_string());
std::string numstrrev;
for (size_t i = 0, n = numstr.size(); i < n; i+=4)
numstrrev = numstr.substr(i, 4) + numstrrev;
std::bitset<16> numrev(numstrrev);
std::cout << std::hex << numrev.to_ulong() << std::endl;
return 0;
}https://stackoverflow.com/questions/48814853
复制相似问题