Converting binary 2 string is corrupting my bytes! how do I just get a hex rep of a binary file!
I want hex as in [2A,34,5B] or [0x2A,0x34,0x5B] Anyone know how I would do this?
It's all there in strformat, if you can get through the docs.
import std/strformat
let foo = 3735928559
echo fmt("{foo:X}")
stdout.write('[')
for i, n in [42,52,91]:
if i > 0: stdout.write(',')
stdout.write(fmt("{n:#X}"))
echo ']'
# DEADBEEF
# [0x2A,0x34,0x5B]
There is toHex in strutils: https://nim-lang.org/docs/strutils.html#toHex%2Cstring
You'll have to chunk the resulting string into 2-character strings yourself though, which you can do with eg countUp.
Getting a byte from the file is just reading it. There's a bunch of ways to do it, most of them involve some type of read proc.
func genByteToCharLUT(): array[byte, array[2, char]] =
const HexChars = "0123456789ABCDEF"
for i in 0.byte .. 255:
result[i] = [HexChars[i shr 4], HexChars[i and 0xF]]
const
byteToChars = genByteToCharLut()
bufSize = 128
var
buf: array[128, byte]
f: File
if f.open("/tmp/FILE", bufSize = bufSize):
let n = f.readBuffer(buf.addr, bufSize)
for i in 0..<n:
discard stdout.writeBuffer(byteToChars[buf[i]].unsafeAddr, 2)
echo("")
f.close()
Check your results with od -N 128 -c -t x1 /tmp/FILE
Example of the first part of my problem:
Expected bytes
BIMG010a���###$$$%%%"""!!!��
What I got from toHex(And back again)
BIMG010aººº###$$$%%%"""!!!ºï
It is completely broken. Please help.
Conversion script:
proc toHexSeq(str:string): seq[string] =
var hex = toSeq(str.toHex.items)
var output: seq[string] = @[]
for i, x in enumerate(hex):
if (i mod 2) > 0:
output.add(hex[i-1] & x)
return output
Thanks,
It is completely broken. Please help.
It's not, maybe check how you compare the bytes.
import std/[sequtils, strutils]
proc toHexSeq(str: string): seq[string] =
let hex = str.toHex()
for i, x in hex:
if (i mod 2) > 0:
result.add(hex[i-1] & x)
# Put your bytes in a `string.txt` next to your source file
const s = staticRead("string.txt")
echo s
var outstr: string
for bs in s.toHexSeq:
let b = bs.parseHexStr()
outstr.add(b)
stdout.write(b)
echo ""
doAssert s == outstr
Also, if you're going to work with bytes, it's better to lean onto the type system and keep them as byte, and only convert to char/string as necessary for user output.
In flatty i have hex print which I use for debugging binary files:
Sentence:
0000000000000000: 48 69 20 68 6F 77 20 61-72 65 20 79 6F 75 20 64 Hi how are you d
0000000000000010: 6F 69 6E 67 20 74 6F 64-61 79 3F .. .. .. .. .. oing today?
ASCII:
0000000000000000: 00 01 02 03 04 05 06 07-08 09 0A 0B 0C 0D 0E 0F ................
0000000000000010: 10 11 12 13 14 15 16 17-18 19 1A 1B 1C 1D 1E 1F ................
0000000000000020: 20 21 22 23 24 25 26 27-28 29 2A 2B 2C 2D 2E 2F !"#$%&'()*+,-./
0000000000000030: 30 31 32 33 34 35 36 37-38 39 3A 3B 3C 3D 3E 3F 0123456789:;<=>?
0000000000000040: 40 41 42 43 44 45 46 47-48 49 4A 4B 4C 4D 4E 4F @ABCDEFGHIJKLMNO
0000000000000050: 50 51 52 53 54 55 56 57-58 59 5A 5B 5C 5D 5E 5F PQRSTUVWXYZ[\]^_
0000000000000060: 60 61 62 63 64 65 66 67-68 69 6A 6B 6C 6D 6E 6F `abcdefghijklmno
0000000000000070: 70 71 72 73 74 75 76 77-78 79 7A 7B 7C 7D 7E 7F pqrstuvwxyz{|}~.
0000000000000080: 80 81 82 83 84 85 86 87-88 89 8A 8B 8C 8D 8E 8F ................
0000000000000090: 90 91 92 93 94 95 96 97-98 99 9A 9B 9C 9D 9E 9F ................
00000000000000A0: A0 A1 A2 A3 A4 A5 A6 A7-A8 A9 AA AB AC AD AE AF ................
00000000000000B0: B0 B1 B2 B3 B4 B5 B6 B7-B8 B9 BA BB BC BD BE BF ................
00000000000000C0: C0 C1 C2 C3 C4 C5 C6 C7-C8 C9 CA CB CC CD CE CF ................
00000000000000D0: D0 D1 D2 D3 D4 D5 D6 D7-D8 D9 DA DB DC DD DE DF ................
00000000000000E0: E0 E1 E2 E3 E4 E5 E6 E7-E8 E9 EA EB EC ED EE EF ................
00000000000000F0: F0 F1 F2 F3 F4 F5 F6 F7-F8 F9 FA FB FC FD FE FF ................
int16s:
0000000000000000: 00 00 01 00 02 00 03 00-04 00 05 00 06 00 07 00 ................
0000000000000010: 08 00 09 00 0A 00 0B 00-0C 00 0D 00 0E 00 0F 00 ................
0000000000000020: 10 00 .. .. .. .. .. ..-.. .. .. .. .. .. .. .. ..
int32s
0000000000000000: 00 00 00 00 E8 03 00 00-D0 07 00 00 B8 0B 00 00 ................
0000000000000010: A0 0F 00 00 88 13 00 00-70 17 00 00 58 1B 00 00 ........p...X...
0000000000000020: 40 1F 00 00 28 23 00 00-10 27 00 00 F8 2A 00 00 @...(#...'...*..
0000000000000030: E0 2E 00 00 C8 32 00 00-B0 36 00 00 98 3A 00 00 .....2...6...:..
0000000000000040: 80 3E 00 00 .. .. .. ..-.. .. .. .. .. .. .. .. .>..
See here on how to use: https://github.com/treeform/flatty/blob/master/tests/test_hexprint.nim