Qt、C++中,大小端变量类型转换

Qt、C++中,大小端变量类型转换

码农世界 2024-06-17 后端 93 次浏览 0个评论

#define isEndianTyped true //ture为大端;false为小端

1、quint32转QbyteArray

QByteArray byteToBytes::quint32ToQByteArray(quint32 input)
{
	QByteArray output;
	if (isEndianTyped)
	{
		output[3] = (uchar)(0x000000ff & input);
		output[2] = (uchar)((0x0000ff00 & input) >> 8);
		output[1] = (uchar)((0x00ff0000 & input) >> 16);
		output[0] = (uchar)((0xff000000 & input) >> 24);
	}
	else
	{
		output[0] = (uchar)(0x000000ff & input);
		output[1] = (uchar)((0x0000ff00 & input) >> 8);
		output[2] = (uchar)((0x00ff0000 & input) >> 16);
		output[3] = (uchar)((0xff000000 & input) >> 24);
	}
	return output;
}

2、QbyteArray 转quint32

quint32 byteToBytes::QByteArrayToquint32(QByteArray input)
{
	qint64 output;
	if (isEndianTyped)
	{
		output = input[3] & 0x000000FF;
		output |= (input[2] << 8) & 0x0000FF00;
		output |= (input[1] << 16) & 0x00FF0000;
		output |= (input[0] << 24) & 0xFF000000;
	}
	else
	{
		output = input[0] & 0x000000FF;
		output |= (input[1] << 8) & 0x0000FF00;
		output |= (input[2] << 16) & 0x00FF0000;
		output |= (input[3] << 24) & 0xFF000000;
	}
	return output;
}

3、qint32转QbyteArray

QByteArray byteToBytes::qint32ToQByteArray(qint32 input)
{
	QByteArray output;
	if (isEndianTyped)
	{
		output[3] = (uchar)(0x000000ff & input);
		output[2] = (uchar)((0x0000ff00 & input) >> 8);
		output[1] = (uchar)((0x00ff0000 & input) >> 16);
		output[0] = (uchar)((0xff000000 & input) >> 24);
	}
	else
	{
		output[0] = (uchar)(0x000000ff & input);
		output[1] = (uchar)((0x0000ff00 & input) >> 8);
		output[2] = (uchar)((0x00ff0000 & input) >> 16);
		output[3] = (uchar)((0xff000000 & input) >> 24);
	}
	return output;
}

4、QbyteArray转qint32

qint32 byteToBytes::QByteArrayToqint32(QByteArray input)
{
	qint64 output;
	if (isEndianTyped)
	{
		output = input[3] & 0x000000FF;
		output |= (input[2] << 8) & 0x0000FF00;
		output |= (input[1] << 16) & 0x00FF0000;
		output |= (input[0] << 24) & 0xFF000000;
	}
	else
	{
		output = input[0] & 0x000000FF;
		output |= (input[1] << 8) & 0x0000FF00;
		output |= (input[2] << 16) & 0x00FF0000;
		output |= (input[3] << 24) & 0xFF000000;
	}
	return output;
}

5、quint16转QbyteArray

QByteArray byteToBytes::quint16ToQByteArray(quint16 input)
{
	QByteArray output;
	if (isEndianTyped)
	{
		output[3] = (uchar)(0x000000ff & input);
		output[2] = (uchar)((0x0000ff00 & input) >> 8);
	}
	else
	{
		output[0] = (uchar)(0x000000ff & input);
		output[1] = (uchar)((0x0000ff00 & input) >> 8);
	}
	return output;
}

6、QbyteArray转quint16

quint16 byteToBytes::QByteArrayToquint16(QByteArray input)
{
	qint64 output;
	if (isEndianTyped)
	{
		output = input[3] & 0x000000FF;
		output |= (input[2] << 8) & 0x0000FF00;
	}
	else
	{
		output = input[0] & 0x000000FF;
		output |= (input[1] << 8) & 0x0000FF00;
	}
	return output;
}

7、qint16转QbyteArray

QByteArray byteToBytes::qint16ToQByteArray(qint16 input)
{
	QByteArray output;
	if (isEndianTyped)
	{
		output[3] = (uchar)(0x000000ff & input);
		output[2] = (uchar)((0x0000ff00 & input) >> 8);
	}
	else
	{
		output[0] = (uchar)(0x000000ff & input);
		output[1] = (uchar)((0x0000ff00 & input) >> 8);
	}
	return output;
}

8、QbyteArray转qint16

qint16 byteToBytes::QByteArrayToqint16(QByteArray input)
{
	qint64 output;
	if (isEndianTyped)
	{
		output = input[3] & 0x000000FF;
		output |= (input[2] << 8) & 0x0000FF00;
	}
	else
	{
		output = input[0] & 0x000000FF;
		output |= (input[1] << 8) & 0x0000FF00;
	}
	return output;
}

9、QbyteArray转quint64

QByteArray byteToBytes::quint64ToQByteArray(quint64 input)
{
	QByteArray output;
	if (isEndianTyped)
	{
		QByteArray _output;
		_output.resize(sizeof(input));
		memcpy(_output.data(), &input, sizeof(input));
		for (int i = 0; i < _output.size(); i++)
		{
			output[i] = _output[_output.size() - i - 1];
		}
	}
	else
	{
		output.resize(sizeof(input));
		memcpy(output.data(), &input, sizeof(input));
	}
	return output;
}

10、quint64转QbyteArray

quint64 byteToBytes::QByteArrayToquint64(QByteArray input)
{
	qint64 output;
	if (isEndianTyped)
	{
		QByteArray _input;
		for (int i = 0; i < input.size(); i++)
		{
			_input[i] = input[input.size() - i - 1];
		}
		memcpy(&output, _input, sizeof(output));
	}
	else
	{
		memcpy(&output, input, sizeof(output));
	}
	return output;
}

11、QbyteArray转qint64

QByteArray byteToBytes::qint64ToQByteArray(qint64 input)
{
	QByteArray output;
	if (isEndianTyped)
	{
		QByteArray _output;
		_output.resize(sizeof(input));
		memcpy(_output.data(), &input, sizeof(input));
		for (int i = 0; i < _output.size(); i++)
		{
			output[i] = _output[_output.size() - i - 1];
		}
	}
	else
	{
		output.resize(sizeof(input));
		memcpy(output.data(), &input, sizeof(input));
	}
	return output;
}

12、qint64转QbyteArray

qint64 byteToBytes::QByteArrayToqint64(QByteArray input)
{
	qint64 output;
	if (isEndianTyped)
	{
		QByteArray _input;
		for (int i = 0; i < input.size(); i++)
		{
			_input[i] = input[input.size() - i - 1];
		}
		memcpy(&output, _input, sizeof(output));
	}
	else
	{
		memcpy(&output, input, sizeof(output));
	}
	return output;
}

13、QbyteArray转int

int byteToBytes::QByteArrayToint(QByteArray input)
{
	int output;
	if (isEndianTyped)
	{
		output = input[3] & 0x000000FF;
		output |= (input[2] << 8) & 0x0000FF00;
		output |= (input[1] << 16) & 0x00FF0000;
		output |= (input[0] << 24) & 0xFF000000;
	}
	else
	{
		output = input[0] & 0x000000FF;
		output |= (input[1] << 8) & 0x0000FF00;
		output |= (input[2] << 16) & 0x00FF0000;
		output |= (input[3] << 24) & 0xFF000000;
	}
	return output;
}

14、int转QbyteArray

QByteArray byteToBytes::intToQByteArray(int input)
{
	QByteArray output;
	if (isEndianTyped)
	{
		output[3] = (uchar)(0x000000ff & input);
		output[2] = (uchar)((0x0000ff00 & input) >> 8);
		output[1] = (uchar)((0x00ff0000 & input) >> 16);
		output[0] = (uchar)((0xff000000 & input) >> 24);
	}
	else
	{
		output[0] = (uchar)(0x000000ff & input);
		output[1] = (uchar)((0x0000ff00 & input) >> 8);
		output[2] = (uchar)((0x00ff0000 & input) >> 16);
		output[3] = (uchar)((0xff000000 & input) >> 24);
	}
	return output;
}

15、QbyteArray转uint

uint byteToBytes::QByteArrayTouint(QByteArray input)
{
	uint output;
	if (isEndianTyped)
	{
		output = input[3] & 0x000000FF;
		output |= (input[2] << 8) & 0x0000FF00;
		output |= (input[1] << 16) & 0x00FF0000;
		output |= (input[0] << 24) & 0xFF000000;
	}
	else
	{
		output = input[0] & 0x000000FF;
		output |= (input[1] << 8) & 0x0000FF00;
		output |= (input[2] << 16) & 0x00FF0000;
		output |= (input[3] << 24) & 0xFF000000;
	}
	return output;
}

16、uint转QbyteArray

QByteArray byteToBytes::uintToQByteArray(uint input)
{
	QByteArray output;
	if (isEndianTyped)
	{
		output[3] = (uchar)(0x000000ff & input);
		output[2] = (uchar)((0x0000ff00 & input) >> 8);
		output[1] = (uchar)((0x00ff0000 & input) >> 16);
		output[0] = (uchar)((0xff000000 & input) >> 24);
	}
	else
	{
		output[0] = (uchar)(0x000000ff & input);
		output[1] = (uchar)((0x0000ff00 & input) >> 8);
		output[2] = (uchar)((0x00ff0000 & input) >> 16);
		output[3] = (uchar)((0xff000000 & input) >> 24);
	}
	return output;
}

17、QbyteArray转short

short byteToBytes::QByteArrayToshort(QByteArray input)
{
	qint64 output;
	if (isEndianTyped)
	{
		output = input[3] & 0x000000FF;
		output |= (input[2] << 8) & 0x0000FF00;
	}
	else
	{
		output = input[0] & 0x000000FF;
		output |= (input[1] << 8) & 0x0000FF00;
	}
	return output;
}

18、short转QbyteArray

QByteArray byteToBytes::shortToQByteArray(short input)
{
	QByteArray output;
	if (isEndianTyped)
	{
		output[3] = (uchar)(0x000000ff & input);
		output[2] = (uchar)((0x0000ff00 & input) >> 8);
	}
	else
	{
		output[0] = (uchar)(0x000000ff & input);
		output[1] = (uchar)((0x0000ff00 & input) >> 8);
	}
	return output;
}

19、QbyteArray转ushort

ushort byteToBytes::QByteArrayToushort(QByteArray input)
{
	qint64 output;
	if (isEndianTyped)
	{
		output = input[3] & 0x000000FF;
		output |= (input[2] << 8) & 0x0000FF00;
	}
	else
	{
		output = input[0] & 0x000000FF;
		output |= (input[1] << 8) & 0x0000FF00;
	}
	return output;
}

20、ushort转QbyteArray

QByteArray byteToBytes::ushortToQByteArray(ushort input)
{
	QByteArray output;
	if (isEndianTyped)
	{
		output[3] = (uchar)(0x000000ff & input);
		output[2] = (uchar)((0x0000ff00 & input) >> 8);
	}
	else
	{
		output[0] = (uchar)(0x000000ff & input);
		output[1] = (uchar)((0x0000ff00 & input) >> 8);
	}
	return output;
}

21、QbyteArray转float

float byteToBytes::QByteArrayTofloat(QByteArray input)
{
	float output;
	if (isEndianTyped)
	{
		QByteArray _input;
		for (int i = 0; i < input.size(); i++)
		{
			_input[i] = input[input.size() - i - 1];
		}
		memcpy(&output, _input, sizeof(output));
	}
	else
	{
		memcpy(&output, input, sizeof(output));
	}
	return output;
}

22、float转QbyteArray

QByteArray byteToBytes::floatToQByteArray(float input)
{
	QByteArray output;
	if (isEndianTyped)
	{
		QByteArray _output;
		_output.resize(sizeof(input));
		memcpy(_output.data(), &input, sizeof(input));
		for (int i = 0; i < _output.size(); i++)
		{
			output[i] = _output[_output.size() - i - 1];
		}
	}
	else
	{
		output.resize(sizeof(input));
		memcpy(output.data(), &input, sizeof(input));
	}
	return output;
}

23、QbyteArray转double

double byteToBytes::QByteArrayTodouble(QByteArray input)
{
	double output;
	if (isEndianTyped)
	{
		QByteArray _input;
		for (int i = 0; i < input.size(); i++)
		{
			_input[i] = input[input.size() - i - 1];
		}
		memcpy(&output, _input, sizeof(output));
	}
	else
	{
		memcpy(&output, input, sizeof(output));
	}
	return output;
}

24、double转QbyteArray

QByteArray byteToBytes::doubleToQByteArray(double input)
{
	QByteArray output;
	if (isEndianTyped)
	{
		QByteArray _output;
		_output.resize(sizeof(input));
		memcpy(_output.data(), &input, sizeof(input));
		for (int i = 0; i < _output.size(); i++)
		{
			output[i] = _output[_output.size() - i - 1];
		}
	}
	else
	{
		output.resize(sizeof(input));
		memcpy(output.data(), &input, sizeof(input));
	}
	return output;
}

版权声明:本文为博主原创文章,转载请附上博文链接!

转载请注明来自码农世界,本文标题:《Qt、C++中,大小端变量类型转换》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,93人围观)参与讨论

还没有评论,来说两句吧...

Top