C语言实现基本数据类型与字节类型互相转换
在网络编程中,一般通过数据包的方式来传递数据,数据包内存储的是各种数据的表示形式,例如浮点数使用IEEE754标准存储,字符使用字符码存储(例如使用ASCII码)。本篇文章主要讲述了基本数据类型与这些存储格式的相互转换。
数据包如何在实际中使用可以参考->C#数据交互服务器(一)
定义unsigned char Byte为字节类型,使用小端模式存储(如操作系统使用大端这里还是转换成小端方式),基本数据类型大小采用64位标准。
BitConvert.h
#pragma once
#ifndef Byte
#include
typedef unsigned char Byte; //定义字节类型
typedef long long Long; //定义长整型64位
Byte* Long2Bytes(Long data);
Byte* Int2Bytes(int data);
Byte* Short2Bytes(short data);
Byte* Bool2Bytes(bool data);
Byte* String2Bytes(const char* data, const char* encoding);
Byte* Float2Bytes(float data);
Byte* Double2Bytes(double data);
Long ToInt64(Byte* bytes, unsigned int origin);
int ToInt32(Byte* bytes, unsigned int origin);
short ToInt16(Byte* bytes, unsigned int origin);
bool ToBool(Byte* bytes, unsigned int origin);
char* ToString(Byte* bytes, unsigned int origin, unsigned int len, const char* encoding);
float ToFloat(Byte* bytes, unsigned int origin);
double ToDouble(Byte* bytes, unsigned int origin);
void Reversal(Byte* bytes, unsigned int origin, unsigned int len); //存储方式转换(操作系统为大端模式时自动转换)
bool IsBig_Endian(); //是否为大端模式
#endif
BitConvert.c
#pragma once
/*
靠左高位,靠右低位
大端模式和小端模式,例如有short=0x1234,大端模式存储为12 34,小端模式存储为34 12;大端模式更符合人脑思维,小端模式更适合计算机
此处使用小端模式存储数据,Byte范围为0~255,满255进1,例如有int=1234,则byte[0]=214,byte[1]=4
注意:此处没有做溢出检测,无论操作系统使用小端还是大端,这里都将使用小端模式存储
*/
#include "BitConvert.h"
#include
#include
#include
Byte * Long2Bytes(Long data)
{
Byte* bytes = malloc(8);
bytes[7] = data >> 56;
bytes[6] = data >> 48;
bytes[5] = data >> 40;
bytes[4] = data >> 32;
bytes[3] = data >> 24;
bytes[2] = data >> 16;
bytes[1] = data >> 8;
bytes[0] = data >> 0;
if (IsBig_Endian())
{
Reversal(bytes, 0, 8);
}
return bytes;
}
Byte * Int2Bytes(int data)
{
Byte* bytes = malloc(4);
bytes[3] = data >> 24;
bytes[2] = data >> 16;
bytes[1] = data >> 8;
bytes[0] = data >> 0;
if (IsBig_Endian())
{
Reversal(bytes, 0, 4);
}
return bytes;
}
Byte * Short2Bytes(short data)
{
Byte* bytes = malloc(2);
bytes[1] = data >> 8;
bytes[0] = data >> 0;
if (IsBig_Endian())
{
Reversal(bytes, 0, 2);
}
return bytes;
}
Byte * Bool2Bytes(bool data)
{
Byte* bytes = malloc(1);
if (data == true)
{
bytes[0] = 1;
}
else
{
bytes[0] = 0;
}
return bytes;
}
Byte * String2Bytes(const char * data,const char * encoding)
{
size_t len = strlen(data);
Byte* bytes = malloc(len);
for (size_t i = 0; i < len; i++)
{
bytes[i] = (int)data[i]; //字符转字符码
}
return bytes;
}
Byte * Float2Bytes(float data)
{
Int2Bytes(*(int*)&data); //(int*)&data为指向data的地址的int指针,前方加*表示该地址存储的内容,即IEE754标准格式数据,此处强制转换部分精度将丢失
}
Byte * Double2Bytes(double data)
{
Long2Bytes(*(long*)&data);
}
long long ToInt64(Byte * bytes, unsigned int origin)
{
if (IsBig_Endian())
{
Reversal(bytes, origin, 8);
}
Long data = ((bytes[origin + 7] & 0xFF) << 56)
| ((bytes[origin + 6] & 0xFF) << 48)
| ((bytes[origin + 5] & 0xFF) << 40)
| ((bytes[origin + 4] & 0xFF) << 32)
| ((bytes[origin + 3] & 0xFF) << 24)
| ((bytes[origin + 2] & 0xFF) << 16)
| ((bytes[origin + 1] & 0xFF) << 8)
| ((bytes[origin + 0] & 0xFF) << 0);
return data;
}
int ToInt32(Byte* bytes, unsigned int origin)
{
if (IsBig_Endian())
{
Reversal(bytes, origin, 4);
}
int data = ((bytes[origin + 3] & 0xFF) << 24)
| ((bytes[origin + 2] & 0xFF) << 16)
| ((bytes[origin + 1] & 0xFF) << 8)
| ((bytes[origin + 0] & 0xFF) << 0);
return data;
}
short ToInt16(Byte * bytes, unsigned int origin)
{
if (IsBig_Endian())
{
Reversal(bytes, origin, 2);
}
short data = ((bytes[origin + 0] & 0xFF) << 0)
| ((bytes[origin + 1] & 0xFF) << 8);
return data;
}
bool ToBool(Byte * bytes, unsigned int origin)
{
bool data;
if (bytes[origin] == 1)
{
data = true;
}
else
{
data = false;
}
return data;
}
char * ToString(Byte * bytes, unsigned int origin, unsigned int len, const char * encoding)
{
char* data = malloc(len);
for (size_t i = 0; i < len; i++)
{
data[i] = (char)bytes[origin+i]; //字符码转成字符
}
return data;
}
float ToFloat(Byte * bytes, unsigned int origin)
{
int val = ToInt32(bytes, origin);
return *(float*)&val;
}
double ToDouble(Byte * bytes, unsigned int origin)
{
int val = ToInt64(bytes, origin);
return *(double*)&val;
}
void Reversal(Byte * bytes, unsigned int origin, unsigned int len)
{
printf("格式转换");
int end = origin + len;
for (int i = origin; i < origin + len; i++)
{
Byte temp; //拷贝内存,高低字节数据互换
memcpy(&temp, &bytes[origin + i], 1);
memcpy(&bytes[origin+i], &bytes[end-i], 1);
memcpy(&bytes[end - i], &temp, 1);
temp = NULL;
}
}
bool IsBig_Endian()
{
unsigned short test = 0x1234;
if (*((unsigned char*)&test) == 0x12)
{
return true;
}
else
{
return false;
}
}
测试,program.cpp
#include
extern "C"
{
#include "BitConvert.h"
#include
}
int main()
{
Byte* a = Int2Bytes(214647);
std::cout << ToInt32(a, 0) << std::endl;
free(a);
Byte* b = Short2Bytes(1234);
std::cout << ToInt16(b, 0) << std::endl;
free(b);
Byte* c = Bool2Bytes(true);
std::cout << ToBool(c, 0) << std::endl;
free(c);
Byte* d = String2Bytes("懂撒课大赛第三", "UTF-8");
char* str = ToString(d, 0, strlen("懂撒课大赛第三"), "UTF-8");
std::cout << str << std::endl;
free(str);
free(d);
Byte* e = Long2Bytes(1234567891);
std::cout << ToInt64(e, 0) << std::endl;
free(e);
float f = 123456.1245f;
Byte* f_bytes = Float2Bytes(f);
std::cout << ToFloat(f_bytes, 0) << std::endl;
return 0;
}