C++中二進(jìn)制數(shù)據(jù)序列化和反序列化詳解
一、基礎(chǔ)知識(shí)
1、memcpy:內(nèi)存拷貝函數(shù)
函數(shù)原型:void *memcpy(void *dest, const void *src, size_t count);
2、代碼編寫(xiě)(采用循環(huán)的方式將字節(jié)內(nèi)容逐一拷貝)
void* my_memcpy(void* dest, const void* src, size_t count) { assert(dest); //防止函數(shù)接受空指針進(jìn)行操作 assert(src); void* ret = dest; while (count--) { *(char*)dest = *(char*)src; dest = (char*)dest + 1; src = (char*)src + 1; } return ret; }
3、c_str()函數(shù):可以將const string* 類(lèi)型 轉(zhuǎn)換為 const char* 類(lèi)型
二、什么是數(shù)據(jù)序列化和數(shù)據(jù)反序列化
數(shù)據(jù)序列化:將對(duì)象變成字節(jié)流的形式傳出去
數(shù)據(jù)反序列化:從字節(jié)流恢復(fù)成原來(lái)的對(duì)象
三、為什么需要讀數(shù)據(jù)進(jìn)行序列化和反序列化處理
1、序列號(hào)可以將數(shù)據(jù)轉(zhuǎn)換為一種格式,使其容易通過(guò)網(wǎng)絡(luò)進(jìn)行傳輸和存儲(chǔ)。反序列化則可以將序列化后的數(shù)據(jù)還原為原始形式,以便后續(xù)進(jìn)行處理
2、內(nèi)存中對(duì)象轉(zhuǎn)換為二進(jìn)制數(shù)據(jù):當(dāng)需要將對(duì)象的狀態(tài)傳輸?shù)竭h(yuǎn)程機(jī)器,或?qū)⑵浯鎯?chǔ)在磁盤(pán)上時(shí),就需要將對(duì)象轉(zhuǎn)換為二進(jìn)制數(shù)據(jù)。
3、二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為內(nèi)存中的對(duì)象:當(dāng)需要從遠(yuǎn)程機(jī)器或磁盤(pán)中讀取對(duì)象狀態(tài)時(shí),需要將已序列化的二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為原始對(duì)象。
四、數(shù)據(jù)序列化處理
struct Student { string name; string grade; int age; double scale; }; //數(shù)據(jù)序列化 char* serialization(Student* student) { int size = sizeof(Student); char* pBuf = new char[size]; //記錄指針的位置 int num = 0; memcpy((pBuf + num), student->name.c_str(), 8); num += 8; memcpy((pBuf + num), student->grade.c_str(), 8); num += 8; *(int *)(pBuf + num) = student->age; num += 4; *(double*)(pBuf + num) = student->scale; return pBuf; }
五、數(shù)據(jù)反序列化處理
void deserialization(char* data) { string name; string garde; int age; double scale; //記錄指針的位置 int num = 0; name = (data + num); num += 8; garde = (data + num); num += 8; age = *(int*)(data + num); num += 4; scale = *(double*)(data + num); cout << "name:" << name << endl; cout << "garde:" << garde << endl; cout << "age:" << age << endl; cout << "scale:" << scale << endl; }
六、運(yùn)行處理
int main() { Student student; student.name = "Jim"; student.grade = "ten"; student.age = 12; student.scale = 88; char* data = serialization(&student); deserialization(data); return 0; }
以上就是C++中二進(jìn)制數(shù)據(jù)序列化和反序列化詳解的詳細(xì)內(nèi)容,更多關(guān)于C++二進(jìn)制數(shù)據(jù)序列化和反序列化的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在C++中實(shí)現(xiàn)云端存儲(chǔ)變量的操作步驟
隨著云計(jì)算技術(shù)的快速發(fā)展,現(xiàn)在我們可以將數(shù)據(jù)存儲(chǔ)在云端,以便于在不同設(shè)備和地點(diǎn)訪問(wèn),在C++中,我們也可以通過(guò)一些方法來(lái)實(shí)現(xiàn)這個(gè)功能,本文將詳細(xì)介紹如何在C++中實(shí)現(xiàn)云端存儲(chǔ)變量,需要的朋友可以參考下2023-11-11c++中string類(lèi)型和int類(lèi)型相互轉(zhuǎn)換的幾種常用方法
我們?cè)诰帉?xiě)程序時(shí),經(jīng)常涉及到int與string之間的類(lèi)型轉(zhuǎn)換,本文主要介紹了c++中string類(lèi)型和int類(lèi)型相互轉(zhuǎn)換的幾種常用方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08C++ OpenCV實(shí)戰(zhàn)之網(wǎng)孔檢測(cè)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用C++和OpenCV實(shí)現(xiàn)網(wǎng)孔檢測(cè),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)OpenCV有一定幫助,感興趣的小伙伴可以了解一下2022-05-05QT中在QLabel顯示圖片并且利用鼠標(biāo)點(diǎn)擊畫(huà)線問(wèn)題
這篇文章主要介紹了QT中在QLabel顯示圖片并且利用鼠標(biāo)點(diǎn)擊畫(huà)線問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11