c_cpp 大/小端的字节数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 大/小端的字节数相关的知识,希望对你有一定的参考价值。


typedef unsigned int size_t;

typedef unsigned char       uint8;
typedef unsigned short      uint16;
typedef unsigned int        uint32;
typedef unsigned long long  uint64;

inline void Set8(void* memory, size_t offset, uint8 v) {
	static_cast<uint8*>(memory)[offset] = v;
}

inline uint8 Get8(const void* memory, size_t offset) {
	return static_cast<const uint8*>(memory)[offset];
}

inline void SetBE16(void* memory, uint16 v) {
	Set8(memory, 0, static_cast<uint8>(v >> 8));
	Set8(memory, 1, static_cast<uint8>(v >> 0));
}

inline void SetBE32(void* memory, uint32 v) {
	Set8(memory, 0, static_cast<uint8>(v >> 24));
	Set8(memory, 1, static_cast<uint8>(v >> 16));
	Set8(memory, 2, static_cast<uint8>(v >> 8));
	Set8(memory, 3, static_cast<uint8>(v >> 0));
}

inline void SetBE64(void* memory, uint64 v) {
	Set8(memory, 0, static_cast<uint8>(v >> 56);
	Set8(memory, 1, static_cast<uint8>(v >> 48);
	Set8(memory, 2, static_cast<uint8>(v >> 40);
	Set8(memory, 3, static_cast<uint8>(v >> 32);
	Set8(memory, 4, static_cast<uint8>(v >> 24);
	Set8(memory, 5, static_cast<uint8>(v >> 16);
	Set8(memory, 6, static_cast<uint8>(v >> 8);
	Set8(memory, 7, static_cast<uint8>(v >> 0);
}

inline uint16 GetBE16(const void* memory) {
	return static_cast<uint16>((Get8(memory, 0) << 8) | (Get8(memory, 1) << 0));
}

inline uint32 GetBE32(const void* memory) {
	return (static_cast<uint32>(Get8(memory, 0)) << 24) |
		(static_cast<uint32>(Get8(memory, 1)) << 16) |
        (static_cast<uint32>(Get8(memory, 2)) << 8) |
        (static_cast<uint32>(Get8(memory, 3)) << 0);
}

inline uint64 GetBE64(const void* memory) {
	return (static_cast<uint64>(Get8(memory, 0)) << 56) |
		(static_cast<uint64>(Get8(memory, 1)) << 48) |
		(static_cast<uint64>(Get8(memory, 2)) << 40) |
		(static_cast<uint64>(Get8(memory, 3)) << 32) |
		(static_cast<uint64>(Get8(memory, 4)) << 24) |
		(static_cast<uint64>(Get8(memory, 5)) << 16) |
		(static_cast<uint64>(Get8(memory, 6)) << 8) |
		(static_cast<uint64>(Get8(memory, 7)) << 0);
}

inline void SetLE16(void* memory, uint16 v) {
	Set8(memory, 0, static_cast<uint8>(v >> 0));
	Set8(memory, 1, static_cast<uint8>(v >> 8));
}

inline void SetLE32(void* memory, uint32 v) {
	Set8(memory, 0, static_cast<uint8>(v >> 0));
	Set8(memory, 1, static_cast<uint8>(v >> 8));
	Set8(memory, 2, static_cast<uint8>(v >> 16));
	Set8(memory, 3, static_cast<uint8>(v >> 24));
}

inline void SetLE64(void* memory, uint64 v) {
	Set8(memory, 0, static_cast<uint8>(v >> 0));
	Set8(memory, 1, static_cast<uint8>(v >> 8));
	Set8(memory, 2, static_cast<uint8>(v >> 16));
	Set8(memory, 3, static_cast<uint8>(v >> 24));
	Set8(memory, 4, static_cast<uint8>(v >> 32));
	Set8(memory, 5, static_cast<uint8>(v >> 40));
	Set8(memory, 6, static_cast<uint8>(v >> 48));
	Set8(memory, 7, static_cast<uint8>(v >> 56));
}

inline uint16 GetLE16(const void* memory) {
	return static_cast<uint16>((Get8(memory, 0) << 0) | (Get8(memory, 1) << 8));
}

inline uint32 GetLE32(const void* memory) {
	return (static_cast<uint32>(Get8(memory, 0)) << 0) |
		(static_cast<uint32>(Get8(memory, 1)) << 8) |
		(static_cast<uint32>(Get8(memory, 2)) << 16) |
		(static_cast<uint32>(Get8(memory, 3)) << 24);
}

inline uint64 GetLE64(const void* memory) {
	return (static_cast<uint64>(Get8(memory, 0)) << 0) |
		(static_cast<uint64>(Get8(memory, 1)) << 8) |
		(static_cast<uint64>(Get8(memory, 2)) << 16) |
		(static_cast<uint64>(Get8(memory, 3)) << 24) |
		(static_cast<uint64>(Get8(memory, 4)) << 32) |
		(static_cast<uint64>(Get8(memory, 5)) << 40) |
		(static_cast<uint64>(Get8(memory, 6)) << 48) |
		(static_cast<uint64>(Get8(memory, 7)) << 56);
}

inline bool IsBigEndian() {
	static const int number = 1;
	return 0 == *reinterpret_cast<const char*>(&number);
}

以上是关于c_cpp 大/小端的字节数的主要内容,如果未能解决你的问题,请参考以下文章

大端小端的概念

大端/小端的存储模式

对于字节顺序——大端与小端的理解

写程序判断系统是大端序还是小端序

Mysql查询优化小结

字节序