A hexdumper using echo only...


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users A hexdumper using echo only...
# 1  
Old 01-11-2015
A hexdumper using echo only...

Hi guys...

Sometime ago I said I was going to attempt an Android project.
I got my phone for XMAS and after installing a terminal program I realised how limited the command line is.
I do NOT intend to __root__ the phone at this point but I can read and write to certain folders.
The biggest problem is the vast majority of the commands are missing - OUCH!
awk, printf, sed, hexdump, xxd, od, and many many others we take for granted are not available...
I DO have a subset of builtins of which echo -n -e -E is but one.
The terminal program has a large subset of the terminal escape codes so this is a bonus.

This set me on a quest to start my own commands using what I have got to play with.
This DEMO hex dumper for example is a starter.
This is as bizarre as shell scripting can possibly get.
It is SSLLOOWW but gives me the ability to read in small amounts of binary data for ideas in my head.

I have no idea yet whether this works on my phone but I will let you all know in due course.

The phones's default shell [sh] is bash...

** My question is can this be speeded up as it takes around a minute or so to read a random 256 byte file on this MBP. **

This DEMO works like this:-

1) 256 single byte files are generated from 0x00 to 0xFF named as 00.HEX to FF.HEX.
2) $1 is the full path and filename required for viewing.
3) The file $1 is read byte by byte and compared against the single byte files cat(ted) using a brute force method.
4) With the exception of two values, NULL and NEWLINE, these binary values are are printed onto STDOUT only for this DEMO.
5) Read the code for NULL and NEWLINE.
6) The HEX values are extracted from the filenames, NOT, the bytes themselves... <wink>

This was the only way I could think of to get binary into my machine with a small subset of the shell command available...
There is nothing on the net about this... ;o/

LBNL the first display is a random file generated to show it works in binary mode and the second is a hexdump of itself.
Note that hexdump is there for comparison only...
Code:
#!/bin/bash
# hex.sh </full/path/to/filename>
ifs_str="$IFS"
IFS=""
for high_nibble in 0 1 2 3 4 5 6 7 8 9 A B C D E F
do
	for low_nibble in 0 1 2 3 4 5 6 7 8 9 A B C D E F
	do
		echo -e -n "\x$high_nibble$low_nibble" > /tmp/$high_nibble$low_nibble'.HEX'
	done
done
listing=$(ls /tmp/*.HEX)
count=0
char=""
hexval="/tmp/00.HEX"
subscript=0
chr=""
hexdump -C $1
while read -s -d '' -r -n 1 char
do
	subscript=0
	count=0
	while [ $count -le 255 ]
	do
		hexval="${listing:$subscript:${#hexval}}"
		chr=$(cat $hexval)
		if [ "$char" == "$chr" ]
		then
			if [ "$chr" == "" ]
			then
				echo -n "00 "
				break
			fi
			if [ "$chr" != "" ]
			then
				echo -n "${hexval:$((${#hexval}-6)):2} "
				break
			fi
		fi
		subscript=$((subscript+${#hexval}+1))
		count=$((count+1))
		if [ $count -eq 256 ]
		then
			echo -n "0A "
		fi
	done
done < $1
IFS="$ifs_str"
exit

Results, edited to __remove__ the ultra long string created:-
Code:
Last login: Sun Jan 11 09:39:47 on ttys000
AMIGA:barrywalker~> cd Desktop
AMIGA:barrywalker~/Desktop> cd Code
AMIGA:barrywalker~/Desktop/Code> cd Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./hex.sh binary
00000000  b2 5f 96 da d0 5d 4e ba  bc 6d 31 4f 4b 81 2c eb  |._...]N..m1OK.,.|
00000010  b9 ce 53 8e 6a e4 ab fc  98 7a 8a 81 d1 ad 41 ab  |..S.j....z....A.|
00000020  79 83 c0 c9 54 d7 c2 2f  0b ae 54 9e 38 fe 5c 2e  |y...T../..T.8.\.|
00000030  ce 96 1d 51 f8 f2 16 9d  ef dc 23 b9 3f b6 56 8c  |...Q......#.?.V.|
00000040  92 1d 54 38 9b 8c b1 2b  5b 1e 2c 42 b3 89 da 47  |..T8...+[.,B...G|
00000050  7e 95 2d 21 8a d7 77 b9  f9 db 05 76 de 04 ca 14  |~.-!..w....v....|
00000060  20 66 23 7e 93 fc fa fd  5f ad c9 1c 39 8d 47 65  | f#~...._...9.Ge|
00000070  ac ee d8 c2 c9 fe b6 16  13 04 a7 8b 43 77 70 a1  |............Cwp.|
00000080  06 0f 88 9d 0a 0a 1a 14  37 51 57 be fd 74 ab 0b  |........7QW..t..|
00000090  2f 50 64 9b 28 b7 98 df  8a 74 aa 21 07 83 a5 4d  |/Pd.(....t.!...M|
000000a0  d0 42 f2 b1 91 a5 83 64  2c 1e a5 81 80 e2 a7 8a  |.B.....d,.......|
000000b0  84 7d 14 78 9e 38 96 d5  fe b1 7e d6 ad d9 21 f9  |.}.x.8....~...!.|
000000c0  d2 cf 32 92 5d 30 7b ef  93 99 23 81 ab c3 db 87  |..2.]0{...#.....|
000000d0  c9 b8 62 4a 1f 0d 34 fe  9f dc 7d 82 98 bd 09 3d  |..bJ..4...}....=|
000000e0  91 04 60 42 d1 53 09 8f  50 3a 79 80 33 fa 16 23  |..`B.S..P:y.3..#|
000000f0  76 48 76 3a 68 b6 7c 4c  7d 89 30 47 32 8e 56 41  |vHv:h.|L}.0G2.VA|
00000100  9a 6d 19 cc d6 c5 8b db  6f 29 60 19 2e f9 4c 17  |.m......o)`...L.|
00000110  f8 31 8c a0 27 34 63 74  f7 ed 8e cf e5 53 16 e1  |.1..'4ct.....S..|
00000120  18 c2 47 2c 4b 7a ed 0b  c8 f1 58 f1 72 36 bf c4  |..G,Kz....X.r6..|
00000130  a4 f8 d4 9b f1 64 6b 57  dc 36 1b 22 4c 26 2a 87  |.....dkW.6."L&*.|
00000140  e4 fd fa 5f a2 6b b8 1f  51 8e c9 f5 cb 23 e3 38  |..._.k..Q....#.8|
00000150  35 1d bb 0f d8 ec 57 70  c7 6b cb 7d 74 7e 2f 62  |5.....Wp.k.}t~/b|
00000160  44 12 08 b3 11 17 e5 54  6d f5 76 53 bd 91 ba d2  |D......Tm.vS....|
00000170  cb 15 4d 94 34 d0 1e 2b  63 f3 05 eb d7 4f 42 e6  |..M.4..+c....OB.|
00000180  a7 fa 66 21 40 42 ad 43  83 12 31 23 d0 cf b0 6b  |..f!@B.C..1#...k|
00000190  b2 d7 b2 e8 e5 65 1d 88  dd 03 8a 3a 25 93 b9 fe  |.....e.....:%...|
000001a0  c7 96 5c 66 52 b8 3f 14  c9 88 8a 53 19 c4 a1 0a  |..\fR.?....S....|
000001b0  f6 be d4 a9 36 85 b8 4d  2f f8 eb ff da be 8a d7  |....6..M/.......|
000001c0  8e 48 58 02 ed fc 69 44  fe 7e 94 43 00 8e 74 7c  |.HX...iD.~.C..t||
000001d0  b8 45 57 2b 0a 28 f5 a6  ba 3c 1d da 6c 70 23 41  |.EW+.(...<..lp#A|
000001e0  9f 89 74 fd 1b be 2a ff  18 6a 79 a6 b0 7c f9 36  |..t...*..jy..|.6|
000001f0  8d ce 2d b0 c4 83 78 fd  cf 9c de 48 ec 46 9c 9f  |..-...x....H.F..|
00000200
B2 5F 96 DA D0 5D 4E BA BC 6D 31 4F 4B 81 2C EB B9 CE 53 8E 6A E4 AB FC 98 7A
 8A 81 D1 AD 41 AB 79 83 C0 C9 54 D7 C2 2F 0B AE 54 9E 38 FE 5C 2E CE 96 1D 51
 F8 F2 16 9D EF DC 23 B9 3F B6 56 8C 92 1D 54 38 9B 8C B1 2B 5B 1E 2C 42 B3 89
 DA 47 7E 95 2D 21 8A D7 77 B9 F9 DB 05 76 DE 04 CA 14 20 66 23 7E 93 FC FA FD
 5F AD C9 1C 39 8D 47 65 AC EE D8 C2 C9 FE B6 16 13 04 A7 8B 43 77 70 A1 06 0F
 88 9D 0A 0A 1A 14 37 51 57 BE FD 74 AB 0B 2F 50 64 9B 28 B7 98 DF 8A 74 AA 21
 07 83 A5 4D D0 42 F2 B1 91 A5 83 64 2C 1E A5 81 80 E2 A7 8A 84 7D 14 78 9E 38
 96 D5 FE B1 7E D6 AD D9 21 F9 D2 CF 32 92 5D 30 7B EF 93 99 23 81 AB C3 DB 87
 C9 B8 62 4A 1F 0D 34 FE 9F DC 7D 82 98 BD 09 3D 91 04 60 42 D1 53 09 8F 50 3A
 79 80 33 FA 16 23 76 48 76 3A 68 B6 7C 4C 7D 89 30 47 32 8E 56 41 9A 6D 19 CC
 D6 C5 8B DB 6F 29 60 19 2E F9 4C 17 F8 31 8C A0 27 34 63 74 F7 ED 8E CF E5 53
 16 E1 18 C2 47 2C 4B 7A ED 0B C8 F1 58 F1 72 36 BF C4 A4 F8 D4 9B F1 64 6B 57
 DC 36 1B 22 4C 26 2A 87 E4 FD FA 5F A2 6B B8 1F 51 8E C9 F5 CB 23 E3 38 35 1D
 BB 0F D8 EC 57 70 C7 6B CB 7D 74 7E 2F 62 44 12 08 B3 11 17 E5 54 6D F5 76 53
 BD 91 BA D2 CB 15 4D 94 34 D0 1E 2B 63 F3 05 EB D7 4F 42 E6 A7 FA 66 21 40 42
 AD 43 83 12 31 23 D0 CF B0 6B B2 D7 B2 E8 E5 65 1D 88 DD 03 8A 3A 25 93 B9 FE
 C7 96 5C 66 52 B8 3F 14 C9 88 8A 53 19 C4 A1 0A F6 BE D4 A9 36 85 B8 4D 2F F8
 EB FF DA BE 8A D7 8E 48 58 02 ED FC 69 44 FE 7E 94 43 00 8E 74 7C B8 45 57 2B
 0A 28 F5 A6 BA 3C 1D DA 6C 70 23 41 9F 89 74 FD 1B BE 2A FF 18 6A 79 A6 B0 7C
 F9 36 8D CE 2D B0 C4 83 78 FD CF 9C DE 48 EC 46 9C 9F AMIGA:barrywalker~/Desktop/Code/Shell> 
AMIGA:barrywalker~/Desktop/Code/Shell> 
AMIGA:barrywalker~/Desktop/Code/Shell> ./hex.sh hex.sh
00000000  23 21 2f 62 69 6e 2f 62  61 73 68 0a 23 20 68 65  |#!/bin/bash.# he|
00000010  78 2e 73 68 20 3c 2f 66  75 6c 6c 2f 70 61 74 68  |x.sh </full/path|
00000020  2f 74 6f 2f 66 69 6c 65  6e 61 6d 65 3e 0a 69 66  |/to/filename>.if|
00000030  73 5f 73 74 72 3d 22 24  49 46 53 22 0a 49 46 53  |s_str="$IFS".IFS|
00000040  3d 22 22 0a 66 6f 72 20  68 69 67 68 5f 6e 69 62  |="".for high_nib|
00000050  62 6c 65 20 69 6e 20 30  20 31 20 32 20 33 20 34  |ble in 0 1 2 3 4|
00000060  20 35 20 36 20 37 20 38  20 39 20 41 20 42 20 43  | 5 6 7 8 9 A B C|
00000070  20 44 20 45 20 46 0a 64  6f 0a 09 66 6f 72 20 6c  | D E F.do..for l|
00000080  6f 77 5f 6e 69 62 62 6c  65 20 69 6e 20 30 20 31  |ow_nibble in 0 1|
00000090  20 32 20 33 20 34 20 35  20 36 20 37 20 38 20 39  | 2 3 4 5 6 7 8 9|
000000a0  20 41 20 42 20 43 20 44  20 45 20 46 0a 09 64 6f  | A B C D E F..do|
000000b0  0a 09 09 65 63 68 6f 20  2d 65 20 2d 6e 20 22 5c  |...echo -e -n "\|
000000c0  78 24 68 69 67 68 5f 6e  69 62 62 6c 65 24 6c 6f  |x$high_nibble$lo|
000000d0  77 5f 6e 69 62 62 6c 65  22 20 3e 20 2f 74 6d 70  |w_nibble" > /tmp|
000000e0  2f 24 68 69 67 68 5f 6e  69 62 62 6c 65 24 6c 6f  |/$high_nibble$lo|
000000f0  77 5f 6e 69 62 62 6c 65  27 2e 48 45 58 27 0a 09  |w_nibble'.HEX'..|
00000100  64 6f 6e 65 0a 64 6f 6e  65 0a 6c 69 73 74 69 6e  |done.done.listin|
00000110  67 3d 24 28 6c 73 20 2f  74 6d 70 2f 2a 2e 48 45  |g=$(ls /tmp/*.HE|
00000120  58 29 0a 63 6f 75 6e 74  3d 30 0a 63 68 61 72 3d  |X).count=0.char=|
00000130  22 22 0a 68 65 78 76 61  6c 3d 22 2f 74 6d 70 2f  |"".hexval="/tmp/|
00000140  30 30 2e 48 45 58 22 0a  73 75 62 73 63 72 69 70  |00.HEX".subscrip|
00000150  74 3d 30 0a 63 68 72 3d  22 22 0a 68 65 78 64 75  |t=0.chr="".hexdu|
00000160  6d 70 20 2d 43 20 24 31  0a 77 68 69 6c 65 20 72  |mp -C $1.while r|
00000170  65 61 64 20 2d 73 20 2d  64 20 27 27 20 2d 72 20  |ead -s -d '' -r |
00000180  2d 6e 20 31 20 63 68 61  72 0a 64 6f 0a 09 73 75  |-n 1 char.do..su|
00000190  62 73 63 72 69 70 74 3d  30 0a 09 63 6f 75 6e 74  |bscript=0..count|
000001a0  3d 30 0a 09 77 68 69 6c  65 20 5b 20 24 63 6f 75  |=0..while [ $cou|
000001b0  6e 74 20 2d 6c 65 20 32  35 35 20 5d 0a 09 64 6f  |nt -le 255 ]..do|
000001c0  0a 09 09 68 65 78 76 61  6c 3d 22 24 7b 6c 69 73  |...hexval="${lis|
000001d0  74 69 6e 67 3a 24 73 75  62 73 63 72 69 70 74 3a  |ting:$subscript:|
000001e0  24 7b 23 68 65 78 76 61  6c 7d 7d 22 0a 09 09 63  |${#hexval}}"...c|
000001f0  68 72 3d 24 28 63 61 74  20 24 68 65 78 76 61 6c  |hr=$(cat $hexval|
00000200  29 0a 09 09 69 66 20 5b  20 22 24 63 68 61 72 22  |)...if [ "$char"|
00000210  20 3d 3d 20 22 24 63 68  72 22 20 5d 0a 09 09 74  | == "$chr" ]...t|
00000220  68 65 6e 0a 09 09 09 69  66 20 5b 20 22 24 63 68  |hen....if [ "$ch|
00000230  72 22 20 3d 3d 20 22 22  20 5d 0a 09 09 09 74 68  |r" == "" ]....th|
00000240  65 6e 0a 09 09 09 09 65  63 68 6f 20 2d 6e 20 22  |en.....echo -n "|
00000250  30 30 20 22 0a 09 09 09  09 62 72 65 61 6b 0a 09  |00 ".....break..|
00000260  09 09 66 69 0a 09 09 09  69 66 20 5b 20 22 24 63  |..fi....if [ "$c|
00000270  68 72 22 20 21 3d 20 22  22 20 5d 0a 09 09 09 74  |hr" != "" ]....t|
00000280  68 65 6e 0a 09 09 09 09  65 63 68 6f 20 2d 6e 20  |hen.....echo -n |
00000290  22 24 7b 68 65 78 76 61  6c 3a 24 28 28 24 7b 23  |"${hexval:$((${#|
000002a0  68 65 78 76 61 6c 7d 2d  36 29 29 3a 32 7d 20 22  |hexval}-6)):2} "|
000002b0  0a 09 09 09 09 62 72 65  61 6b 0a 09 09 09 66 69  |.....break....fi|
000002c0  0a 09 09 66 69 0a 09 09  73 75 62 73 63 72 69 70  |...fi...subscrip|
000002d0  74 3d 24 28 28 73 75 62  73 63 72 69 70 74 2b 24  |t=$((subscript+$|
000002e0  7b 23 68 65 78 76 61 6c  7d 2b 31 29 29 0a 09 09  |{#hexval}+1))...|
000002f0  63 6f 75 6e 74 3d 24 28  28 63 6f 75 6e 74 2b 31  |count=$((count+1|
00000300  29 29 0a 09 09 69 66 20  5b 20 24 63 6f 75 6e 74  |))...if [ $count|
00000310  20 2d 65 71 20 32 35 36  20 5d 0a 09 09 74 68 65  | -eq 256 ]...the|
00000320  6e 0a 09 09 09 65 63 68  6f 20 2d 6e 20 22 30 41  |n....echo -n "0A|
00000330  20 22 0a 09 09 66 69 0a  09 64 6f 6e 65 0a 64 6f  | "...fi..done.do|
00000340  6e 65 20 3c 20 24 31 0a  49 46 53 3d 22 24 69 66  |ne < $1.IFS="$if|
00000350  73 5f 73 74 72 22 0a 65  78 69 74 0a              |s_str".exit.|
0000035c
23 21 2F 62 69 6E 2F 62 61 73 68 0A 23 20 68 65 78 2E 73 68 20 3C 2F 66 75 6C 
6C 2F 70 61 74 68 2F 74 6F 2F 66 69 6C 65 6E 61 6D 65 3E 0A 69 66 73 5F 73 74 
72 3D 22 24 49 46 53 22 0A 49 46 53 3D 22 22 0A 66 6F 72 20 68 69 67 68 5F 6E 
69 62 62 6C 65 20 69 6E 20 30 20 31 20 32 20 33 20 34 20 35 20 36 20 37 20 38 
20 39 20 41 20 42 20 43 20 44 20 45 20 46 0A 64 6F 0A 09 66 6F 72 20 6C 6F 77 
5F 6E 69 62 62 6C 65 20 69 6E 20 30 20 31 20 32 20 33 20 34 20 35 20 36 20 37 
20 38 20 39 20 41 20 42 20 43 20 44 20 45 20 46 0A 09 64 6F 0A 09 09 65 63 68 
6F 20 2D 65 20 2D 6E 20 22 5C 78 24 68 69 67 68 5F 6E 69 62 62 6C 65 24 6C 6F
 77 5F 6E 69 62 62 6C 65 22 20 3E 20 2F 74 6D 70 2F 24 68 69 67 68 5F 6E 69 62
 62 6C 65 24 6C 6F 77 5F 6E 69 62 62 6C 65 27 2E 48 45 58 27 0A 09 64 6F 6E 65
 0A 64 6F 6E 65 0A 6C 69 73 74 69 6E 67 3D 24 28 6C 73 20 2F 74 6D 70 2F 2A 2E
 48 45 58 29 0A 63 6F 75 6E 74 3D 30 0A 63 68 61 72 3D 22 22 0A 68 65 78 76 61
 6C 3D 22 2F 74 6D 70 2F 30 30 2E 48 45 58 22 0A 73 75 62 73 63 72 69 70 74 3D
 30 0A 63 68 72 3D 22 22 0A 68 65 78 64 75 6D 70 20 2D 43 20 24 31 0A 77 68 69
 6C 65 20 72 65 61 64 20 2D 73 20 2D 64 20 27 27 20 2D 72 20 2D 6E 20 31 20 63
 68 61 72 0A 64 6F 0A 09 73 75 62 73 63 72 69 70 74 3D 30 0A 09 63 6F 75 6E 74
 3D 30 0A 09 77 68 69 6C 65 20 5B 20 24 63 6F 75 6E 74 20 2D 6C 65 20 32 35 35
 20 5D 0A 09 64 6F 0A 09 09 68 65 78 76 61 6C 3D 22 24 7B 6C 69 73 74 69 6E 67
 3A 24 73 75 62 73 63 72 69 70 74 3A 24 7B 23 68 65 78 76 61 6C 7D 7D 22 0A 09
 09 63 68 72 3D 24 28 63 61 74 20 24 68 65 78 76 61 6C 29 0A 09 09 69 66 20 5B
 20 22 24 63 68 61 72 22 20 3D 3D 20 22 24 63 68 72 22 20 5D 0A 09 09 74 68 65
 6E 0A 09 09 09 69 66 20 5B 20 22 24 63 68 72 22 20 3D 3D 20 22 22 20 5D 0A 09
 09 09 74 68 65 6E 0A 09 09 09 09 65 63 68 6F 20 2D 6E 20 22 30 30 20 22 0A 09
 09 09 09 62 72 65 61 6B 0A 09 09 09 66 69 0A 09 09 09 69 66 20 5B 20 22 24 63
 68 72 22 20 21 3D 20 22 22 20 5D 0A 09 09 09 74 68 65 6E 0A 09 09 09 09 65 63
 68 6F 20 2D 6E 20 22 24 7B 68 65 78 76 61 6C 3A 24 28 28 24 7B 23 68 65 78 76
 61 6C 7D 2D 36 29 29 3A 32 7D 20 22 0A 09 09 09 09 62 72 65 61 6B 0A 09 09 09
 66 69 0A 09 09 66 69 0A 09 09 73 75 62 73 63 72 69 70 74 3D 24 28 28 73 75 62
 73 63 72 69 70 74 2B 24 7B 23 68 65 78 76 61 6C 7D 2B 31 29 29 0A 09 09 63 6F
 75 6E 74 3D 24 28 28 63 6F 75 6E 74 2B 31 29 29 0A 09 09 69 66 20 5B 20 24 63
 6F 75 6E 74 20 2D 65 71 20 32 35 36 20 5D 0A 09 09 74 68 65 6E 0A 09 09 09 65
 63 68 6F 20 2D 6E 20 22 30 41 20 22 0A 09 09 66 69 0A 09 64 6F 6E 65 0A 64 6F
 6E 65 20 3C 20 24 31 0A 49 46 53 3D 22 24 69 66 73 5F 73 74 72 22 0A 65 78 69
 74 0A AMIGA:barrywalker~/Desktop/Code/Shell> 
AMIGA:barrywalker~/Desktop/Code/Shell> _

This User Gave Thanks to wisecracker For This Post:
# 2  
Old 01-11-2015
Nice one.

I'm quite confused that the 'difficult', as in structured, output is printed increadible fast, where as the 'long single string' takes that long.

I tried a bit and.. well you not gonna like it...
I actualy just commented out all your translating, so the script ends alot faster.
Code:
#!/bin/bash
# hex.sh </full/path/to/filename>
# Barry Walker @ https://www.unix.com/unix-for-advanced-and-expert-users/254451-hexdumper-using-echo-only.html
#

....
while read -s -d '' -r -n 1 char
do
	subscript=0
	count=0
	while [ $count -le 255 ]
	do
		...
		count=$((count+1))
		if [ $count -eq 256 ]
		then
			#echo -n "0A "
			break
		fi
	done
done < $1
IFS="$ifs_str"
exit


What i do not understand is, how do you get it formated in the first place?
Is that read doing it?
Not sure if this could be displayed on a moblie phone display.

Code:
time hex.sh blubber.sh ;ll blubber.sh 

00000000  23 2f 62 69 6e 2f 62 61  73 68 0a 23 0a 23 09 46  |#/bin/bash.#.#.F|
00000010  75 6e 63 74 69 6f 6e 0a  23 0a 09 6e 75 6d 32 72  |unction.#..num2r|
00000020  6f 6d 61 6e 28 29 20 7b  20 23 20 4e 55 4d 0a 09  |oman() { # NUM..|
00000030  23 20 52 65 74 75 72 6e  73 20 4e 55 4d 20 69 6e  |# Returns NUM in|
00000040  20 72 6f 6d 61 6e 20 6c  65 74 74 65 72 73 0a 09  | roman letters..|
00000050  23 0a 09 09 69 6e 70 75  74 3d 24 31 09 23 20 69  |#...input=$1.# i|
00000060  6e 70 75 74 20 6e 75 6d  0a 09 09 6f 75 74 70 75  |nput num...outpu|
00000070  74 3d 22 22 09 23 20 43  6c 65 61 72 20 6f 75 74  |t="".# Clear out|
00000080  70 75 74 20 73 74 72 69  6e 67 0a 09 09 6c 65 6e  |put string...len|
00000090  3d 24 7b 23 69 6e 70 75  74 7d 09 23 20 49 6e 69  |=${#input}.# Ini|
000000a0  74 69 61 6c 20 6c 65 6e  67 74 68 20 74 6f 20 63  |tial length to c|
000000b0  6f 75 6e 74 20 64 6f 77  6e 0a 09 09 0a 09 09 72  |ount down......r|
000000c0  6f 6d 61 6e 5f 76 61 6c  28 29 20 7b 20 23 20 4e  |oman_val() { # N|
000000d0  55 4d 20 6f 6e 65 20 66  69 76 65 20 74 65 6e 0a  |UM one five ten.|
000000e0  09 09 23 20 54 68 69 73  20 73 75 62 20 64 6f 65  |..# This sub doe|
000000f0  73 20 74 68 65 20 62 61  73 69 63 20 27 72 6f 6d  |s the basic 'rom|
00000100  61 6e 27 20 61 6c 67 6f  72 79 74 68 6d 0a 09 09  |an' algorythm...|
00000110  23 0a 09 09 09 4e 3d 24  31 0a 09 09 09 6f 6e 65  |#....N=$1....one|
00000120  3d 24 32 0a 09 09 09 66  69 76 65 3d 24 33 0a 09  |=$2....five=$3..|
00000130  09 09 74 65 6e 3d 24 34  0a 09 09 09 6f 75 74 3d  |..ten=$4....out=|
00000140  22 22 0a 09 09 09 0a 09  09 09 63 61 73 65 20 24  |""........case $|
00000150  4e 20 69 6e 0a 09 09 09  30 29 09 6f 75 74 2b 3d  |N in....0).out+=|
00000160  22 22 09 3b 3b 0a 09 09  09 5b 31 32 33 5d 29 09  |"".;;....[123]).|
00000170  77 68 69 6c 65 20 5b 5b  20 24 4e 20 2d 67 74 20  |while [[ $N -gt |
00000180  30 20 5d 5d 0a 09 09 09  09 64 6f 09 6f 75 74 2b  |0 ]].....do.out+|
00000190  3d 22 24 6f 6e 65 22 0a  09 09 09 09 09 4e 3d 24  |="$one"......N=$|
000001a0  28 28 24 4e 2d 31 29 29  0a 09 09 09 09 64 6f 6e  |(($N-1)).....don|
000001b0  65 0a 09 09 09 09 3b 3b  0a 09 09 09 34 29 09 6f  |e.....;;....4).o|
000001c0  75 74 2b 3d 22 24 6f 6e  65 24 66 69 76 65 22 09  |ut+="$one$five".|
000001d0  3b 3b 0a 09 09 09 35 29  09 6f 75 74 2b 3d 22 24  |;;....5).out+="$|
000001e0  66 69 76 65 22 09 3b 3b  0a 09 09 09 5b 36 37 38  |five".;;....[678|
000001f0  5d 29 09 6f 75 74 2b 3d  22 24 66 69 76 65 22 0a  |]).out+="$five".|
00000200  09 09 09 09 4e 3d 24 28  28 24 4e 2d 35 29 29 0a  |....N=$(($N-5)).|
00000210  09 09 09 09 77 68 69 6c  65 20 5b 5b 20 24 4e 20  |....while [[ $N |
00000220  2d 67 74 20 30 20 5d 5d  0a 09 09 09 09 64 6f 09  |-gt 0 ]].....do.|
00000230  6f 75 74 2b 3d 22 24 6f  6e 65 22 0a 09 09 09 09  |out+="$one".....|
00000240  09 4e 3d 24 28 28 24 4e  2d 31 29 29 0a 09 09 09  |.N=$(($N-1))....|
00000250  09 64 6f 6e 65 0a 09 09  09 09 3b 3b 0a 09 09 09  |.done.....;;....|
00000260  39 29 09 77 68 69 6c 65  20 5b 5b 20 24 4e 20 2d  |9).while [[ $N -|
00000270  6c 74 20 31 30 20 5d 5d  0a 09 09 09 09 64 6f 09  |lt 10 ]].....do.|
00000280  6f 75 74 2b 3d 22 24 6f  6e 65 22 0a 09 09 09 09  |out+="$one".....|
00000290  09 4e 3d 24 28 28 24 4e  2b 31 29 29 0a 09 09 09  |.N=$(($N+1))....|
000002a0  09 64 6f 6e 65 0a 09 09  09 09 6f 75 74 2b 3d 22  |.done.....out+="|
000002b0  24 74 65 6e 22 0a 09 09  09 09 3b 3b 0a 09 09 09  |$ten".....;;....|
000002c0  65 73 61 63 0a 09 09 09  65 63 68 6f 20 24 6f 75  |esac....echo $ou|
000002d0  74 0a 09 09 7d 0a 09 09  0a 09 09 77 68 69 6c 65  |t...}......while|
000002e0  20 5b 5b 20 24 6c 65 6e  20 2d 67 74 20 30 20 20  | [[ $len -gt 0  |
000002f0  5d 5d 0a 09 09 64 6f 09  23 20 54 68 65 72 65 20  |]]...do.# There |
00000300  61 72 65 20 6c 65 74 74  65 72 73 20 74 6f 20 61  |are letters to a|
00000310  64 64 0a 09 09 09 6e 75  6d 3d 24 7b 69 6e 70 75  |dd....num=${inpu|
00000320  74 3a 30 3a 31 7d 0a 09  09 09 23 20 44 6f 20 61  |t:0:1}....# Do a|
00000330  63 74 69 6f 6e 20 61 63  63 6f 72 64 69 6e 67 20  |ction according |
00000340  70 6f 73 69 74 69 6f 6e  0a 09 09 09 63 61 73 65  |position....case|
00000350  20 24 6c 65 6e 20 69 6e  0a 09 09 09 31 29 09 23  | $len in....1).#|
00000360  20 31 0a 09 09 09 09 6f  75 74 70 75 74 2b 3d 22  | 1.....output+="|
00000370  24 28 72 6f 6d 61 6e 5f  76 61 6c 20 24 6e 75 6d  |$(roman_val $num|
00000380  20 49 20 56 20 58 29 22  0a 09 09 09 09 3b 3b 0a  | I V X)".....;;.|
00000390  09 09 09 32 29 09 23 20  31 30 0a 09 09 09 09 6f  |...2).# 10.....o|
000003a0  75 74 70 75 74 2b 3d 22  24 28 72 6f 6d 61 6e 5f  |utput+="$(roman_|
000003b0  76 61 6c 20 24 6e 75 6d  20 58 20 4c 20 43 29 22  |val $num X L C)"|
000003c0  0a 09 09 09 09 3b 3b 0a  09 09 09 33 29 09 23 20  |.....;;....3).# |
000003d0  31 30 30 0a 09 09 09 09  6f 75 74 70 75 74 2b 3d  |100.....output+=|
000003e0  22 24 28 72 6f 6d 61 6e  5f 76 61 6c 20 24 6e 75  |"$(roman_val $nu|
000003f0  6d 20 43 20 44 20 4d 29  22 0a 09 09 09 09 3b 3b  |m C D M)".....;;|
00000400  0a 09 09 09 2a 29 09 23  20 31 30 30 30 2b 0a 09  |....*).# 1000+..|
00000410  09 09 09 23 20 31 30 27  30 30 30 20 67 65 74 73  |...# 10'000 gets|
00000420  20 61 20 6c 69 6e 65 20  61 62 6f 76 65 2c 20 31  | a line above, 1|
00000430  30 30 27 30 30 30 20 67  65 74 73 20 61 20 6c 69  |00'000 gets a li|
00000440  6e 65 20 6f 6e 20 74 68  65 20 6c 65 66 74 2e 2e  |ne on the left..|
00000450  20 68 6f 77 20 74 6f 3f  0a 09 09 09 09 6e 75 6d  | how to?.....num|
00000460  3d 24 7b 69 6e 70 75 74  3a 30 3a 28 2d 33 29 7d  |=${input:0:(-3)}|
00000470  0a 09 09 09 09 77 68 69  6c 65 20 5b 5b 20 24 6e  |.....while [[ $n|
00000480  75 6d 20 2d 67 74 20 30  20 5d 5d 0a 09 09 09 09  |um -gt 0 ]].....|
00000490  64 6f 09 6f 75 74 70 75  74 2b 3d 22 4d 22 0a 09  |do.output+="M"..|
000004a0  09 09 09 09 6e 75 6d 3d  24 28 28 24 6e 75 6d 2d  |....num=$(($num-|
000004b0  31 29 29 0a 09 09 09 09  64 6f 6e 65 0a 09 09 09  |1)).....done....|
000004c0  09 0a 09 09 09 09 3b 3b  0a 09 09 09 65 73 61 63  |......;;....esac|
000004d0  0a 09 09 09 69 6e 70 75  74 3d 24 7b 69 6e 70 75  |....input=${inpu|
000004e0  74 3a 31 7d 20 3b 20 6c  65 6e 3d 24 7b 23 69 6e  |t:1} ; len=${#in|
000004f0  70 75 74 7d 0a 09 09 64  6f 6e 65 0a 09 09 65 63  |put}...done...ec|
00000500  68 6f 20 24 6f 75 74 70  75 74 0a 09 7d 0a 23 0a  |ho $output..}.#.|
00000510  23 09 43 61 6c 6c 20 69  74 0a 23 0a 09 6e 75 6d  |#.Call it.#..num|
00000520  32 72 6f 6d 61 6e 20 24  31                       |2roman $1|
00000529

real	0m2.539s
user	0m3.583s
sys	0m0.066s
-rw-rw-r--. 1 sea sea 1.3K  9. Dez 12:27 blubber.sh

# 3  
Old 01-11-2015
Hi sea..

Not quite sure what you are doing as a newline character is difficult to __read__ using read.

Binary 0, (zero), is another difficult task along with tab and space. Tab and space are relatively easy but binary 0x00 and 0x0A using echo was NOT at all easy. It IS now though... ;oD

With the printf command things become relatively simple and binary can be read into HEX values with relative ease.

BUT, and this is a big but, echo is a different animal when printf does NOT exist.

The echo commands ARE needed to transfer trenslated data into say another file or into a variable. Translating a binary file to HEX is seriously difficult using just the echo command so brute force was really the only way using part of the filenames as the HEX value, by comparing the real binaries against the contents of these filenames... <wink>

I am still technically a novice at shell scripting but my pet love is handling pure binary and banging the metal and I consider myself semi-pro at this level.

This was promoted from beginners to advanced by one of the big guns on here and might be worth a read about binary 0x00:-

Another binary manipulation thread.
# 4  
Old 01-11-2015
Just figured the red marked dots within the loop might be misleading...
The 'skiped' content there was deleted.

If that doesnt help to clear up, we're at the same stage of question. Smilie
# 5  
Old 01-11-2015
Hi sea...

Aha, now I see what you are doing...

MY QUOTE:-
Quote:
LBNL the first display is a random file generated to show it works in binary mode and the second is a hexdump of itself.
Note that hexdump is there for comparison only...
hexdump inside the code is there purely to prove that the HEX created but the complex code is the same by comparing each value...

Android does NOT have the od, xxd or hexdump hex dumpers and hexdump inside this DEMO instance needs to be removed. Remember it is purely for comparison purposes.

As it stands it is purely a DEMO to show that binary can be read using what Android has as default...

As I said it IS SLOW but does what it says on the tin...

I only need to read a single byte for my usage and this works just fine.

Next is to make it pipe-able, have switches to display like hexdump -C filename ...

Dunno if any of this is possible for Android but I am willing to try... <wink>
# 6  
Old 01-11-2015
Ahh lol, now that you say it, i see it.

Checked when i just got up, and probably just overseen that line due to all those capital HEX's.
No pun intended, as i would name it the same Smilie

Seeing what you have already done with cygwin should give you confidence.
Good luck
# 7  
Old 01-22-2015
Right guys...

This is the finished hex dumper for my new Android phone and it is running just great.
Believe it or not it is marginally quicker than the OSX version... <shock>
Code:
#!/system/bin/sh
# [./]hex </full/path/to/filename> ['delimiter_character']<CR>
if [ "$1" == "" ]
then
	echo "Usage: [./]hex </full/path/to/filename> ['delimiter_character']<CR>"
	exit 1
fi
. /storage/sdcard0/Home/bin/ENVARC
ifs_str="$IFS"
IFS=""
if [ ! -f $TMPDIR/00.HEX ]
then
	for high_nibble in 0 1 2 3 4 5 6 7 8 9 A B C D E F
	do
		for low_nibble in 0 1 2 3 4 5 6 7 8 9 A B C D E F
		do
			echo -e -n "\x$high_nibble$low_nibble" > $TMPDIR/$high_nibble$low_nibble'.HEX'
		done
	done
fi
listing=$(ls $TMPDIR/*.HEX)
count=0
char=""
hexval="$TMPDIR/00.HEX"
subscript=0
chr=""
while read -s -d '' -r -n 1 char
do
	subscript=0
	count=0
	while [ $count -le 255 ]
	do
		hexval="${listing:$subscript:${#hexval}}"
		chr=$(cat $hexval)
		if [ "$char" == "$chr" ]
		then
			if [ "$chr" == "" ]
			then
				echo -n "00$2"
				break
			fi
			if [ "$chr" != "" ]
			then
				echo -n "${hexval:$((${#hexval}-6)):2}$2"
				break
			fi
		fi
		subscript=$((subscript+${#hexval}+1))
		count=$((count+1))
		if [ $count -eq 256 ]
		then
			echo -n "0A$2"
		fi
	done
done < $1
IFS="$ifs_str"
exit 0

The absolute address(es) I had to create manually...
ENVARC is this file too...
Note that TMPDIR does not exist in Android so why not use it... ;o)
Code:
#!/system/bin/sh
# ENVARC
PATH=$PATH:/storage/sdcard0/Home/bin
TMPDIR=/storage/sdcard0/Home/tmp

Also tr and dos2unix do not exist either so this is another tool for the phone generated from minimal Android tools...
This is a dos2unix work-alike...
Code:
#!/system/bin/sh
# [./]d2u <infile> [outfile]<CR>
. /storage/sdcard0/Home/bin/ENVARC
ifs_str="$IFS"
IFS=""
newfile=""
substring=0
outfile=$2
if [ "$1" == "" ]
then
	echo "Usage: [./]d2u <infile> [outfile]..."
	exit 1
fi
if [ "$2" == "" ]
then
	outfile=$1
fi
read -s -d '' -r oldfile < $1
while [ $substring -lt ${#oldfile} ]
do
	if [ "${oldfile:$substring:1}" == $'\r' ]
	then
		substring=$((substring+1))
	else
		newfile=$newfile${oldfile:$substring:1} 
		substring=$((substring+1))
	fi
done
IFS="$ifs_str"
echo -n "$newfile" > $outfile
echo "File, $1, now converted to UNIX format..."
exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python hexdumper with a difference.

Hi guys... I haven't done any real Python code since I started messing with Shell Scripting in January 2012 when I started AudioScope. Well I fished a little project out of my ANCIENT AMIGA files which created a raw hexdump of any binary file within reason for a stock A1200(HD). Well... (0 Replies)
Discussion started by: wisecracker
0 Replies

2. UNIX for Dummies Questions & Answers

Difference between echo `ls -l` and echo "`ls -l`" ?

Hi guys, Been messing around with shell programming for a couple of days and I found something that was pretty odd in the behavior of the echo command. Below is an example-: When I type the following in my /home directory from my lxterminal in Debian-: echo "`ls -l`" I get the... (2 Replies)
Discussion started by: sreyan32
2 Replies

3. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

4. Shell Programming and Scripting

With that logic this echoes "echo". Question about echo!

echo `echo ` doesn't echoes anything. And it's logic. But echo `echo `echo ` ` does echoes "echo". What's the logic of it? the `echo `echo ` inside of the whole (first) echo, echoes nothing, so the first echo have to echo nothing but echoes "echo" (too much echoing :P):o (2 Replies)
Discussion started by: hakermania
2 Replies

5. Shell Programming and Scripting

Help with echo

Hi Guys, I need to print a value in the same line , But when we use the echo instead the loops (while), the value goes to the next line.. Can you help me in this.. Thanks For your help in advance. (6 Replies)
Discussion started by: mac4rfree
6 Replies

6. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

7. Shell Programming and Scripting

Using Echo

Hi All, Need help in resoving the issue . Can anyone let me know how to get fix length using unix shellscript. For Ex: echo "NUMBER OF RECORDS "${NO_OF_ROWS}"\nFILE CREATION DATE&TIME "${PROD_DT}" output should be : NUMBER OF RECORDS 2546 CREATIN DATE&TIME 2009-12-01 Each... (14 Replies)
Discussion started by: Samtel
14 Replies

8. Shell Programming and Scripting

Difference between using "echo" builtin and /bin/echo

So in my shell i execute: { while true; do echo string; sleep 1; done } | read line This waits one second and returns. But { while true; do /bin/echo string; sleep 1; done } | read line continues to run, and doesn't stop until i kill it explicitly. I have tried this in bash as well as zsh,... (2 Replies)
Discussion started by: ulidtko
2 Replies

9. UNIX for Advanced & Expert Users

What does echo ^[[r do?

I tried echo ^[[r and it takes the cursor to first line in the telnet window... and echo ^[[32m changes font color to green .... Also echo ^[[00ff takes cursor to first command line in the current window (Note : ^ is entered as Ctrl + V and first [ as Escape character) Wud like to know how... (1 Reply)
Discussion started by: hamsasal
1 Replies

10. UNIX for Dummies Questions & Answers

echo with \n

Hi, When I do /usr/ucb/echo "message \n" in bone shell program , \n never works as new line feeder. It just showed it as part of my message. Is there something wrong with my system? Thanks! (3 Replies)
Discussion started by: whatisthis
3 Replies
Login or Register to Ask a Question