Hex dump: Difference between revisions
Content deleted Content added
m →{{header|Phix}}: marked js-incompatible, added one page at a time note |
m →{{header|Phix}}: found an ugly way to make it js-compatible, size not final address |
||
Line 239: | Line 239: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<small>Aside: Phix does not really "do" utf-16, that is in source code terms, but declaring things as utf-8 and converting to utf-16 is not exactly hard. See talk page.</small> |
<small>Aside: Phix does not really "do" utf-16, that is in source code terms, but declaring things as utf-8 and converting to utf-16 is not exactly hard. See talk page.</small> |
||
<!--<syntaxhighlight lang="phix">( |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;"> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- (provided utf8_to_utf16/peek/allocate_wstring/get_text avoided)</span> |
||
<span style="color: #004080;">string</span> <span style="color: #000000;">utf8</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"\#EF\#BB\#BFRosetta Code is a programming chrestomathy site \xF0\x9F\x98\x80."</span> |
<span style="color: #004080;">string</span> <span style="color: #000000;">utf8</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"\#EF\#BB\#BFRosetta Code is a programming chrestomathy site \xF0\x9F\x98\x80."</span> |
||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">utf16</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">utf8_to_utf16</span><span style="color: #0000FF;">(</span><span style="color: #000000;">utf8</span><span style="color: #0000FF;">)</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">utf16</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">utf8_to_utf16</span><span style="color: #0000FF;">(</span><span style="color: #000000;">utf8</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">constant</span> <span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">peek</span><span style="color: #0000FF;">({</span><span style="color: #000000;">allocate_wstring</span><span style="color: #0000FF;">(</span><span style="color: #000000;">utf16</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">utf16</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span> |
<span style="color: #008080;">constant</span> <span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">peek</span><span style="color: #0000FF;">({</span><span style="color: #000000;">allocate_wstring</span><span style="color: #0000FF;">(</span><span style="color: #000000;">utf16</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">utf16</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #000080;font-style:italic;">--or: |
<span style="color: #000080;font-style:italic;">--or: |
||
--constant string s = get_text(filename) -- (if you have a suitable premade one to hand) |
--constant string s = get_text(filename) -- (if you have a suitable premade one to hand) |
||
--or: (javascript compatible) |
|||
--constant string s = "\xFF\xFER\0o\0s\0e\0t\0t\0a\0 \0C\0o\0d\0e\0 \0i\0s\0 \0a\0 "& |
|||
-- "\0p\0r\0o\0g\0r\0a\0m\0m\0i\0n\0g\0 \0c\0h\0r\0e\0s\0t\0o\0m\0a\0t\0h\0y\0 "& |
|||
-- "\0s\0i\0t\0e\0 \0=\xD8\0\xDE.\0"</span> |
|||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">hexdump</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">start</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">finish</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">bHex</span><span style="color: #0000FF;">=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span> <span style="color: #000000;"> |
<span style="color: #008080;">procedure</span> <span style="color: #000000;">hexdump</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">start</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">finish</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">bHex</span><span style="color: #0000FF;">=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bFinalSize</span><span style="color: #0000FF;">=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">start</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">finish</span><span style="color: #0000FF;">]</span> |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">ll</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bHex</span><span style="color: #0000FF;">?</span><span style="color: #000000;">16</span><span style="color: #0000FF;">:</span><span style="color: #000000;">6</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">ls</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #004080;">string</span> <span style="color: #000000;">hbfmt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bHex</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"%02x "</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"%08b "</span><span style="color: #0000FF;">),</span> |
<span style="color: #004080;">string</span> <span style="color: #000000;">hbfmt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bHex</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"%02x "</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"%08b "</span><span style="color: #0000FF;">),</span> |
||
<span style="color: #000000;">lnfmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"%08x %s |%-"</span> <span style="color: #0000FF;">&</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ll</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">&</span> <span style="color: #008000;">"s|\n"</span> |
<span style="color: #000000;">lnfmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"%08x %s |%-"</span> <span style="color: #0000FF;">&</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ll</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">&</span> <span style="color: #008000;">"s|\n"</span> |
||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;"> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ll</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">for</span> <span style="color: #000000;">l</span> <span style="color: #008080;">in</span> <span style="color: #000000;">lines</span> <span style="color: #008080;">do</span> |
<span style="color: #008080;">for</span> <span style="color: #000000;">l</span> <span style="color: #008080;">in</span> <span style="color: #000000;">lines</span> <span style="color: #008080;">do</span> |
||
<span style="color: #004080;">string</span> <span style="color: #000000;">hb</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ascii</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> |
<span style="color: #004080;">string</span> <span style="color: #000000;">hb</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ascii</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> |
||
Line 264: | Line 269: | ||
<span style="color: #000000;">start</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span> |
<span style="color: #000000;">start</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<span style="color: #008080;">if</span> <span style="color: #000000;"> |
<span style="color: #008080;">if</span> <span style="color: #000000;">bFinalSize</span> <span style="color: #008080;">then</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%08x\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;"> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%08x\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ls</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |