I need to fwritea char * to file and fread it on another platform where char‘s signedness varies.
- Are there ways to solve this other than explicitly serializing a
unsigned char*? - Is it always safe to cast a
char*to anunsigned char*?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In C, it’s safe to access any type as
unsigned char [sizeof T]; this is called the representation. The question is whether copying this representation between diverse systems will preserve the value. Here are the relevant facts/issues:char(and keep in mind, all characters in the basic execution character set must be positive) have the same representation as anunsigned charwith the same value. (The same applies to other signed/unsigned integer types too.)chartypes are completely compatible (modulo the difference in how the values are interpreted) and it’s perfectly safe to access them as either type. Moreover, the C standard makes it difficult if not impossible to produce a valid implementation where plaincharis signed and not twos-complement, and I think it’s safe to say no such implementation exists or will ever exist.char(these are integers!) are preserved when transporting the file to another system, that doesn’t necessarily imply that the character identities will be preserved, since the target system might use a different character encoding (EBCDIC puke..).This is a lot of mumbo-jumbo, but the result you should take away is that unless your goal is pedantry and language-lawyering, there’s nothing to worry about. Just use
fwriteandfreaddirectly on strings and don’t worry about whether they wereunsigned char[]orchar[]strings.