Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>New post on september 2016!</h3> <p>As this is very specific, this add will be presented at very bottom of this.</p> <h3>Upgrade for adding specific bash version (with bashisms)</h3> <p>With new version of <code>printf</code> built-in, you could do a lot without having to fork (<code>$(...)</code>) making so your script a lot faster.</p> <p>First let see (by using <code>seq</code> and <code>sed</code>) how to parse <em>hd output</em>:</p> <pre><code>echo ;sed &lt;(seq -f %02g 0 $[COLUMNS-1]) -ne ' /0$/{s/^\(.*\)0$/\o0337\o033[A\1\o03380/;H;}; /[1-9]$/{s/^.*\(.\)/\1/;H}; ${x;s/\n//g;p}';hd &lt; &lt;(echo Hello good world!) 0 1 2 3 4 5 6 7 012345678901234567890123456789012345678901234567890123456789012345678901234567 00000000 48 65 6c 6c 6f 20 67 6f 6f 64 20 77 6f 72 6c 64 |Hello good world| 00000010 21 0a |!.| 00000012 </code></pre> <p>Were hexadecimal part begin at col 10 and end at col 56, spaced by 3 chars and having one extra space at col 34.</p> <p>So parsing this could by done by:</p> <pre><code>while read line ;do for x in ${line:10:48};do printf -v x \\%o 0x$x printf $x done done &lt; &lt;( ls -l --color | hd ) </code></pre> <h3>Old original post</h3> <p><strong>Edit 2</strong> for Hexadecimal, you could use <code>hd</code></p> <pre><code>echo Hello world | hd 00000000 48 65 6c 6c 6f 20 77 6f 72 6c 64 0a |Hello world.| </code></pre> <p>or <code>od</code></p> <pre><code>echo Hello world | od -t x1 -t c 0000000 48 65 6c 6c 6f 20 77 6f 72 6c 64 0a H e l l o w o r l d \n </code></pre> <p>shortly</p> <pre><code>while IFS= read -r -n1 car;do [ "$car" ] &amp;&amp; echo -n "$car" || echo ; done </code></pre> <p>try them:</p> <pre><code>while IFS= read -rn1 c;do [ "$c" ]&amp;&amp;echo -n "$c"||echo;done &lt; &lt;(ls -l --color) </code></pre> <p>Explain:</p> <pre><code>while IFS= read -rn1 car # unset InputFieldSeparator so read every chars do [ "$car" ] &amp;&amp; # Test if there is ``something''? echo -n "$car" || # then echo them echo # Else, there is an end-of-line, so print one done </code></pre> <p><strong>Edit</strong>; Question was edited: need hex values!?</p> <pre><code>od -An -t x1 | while read line;do for char in $line;do echo $char;done ;done </code></pre> <p>Demo:</p> <pre><code>od -An -t x1 &lt; &lt;(ls -l --color ) | # Translate binary to 1 byte hex while read line;do # Read line of HEX pairs for char in $line;do # For each pair printf "\x$char" # Print translate HEX to binary done done </code></pre> <p>Demo 2: <strong>We have both hex and binary</strong></p> <pre><code>od -An -t x1 &lt; &lt;(ls -l --color ) | # Translate binary to 1 byte hex while read line;do # Read line of HEX pairs for char in $line;do # For each pair bin="$(printf "\x$char")" # translate HEX to binary dec=$(printf "%d" 0x$char) # translate to decimal [ $dec -lt 32 ] || # if caracter not printable ( [ $dec -gt 128 ] &amp;&amp; # change bin to a single dot. [ $dec -lt 160 ] ) &amp;&amp; bin="." str="$str$bin" echo -n $char \ # Print HEX value and a space ((i++)) # count printed values if [ $i -gt 15 ] ;then i=0 echo " - $str" str="" fi done done </code></pre> <h2>New post on september 2016:</h2> <p>This could be usefull on very specific cases, ( I've used them to manualy copy GPT partitions between two disk, at low level, without having <code>/usr</code> mounted...)</p> <h1>Yes, bash could read binary!</h1> <p>... but only one byte, by one... (because `char(0)' couldn't be correctly read, the only way of reading them correctly is to consider <em>end-of-file</em>, where if no caracter is read and end of file not reached, then character read is a char(0)).</p> <p>This is more a proof of concept than a relly usefull tool: there is a <strong><em>pure <a href="/questions/tagged/bash" class="post-tag" title="show questions tagged &#39;bash&#39;" rel="tag">bash</a></em></strong> version of <code>hd</code> (hexdump).</p> <p>This use recent <em>bashisms</em>, under <code>bash v4.3</code> or higher.</p> <pre><code>#!/bin/bash printf -v ascii \\%o {32..126} printf -v ascii "$ascii" printf -v cntrl %-20sE abtnvfr values=() todisplay= address=0 printf -v fmt8 %8s fmt8=${fmt8// / %02x} while LANG=C IFS= read -r -d '' -n 1 char ;do if [ "$char" ] ;then printf -v char "%q" "$char" ((${#char}==1)) &amp;&amp; todisplay+=$char || todisplay+=. case ${#char} in 1|2 ) char=${ascii%$char*};values+=($((${#char}+32)));; 7 ) char=${char#*\'\\};values+=($((8#${char%\'})));; 5 ) char=${char#*\'\\};char=${cntrl%${char%\'}*}; values+=($((${#char}+7)));; * ) echo &gt;&amp;2 ERROR: $char;; esac else values+=(0) fi </code></pre> <h1> </h1> <pre><code> if [ ${#values[@]} -gt 15 ] ;then printf "%08x $fmt8 $fmt8 |%s|\n" $address ${values[@]} "$todisplay" ((address+=16)) values=() todisplay= fi done if [ "$values" ] ;then ((${#values[@]}&gt;8))&amp;&amp;fmt="$fmt8 ${fmt8:0:(${#values[@]}%8)*5}"|| fmt="${fmt8:0:${#values[@]}*5}" printf "%08x $fmt%$(( 50-${#values[@]}*3-(${#values[@]}&gt;8?1:0) ))s |%s|\n" $address ${values[@]} ''""'' "$todisplay" fi printf "%08x (%d chars read.)\n" $((address+${#values[@]})){,} </code></pre> <p>You could try/use this, but don't try to compare performances!</p> <pre><code>time hd &lt; &lt;(seq 1 10000|gzip)|wc 1415 25480 111711 real 0m0.020s user 0m0.008s sys 0m0.000s time ./hex.sh &lt; &lt;(seq 1 10000|gzip)|wc 1415 25452 111669 real 0m2.636s user 0m2.496s sys 0m0.048s </code></pre> <p>same job: 20ms for <code>hd</code> vs 2000ms for my <code>bash script</code>.</p> <p>... but if you wanna read 4 bytes in a file header or even a <em>sector address</em> in an hard drive, this could do the job...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload