Convert from ASCII string encoded in Hex to plain ASCII?
A slightly simpler solution:
>>> "7061756c".decode("hex")
'paul'
How to convert Hex to ASCII
There's a hex
package in the standard library that can decode hex into bytes. If it's valid utf-8 (which all ASCII is), you can display it as a string.
Here it is in action:
package main
import (
"encoding/hex"
"fmt"
)
func main() {
a := "73616d706c65"
bs, err := hex.DecodeString(a)
if err != nil {
panic(err)
}
fmt.Println(string(bs))
}
The output is "sample", which you can see on the playground.
How to convert hex to ASCII while preserving non-printable characters
xxd
expects two characters per byte. One A
is invalid. Do:
printf '%02X' 10 | xxd -r -p | xxd -p
How to convert hex to ASCII while preserving non-printable characters
Use xxd
. If your input has one character, pad it with an initial 0
.
ASCII does not preserve non-printable characters
It does preserve any bytes, xxd
is the common tool to work with any binary data in shell.
Is it possible to preserve these characters somehow?
Yes - input sequence of two characters per byte to xxd
.
How to convert Hex string with address and ASCII values to binary file using Powershell?
Going by your comment I want to convert the hex to .bin file.
There are many applications that can do a hexdump of a binary file. PowerShell has the Format-Hex cmdlet for that, but it doesn't create output as you show in your question.
Format-Hex starts with a byte count header line
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
then an empty line and after that the hex data.
It also adds a byte counter left margin before the actual data.
Your example doesnt do that, but instead adds a hyphen in between every eight bytes, so it is obviously not created using Format-Hex.
Anyway, here's how you can recreate a binary file from both types of hexdump output:
$inputFile = 'D:\Test\yourHexdump.txt'
$outputFile = 'D:\Test\yourBinaryFile.bin' # use absolute full path here
# if the output file already exists, delete it
if (Test-Path -Path $outputFile -PathType Leaf) { Remove-Item -Path $outputFile -Force }
# create a FileStream object to write the converted bytes to file
$stream = [System.IO.FileStream]::new($outputFile, [System.IO.FileMode]::Append, [IO.FileAccess]::Write)
# now loop through the file line-by-line and parse out the bytes
switch -Regex -File $inputFile {
# if this is from Format-Hex output, ignore the 1-16 byte header and empty line(s)
'^\s+00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|^\s*$' {}
default {
# if this is from Format-Hex output, remove the byte counters in the left margin
$line = if ($_ -match '^[0-9A-f]{8}\s') { $_.Substring(8) } else { $_ }
# convert the hex values into an array of bytes
[byte[]]$bytes = (($line.TrimStart() -split '\s{2,}')[0].Trim() -replace '-' -split '\s+' -ne '' -replace '^', '0X')
# write to file
$stream.Write($bytes, 0, $bytes.Count)
}
}
# dispose of the stream
$stream.Flush() # probably Dispose() also flushes..
$stream.Dispose()
Related Topics
Case Insensitive Flask-Sqlalchemy Query
How to Get Element-Wise Matrix Multiplication (Hadamard Product) in Numpy
Any Reason Not to Use '+' to Concatenate Two Strings
Pip Connection Failure: Cannot Fetch Index Base Url Http://Pypi.Python.Org/Simple/
Pyspark: Explode JSON in Column to Multiple Columns
Installing Module from Github Through Jupyter Notebook
Python: Nameerror: Global Name 'Foobar' Is Not Defined
Convert Bytes to Bits in Python
Get Loop Count Inside a For-Loop
Activate Python Virtualenv in Dockerfile
Why Is the Empty Dictionary a Dangerous Default Value in Python