charunsigned char 1 byte shortunsigned short 2 bytes
常見的資料型態 • • • char,unsigned char: 1 byte short,unsigned short : 2 bytes int,unsigned int: 4 bytes float: 4 bytes double: 8 bytes • 在記憶體中皆以 byte 存放。讀入變數後, 代表特定數值
資料寫入 char buffer[4]={-1, 0, 1, 2}; FILE * p. File; p. File = fopen ("d: \my. Data. dat", "wb"); fwrite(buffer , sizeof(char), sizeof(buffer), p. File); • fclose (p. File); • •
資料讀取 unsigned char buffer[4]; FILE * p. File; p. File = fopen ("d: \my. Data. dat", "rb"); fread (buffer , sizeof(char), sizeof(buffer), p. File); • fclose (p. File); • •
負整數表示方式 • 一補數: 數字作位元反相運算,如: – 00000001 -->11111110,1 and -1 – 01000000 -->10111111,64 and -64 – 0000 -->1111,+0 and -0 • 2's complement(二補數): 一補數 + 1,如: – 00000001 -->1111,1 and -1 – 01000000 -->11000000,64 and -64 – 0000 -->0000,+0 and -0 • 一補數 0 有兩種表示方式,二補數只一種 • https: //zh. wikipedia. org/wiki/%E 4%BA%8 C%E 8%A 3%9 C%E 6%95%B 8
C data type range • https: //www. tutorialspoint. com/cprogrammin g/c_data_types. htm
位元組順序 • 令 int n 的值為 256*256 + 2*256 + 3,在不 同平台,其 bytes 存放次序不同: • big endian (如 linux) – 0000, 00000001, 00000010, 00000011 • little endian (如 windows) – 00000011, 00000010, 00000001, 0000
- Slides: 8