%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%22marimo%3E%3D0.23.9%22%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Text%20and%20binary%20files%0A%0A%20%20%20%20%3E%20Marcos%20Duarte%2C%20Renato%20Naville%20Watanabe%3Cbr%3E%0A%20%20%20%20%3E%20%5BLaboratory%20of%20Biomechanics%20and%20Motor%20Control%5D(https%3A%2F%2Fbmclab.pesquisa.ufabc.edu.br%2F)%3Cbr%3E%0A%20%20%20%20%3E%20Federal%20University%20of%20ABC%2C%20Brazil%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20How%20to%20use%20this%20guide%0A%0A%20%20%20%20A%20sensor%20sends%20the%20value%201.5.%20How%20can%20the%20same%20number%20appear%20as%20readable%20characters%20or%20as%20four%20binary%20bytes%3F%0A%0A%20%20%20%20We%20will%20distinguish%20an%20encoding%20from%20a%20file%20layout%20and%20inspect%20both%20representations%20in%20memory.%20No%20files%20or%20extra%20packages%20are%20required.%0A%0A%20%20%20%20Predict%20the%20result%20before%20running%20each%20example.%20Try%20the%20**Exercises**%20before%20opening%20their%20worked%20solutions.%20Finish%20with%20the%20**Checkpoint%20questions**%20and%20**Problems**%3B%20the%20latter%20provide%20final%20answers%20only.%0A%0A%20%20%20%20Open%20this%20file%20locally%20in%20marimo%2C%20on%20%5Bmarimo.app%5D(https%3A%2F%2Fmarimo.app%2F)%2C%20or%20in%20%5Bmolab%5D(https%3A%2F%2Fmolab.marimo.io%2Fnotebooks).%20The%20examples%20are%20self-contained.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Python%20setup%0A%0A%20%20%20%20Python's%20built-in%20text%20and%20byte%20types%20and%20its%20%60struct%60%20module%20are%20enough%20for%20these%20examples.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20struct%0A%0A%20%20%20%20return%20mo%2C%20struct%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20All%20files%20contain%20bytes%0A%0A%20%20%20%20Text%20formats%20interpret%20bytes%20using%20a%20character%20encoding.%20ASCII%20defines%20128%20character%20codes%3B%20UTF-8%20can%20encode%20Unicode%20characters%2C%20including%20accented%20letters.%20A%20file%20extension%20is%20a%20convention%2C%20not%20proof%20of%20an%20encoding.%20To%20interpret%20a%20scientific%20text%20table%2C%20we%20also%20need%20its%20delimiter%2C%20column%20meanings%2C%20and%20units.%0A%0A%20%20%20%20A%20binary%20format%20directly%20specifies%20numeric%20representation%20and%20structure.%20It%20may%20be%20more%20compact%20or%20faster%20to%20parse%2C%20but%20that%20depends%20on%20the%20data%20and%20format%3B%20neither%20is%20guaranteed%20merely%20by%20calling%20a%20file%20binary.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20text_bytes%20%3D%20%221.5%5Cn%22.encode(%22utf-8%22)%0A%20%20%20%20text_bytes%2C%20len(text_bytes)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%201.**%20How%20many%20bytes%20does%20the%20UTF-8%20text%20%601.5%60%20occupy%20without%20a%20newline%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%201%0A%0A%20%20%20%20Three%20bytes%3A%20one%20for%20each%20ASCII%20character%20%601%60%2C%20%60.%60%2C%20and%20%605%60.%20A%20trailing%20newline%20adds%20one%20byte%20in%20this%20example.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20A%20number%20needs%20a%20representation%0A%0A%20%20%20%20The%20format%20%60%3Cf%60%20specifies%20a%20little-endian%2032-bit%20floating-point%20value.%20Decoding%20the%20same%20bytes%20with%20the%20wrong%20type%20or%20byte%20order%20can%20produce%20a%20plausible-looking%20but%20incorrect%20number.%20This%20is%20why%20binary%20readers%20need%20a%20format%20specification.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(struct)%3A%0A%20%20%20%20binary_bytes%20%3D%20struct.pack(%22%3Cf%22%2C%201.5)%0A%20%20%20%20binary_bytes%2C%20len(binary_bytes)%2C%20struct.unpack(%22%3Cf%22%2C%20binary_bytes)%5B0%5D%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%202.**%20Does%20seeing%20%601.5%60%20in%20a%20text%20column%20tell%20you%20whether%20it%20is%20metres%2C%20seconds%2C%20or%20volts%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%202%0A%0A%20%20%20%20No.%20Character%20decoding%20recovers%20the%20text%2C%20but%20physical%20interpretation%20requires%20metadata%20or%20a%20documented%20column%20schema.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Checkpoint%20questions%0A%0A%20%20%20%201.%20Is%20UTF-8%20a%20numerical%20data%20schema%3F%0A%0A%20%20%20%202.%20Does%20a%20.txt%20extension%20guarantee%20ASCII%3F%0A%0A%20%20%20%203.%20Are%20text%20files%20stored%20as%20physical%20characters%20rather%20than%20bits%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Checkpoint%20answers%0A%0A%20%20%20%201.%20No%3B%20it%20is%20a%20character%20encoding.%0A%20%20%20%202.%20No.%0A%20%20%20%203.%20No%3B%20both%20text%20and%20other%20formats%20are%20stored%20as%20bytes.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Problems%0A%0A%20%20%20%20Try%20these%20independently%3B%20open%20a%20panel%20only%20to%20check%20your%20final%20answer.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Problem%201.**%20How%20many%20bytes%20are%20used%20by%20ten%2032-bit%20floating-point%20values%2C%20excluding%20headers%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Answer%20to%20Problem%201%0A%0A%20%20%20%2040%20bytes.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Problem%202.**%20What%20two%20numerical%20conventions%20does%20%60%3Cf%60%20specify%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Answer%20to%20Problem%202%0A%0A%20%20%20%20Little-endian%20byte%20order%20and%2032-bit%20floating-point%20representation.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Go%20deeper%0A%0A%20%20%20%20Apply%20these%20ideas%20in%20the%20%5BC3D%20guide%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fopen_c3d_file.py)%20and%20%5BCortex%20text-file%20guide%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fread_mac_ascii_files.py).%0A%0A%20%20%20%20%23%23%23%20Video%20lectures%20on%20the%20Internet%0A%0A%20%20%20%20%5BHarvard%20CS50%20lecture%20archive%5D(https%3A%2F%2Fcs50.harvard.edu%2Fx%2F2026%2Fweeks%2F0%2F)%20introduces%20binary%20representation.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20References%0A%0A%20%20%20%20-%20%5BPython%3A%20struct%5D(https%3A%2F%2Fdocs.python.org%2F3%2Flibrary%2Fstruct.html).%0A%0A%20%20%20%20-%20%5BPython%20Unicode%20how-to%5D(https%3A%2F%2Fdocs.python.org%2F3%2Fhowto%2Funicode.html).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
1ce7eb702566223926550785cc5a89b0