r/C_Programming • u/toon_link_776 • 3h ago
Best Way to Read 'Raw Data'
I'm writing a C program for the pi pico and realized I never really figured out how to properly cast characters to raw bytes then to other things, or if there was really a way to do that.
- Does char mean "signed char" by default in any C program, or is that up to interpretation by the compiler?
- Would using a void pointer to dereference then recast to *uint8_t work?
- When bytes are read through getc(), are the bytes modified before they are stored? As in, how are the bytes that are read interpreted to translate to characters, and how are they stored? My initial thought was that the bytes were interpreted "as-is", meaning that they were copied directly to the memory in the character and interpreted as signed characters. But it seems like they're being reinterpreted in some way as they are cast.
- If something about the process is too implementation specific, is reading as "raw data" just a flat out bad idea or not supported at all? Will the behavior be undefined on other systems? As long as I can get it to work on the pico it's fine, it doesn't have to be portable. But for future reference, should I not try to think of things as "raw data" unless the implementation specifically defines how it stores the data?
- Less important, but how are ascii codes mapped to signed characters? (every chart I've seen has never listed a negative number, only 0-255) (( and if signed is the standard, then why does every asii chart list 0-255... ))
// this was the void pointer way I was going to try, would this work?
tnext = *((uint8_t*)((void*)&next)); // Cast to uint8_t(Not sure if this is a repeat but I couldn't find exactly what I was looking for. Sorry if this is a dupe)