C++ 先将MSB转换为LSB
我想将无符号短值从MSB first转换为LSB first。执行了以下代码,但它不工作。有人能指出我所做的错误吗C++ 先将MSB转换为LSB,c++,c,visual-c++,C++,C,Visual C++,我想将无符号短值从MSB first转换为LSB first。执行了以下代码,但它不工作。有人能指出我所做的错误吗 #include <iostream> using namespace std; int main() { unsigned short value = 0x000A; char *m_pCurrent = (char *)&value; short temp; temp = *(m_pCurrent+1); temp = (t
#include <iostream>
using namespace std;
int main()
{
unsigned short value = 0x000A;
char *m_pCurrent = (char *)&value;
short temp;
temp = *(m_pCurrent+1);
temp = (temp << 8) | *(unsigned char *)m_pCurrent;
m_pCurrent += sizeof(short);
cout << "temp " << temp << endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
无符号短值=0x000A;
char*m_pCurrent=(char*)和value;
短期温度;
温度=*(m_p电流+1);
temp=(temp这里有一个简单但缓慢的实现:
#include <cstdint>
const size_t USHORT_BIT = CHAR_BIT * sizeof(unsigned short);
unsigned short ConvertMsbFirstToLsbFirst(const unsigned short input) {
unsigned short output = 0;
for (size_t offset = 0; offset < USHORT_BIT; ++offset) {
output |= ((input >> offset) & 1) << (USHORT_BIT - 1 - offset);
}
return output;
}
#包括
const size\u t USHORT\u BIT=CHAR\u BIT*sizeof(无符号短字符);
无符号短转换器msbFirstTolsbFirst(常量无符号短输入){
无符号短输出=0;
用于(大小偏移=0;偏移 输出|=((输入>>偏移量)&1)下面是一个简单但缓慢的实现:
#include <cstdint>
const size_t USHORT_BIT = CHAR_BIT * sizeof(unsigned short);
unsigned short ConvertMsbFirstToLsbFirst(const unsigned short input) {
unsigned short output = 0;
for (size_t offset = 0; offset < USHORT_BIT; ++offset) {
output |= ((input >> offset) & 1) << (USHORT_BIT - 1 - offset);
}
return output;
}
#包括
const size\u t USHORT\u BIT=CHAR\u BIT*sizeof(无符号短字符);
无符号短转换器msbFirstTolsbFirst(常量无符号短输入){
无符号短输出=0;
用于(大小偏移=0;偏移 输出|=((输入>>偏移量)&1)下面是一个简单但缓慢的实现:
#include <cstdint>
const size_t USHORT_BIT = CHAR_BIT * sizeof(unsigned short);
unsigned short ConvertMsbFirstToLsbFirst(const unsigned short input) {
unsigned short output = 0;
for (size_t offset = 0; offset < USHORT_BIT; ++offset) {
output |= ((input >> offset) & 1) << (USHORT_BIT - 1 - offset);
}
return output;
}
#包括
const size\u t USHORT\u BIT=CHAR\u BIT*sizeof(无符号短字符);
无符号短转换器msbFirstTolsbFirst(常量无符号短输入){
无符号短输出=0;
用于(大小偏移=0;偏移 输出|=((输入>>偏移量)&1)下面是一个简单但缓慢的实现:
#include <cstdint>
const size_t USHORT_BIT = CHAR_BIT * sizeof(unsigned short);
unsigned short ConvertMsbFirstToLsbFirst(const unsigned short input) {
unsigned short output = 0;
for (size_t offset = 0; offset < USHORT_BIT; ++offset) {
output |= ((input >> offset) & 1) << (USHORT_BIT - 1 - offset);
}
return output;
}
#包括
const size\u t USHORT\u BIT=CHAR\u BIT*sizeof(无符号短字符);
无符号短转换器msbFirstTolsbFirst(常量无符号短输入){
无符号短输出=0;
用于(大小偏移=0;偏移 输出|=((输入>>偏移量)&1)错误的是,您首先将值
的MSB分配给温度
的LSB,然后将其再次移动到MSB并将值
的LSB分配给LSB。基本上,您交换了*(m_pCurrent+1)
和*m_pCurrent
简化代码:
#include <iostream>
using namespace std;
int main()
{
unsigned short value = 0x00FF;
short temp = ((char*) &value)[0]; // assign value's LSB
temp = (temp << 8) | ((char*) &value)[1]; // shift LSB to MSB and add value's MSB
cout << "temp " << temp << endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
无符号短值=0x00FF;
short temp=((char*)&value)[0];//赋值的LSB
temp=(temp错误的是,您首先将值的MSB分配给temp
的LSB,然后将其再次移动到MSB,并将值的LSB分配给LSB。基本上,您交换了*(m_pccurrent+1)
和*m_pccurrent
,所以整个事情都没有效果
简化代码:
#include <iostream>
using namespace std;
int main()
{
unsigned short value = 0x00FF;
short temp = ((char*) &value)[0]; // assign value's LSB
temp = (temp << 8) | ((char*) &value)[1]; // shift LSB to MSB and add value's MSB
cout << "temp " << temp << endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
无符号短值=0x00FF;
short temp=((char*)&value)[0];//赋值的LSB
temp=(temp错误的是,您首先将值的MSB分配给temp
的LSB,然后将其再次移动到MSB,并将值的LSB分配给LSB。基本上,您交换了*(m_pccurrent+1)
和*m_pccurrent
,所以整个事情都没有效果
简化代码:
#include <iostream>
using namespace std;
int main()
{
unsigned short value = 0x00FF;
short temp = ((char*) &value)[0]; // assign value's LSB
temp = (temp << 8) | ((char*) &value)[1]; // shift LSB to MSB and add value's MSB
cout << "temp " << temp << endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
无符号短值=0x00FF;
short temp=((char*)&value)[0];//赋值的LSB
temp=(temp错误的是,您首先将值的MSB分配给temp
的LSB,然后将其再次移动到MSB,并将值的LSB分配给LSB。基本上,您交换了*(m_pccurrent+1)
和*m_pccurrent
,所以整个事情都没有效果
简化代码:
#include <iostream>
using namespace std;
int main()
{
unsigned short value = 0x00FF;
short temp = ((char*) &value)[0]; // assign value's LSB
temp = (temp << 8) | ((char*) &value)[1]; // shift LSB to MSB and add value's MSB
cout << "temp " << temp << endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
无符号短值=0x00FF;
short temp=((char*)&value)[0];//赋值的LSB
temp=(temp的实际意图是将其保留在其他函数中,很抱歉,bat编码样式“它不工作”不是对问题的正确描述。它可以表示“它由于错误XYZ而无法编译”到“它正在计算pi的前100位,而它本应执行其他操作”。你是指最高有效位还是字节?你真正想做什么?我的意思是,你想在不同的处理器之间传输二进制数据,还是只是一个练习?B
指的是位还是字节?代码看起来像B
是指字节,但它“不起作用”。请澄清。实际目的是将其保留在其他函数中,抱歉,bat编码样式“其不工作”不是对问题的正确描述。它可以表示“由于错误XYZ而无法编译”到“它正在计算pi的前100位,而它本应做其他事情”。你是指最高有效位还是字节?你真正想做什么?我的意思是,你想在不同的处理器之间传输二进制数据,还是只是一个练习?B
指的是位还是字节?代码看起来像B
是指字节,但它“不起作用”。请澄清。实际目的是将其保留在其他函数中,抱歉,bat编码样式“其不工作”不是对问题的正确描述。它可以表示“由于错误XYZ而无法编译”到“它正在计算pi的前100位,而它本应做其他事情”。你是指最高有效位还是字节?你真正想做什么?我的意思是,你想在不同的处理器之间传输二进制数据,还是只是一个练习?B
指的是位还是字节?代码看起来像B
是指字节,但它“不起作用”。请澄清。实际目的是将其保留在其他函数中,抱歉,bat编码样式“其不工作”不是对问题的正确描述。它可以表示“由于错误XYZ而无法编译”到“它正在计算pi的前100位,而它本应做其他事情”.你是指最高有效位还是字节?你真正想做什么?我是说,你想在不同的处理器之间传输二进制数据,还是只是一个练习?是B