Download presentation
Presentation is loading. Please wait.
Published byMagdalen Wells Modified over 8 years ago
1
Binary IO Writing and Reading Raw Data
2
Files Two major flavors of file: Text Binary
3
Text Files Text files – Easier to read by hand – Easier to reverse engineer – Easier to hand edit – More portable
4
Binary Files Binary files – More compact "2324223" = 7 ascii chars = 7 bytes (or more) 1 int = 4 bytes – Faster No conversions from strings to numbers Easier random access – Known size for numerics
5
Working in Binary Mode Can specify binary more when opening a file
6
Working in Binary Mode Binary IO : n bytes starting at address s – Address expressed as char * char = 1 byte
7
Writing Write c-string:
8
Results Outputting ascii chars… Hex editor: Good text editor:Notepad:
9
Writing Non-Chars Write other types: – Get pointer to data – Cast as a char* – Use sizeof( ) to calculate number of bytes
10
Cast Types Static_cast sanity checks types Reinterpret_cast sanity checks size C-Style cast picks whichever
11
Cast Types Static_cast sanity checks types Reinterpret_cast sanity checks size C-Style cast picks whichever
12
Results Outputting ascii followed by bits for 15 (F) Hex editor: Good text editor:Notepad:
13
Size Int = 4 bytes
14
Results Outputting 15 (F 16 ) followed by 258 (102 16 )
15
Endianess Endianess : bytes order of a word in main memory
16
Little vs Big Endian Big is "Normal": Little weird – Words in order – Bytes in a word backwards
17
Results Outputting 15 (F 16 ) followed by 258 (102 16 ) Little Endian ArrangementMeaning 0F00 0F Little Endian ArrangementMeaning 020100 0102
18
Reading Need to read string, 2 ints – String unknown length
19
Reading Read string char by char:
20
Reading Same, using c-string
21
Reading Reading two ints:
22
Complex Files Graphic/Sound/etc… files have defined structure:
23
Other Option Structured text : XML – Less efficient than text – Machine parseable – Wide collection of tools
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.