简体   繁体   中英

Convert binary data to char[] in C

I have void *data in C which point to a binary data (image), and want to convert it to char* new_data to store somewhere else; then afterwards I want to convert new_data back to binary form.

The problem is, because of data contains many 0 in the middle, if I do

(char*) data

the new string will be truncated at the first 0 . My very first idea is to temporarily convert 0 to other value, but a new problem will appear.

What is a good way to perform my task?

Thanks for your help

You should not treat your data as a string. You can see it as an array of bytes. For that, I suggest to use the type uint8_t defined in stdint.h .

By assigning one pointer type to other doesn't change the data itself. It seems you are using string operation on your data which doesn't make sense. If you want to just copy data then use memcopy function. And if you want to manipulate your data then typecast it's start address as uint8_t*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM