C語言實(shí)現(xiàn)基本數(shù)據(jù)類型與字節(jié)類型互相轉(zhuǎn)換
在網(wǎng)絡(luò)編程中,一般通過數(shù)據(jù)包的方式來傳遞數(shù)據(jù),數(shù)據(jù)包內(nèi)存儲的是各種數(shù)據(jù)的表示形式,例如浮點(diǎn)數(shù)使用IEEE754標(biāo)準(zhǔn)存儲,字符使用字符碼存儲(例如使用ASCII碼)。本篇文章主要講述了基本數(shù)據(jù)類型與這些存儲格式的相互轉(zhuǎn)換。
數(shù)據(jù)包如何在實(shí)際中使用可以參考->C#數(shù)據(jù)交互服務(wù)器(一)
定義unsigned char Byte為字節(jié)類型,使用小端模式存儲(如操作系統(tǒng)使用大端這里還是轉(zhuǎn)換成小端方式),基本數(shù)據(jù)類型大小采用64位標(biāo)準(zhǔn)。
BitConvert.h
#pragma once
#ifndef Byte
#include <stdbool.h>
typedef unsigned char Byte; //定義字節(jié)類型
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); //存儲方式轉(zhuǎn)換(操作系統(tǒng)為大端模式時(shí)自動轉(zhuǎn)換)
bool IsBig_Endian(); //是否為大端模式
#endif
BitConvert.c
#pragma once
/*
靠左高位,靠右低位
大端模式和小端模式,例如有short=0x1234,大端模式存儲為12 34,小端模式存儲為34 12;大端模式更符合人腦思維,小端模式更適合計(jì)算機(jī)
此處使用小端模式存儲數(shù)據(jù),Byte范圍為0~255,滿255進(jìn)1,例如有int=1234,則byte[0]=214,byte[1]=4
注意:此處沒有做溢出檢測,無論操作系統(tǒng)使用小端還是大端,這里都將使用小端模式存儲
*/
#include "BitConvert.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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]; //字符轉(zhuǎn)字符碼
}
return bytes;
}
Byte * Float2Bytes(float data)
{
Int2Bytes(*(int*)&data); //(int*)&data為指向data的地址的int指針,前方加*表示該地址存儲的內(nèi)容,即IEE754標(biāo)準(zhǔn)格式數(shù)據(jù),此處強(qiáng)制轉(zhuǎn)換部分精度將丟失
}
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]; //字符碼轉(zhuǎn)成字符
}
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("格式轉(zhuǎn)換");
int end = origin + len;
for (int i = origin; i < origin + len; i++)
{
Byte temp; //拷貝內(nèi)存,高低字節(jié)數(shù)據(jù)互換
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 <iostream>
extern "C"
{
#include "BitConvert.h"
#include <stdio.h>
}
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;
}