Hex dump: Difference between revisions
Content deleted Content added
Added XPL0 example. |
m →{{header|Phix}}: use pygments |
||
Line 594: | Line 594: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!-- |
<!--(phixonline)--> |
||
⚫ | |||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
with javascript_semantics |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">utf16</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#FEFF</span> <span style="color: #0000FF;">&</span> <span style="color: #008000;">"Rosetta Code is a programming chrestomathy site "</span> <span style="color: #0000FF;">&</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">#d83d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#DE00</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">}</span> |
|||
sequence utf16 = #FEFF & "Rosetta Code is a programming chrestomathy site " & {#d83d,#DE00,'.'} |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">to2</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#FF</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">/</span><span style="color: #000000;">#100</span><span style="color: #0000FF;">)}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
function to2(integer i) return {and_bits(i,#FF),floor(i/#100)} end function |
|||
<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;">flatten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">utf16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">to2</span><span style="color: #0000FF;">),</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span> |
|||
constant string s = flatten(apply(utf16,to2),"") |
|||
<span style="color: #000080;font-style:italic;">--or: (javascript incompatible) |
|||
--or: (javascript incompatible) |
|||
--constant string s = get_text(filename) -- (if you have a suitable premade one to hand) |
|||
<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> |
|||
procedure hexdump(string s, integer start=0, finish=-1, bool bHex=true, bFinalSize=true) |
|||
<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> |
|||
s = s[start+1..finish] |
|||
<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> |
|||
integer ll = iff(bHex?16:6), ls = length(s) |
|||
<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> |
|||
string hbfmt = iff(bHex?"%02x ":"%08b "), |
|||
<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> |
|||
lnfmt = "%08x %s |%-" & sprintf("%d",ll) & "s|\n" |
|||
<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> |
|||
sequence lines = split_by(s,ll) |
|||
<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> |
|||
for l in lines do |
|||
<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> |
|||
string hb = " ", ascii = "" |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span> <span style="color: #008080;">in</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span> |
|||
for i,b in l do |
|||
<span style="color: #000000;">hb</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hbfmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> |
|||
hb &= sprintf(hbfmt,b) |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;"><</span><span style="color: #008000;">' '</span> <span style="color: #008080;">or</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">></span><span style="color: #008000;">'~'</span> <span style="color: #008080;">then</span> <span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'.'</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
if b<' ' or b>'~' then b = '.' end if |
|||
<span style="color: #000000;">ascii</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">b</span> |
|||
ascii &= b |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">8</span> <span style="color: #008080;">then</span> <span style="color: #000000;">hb</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">' '</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
if i=8 then hb &= ' ' end if |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
end for |
|||
<span style="color: #000000;">hb</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">pad_tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hb</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;">50</span><span style="color: #0000FF;">:</span><span style="color: #000000;">55</span><span style="color: #0000FF;">))</span> |
|||
hb = pad_tail(hb,iff(bHex?50:55)) |
|||
<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: #000000;">lnfmt</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">start</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hb</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ascii</span><span style="color: #0000FF;">})</span> |
|||
printf(1,lnfmt,{start,hb,ascii}) |
|||
<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> |
|||
start += length(l) |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
end for |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">bFinalSize</span> <span style="color: #008080;">then</span> |
|||
if bFinalSize then |
|||
<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> |
|||
printf(1,"%08x\n",{ls}) |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
end if |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
|||
end procedure |
|||
<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;">"Hexadecimal:\n"</span><span style="color: #0000FF;">)</span> |
|||
printf(1,"Hexadecimal:\n") |
|||
<span style="color: #000000;">hexdump</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
hexdump(s) |
|||
<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;">"\nBinary:\n"</span><span style="color: #0000FF;">)</span> |
|||
printf(1,"\nBinary:\n") |
|||
<span style="color: #000000;">hexdump</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bHex</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span> |
|||
hexdump(s,bHex:=false) |
|||
⚫ | |||
</syntaxhighlight> |
|||
Although not shown, the intention is the outer calling code (last four lines) could call hexdump() to display one page at a time, with appropriate user input, and/or read s in chunks and pass a file offset instead of or as well as start, finish. |
Although not shown, the intention is the outer calling code (last four lines) could call hexdump() to display one page at a time, with appropriate user input, and/or read s in chunks and pass a file offset instead of or as well as start, finish. |
||
{{out}} |
{{out}} |