Linux char to hex

Convert string to hexadecimal on command line

I’m trying to convert «Hello» to 48 65 6c 6c 6f in hexadecimal as efficiently as possible using the command line. I’ve tried looking at printf and google, but I can’t get anywhere. Any help greatly appreciated. Many thanks in advance,

5 Answers 5

echo -n "Hello" | od -A n -t x1 
  • The echo program will provide the string to the next command.
  • The -n flag tells echo to not generate a new line at the end of the «Hello».
  • The od program is the «octal dump» program. (We will be providing a flag to tell it to dump it in hexadecimal instead of octal.)
  • The -A n flag is short for —address-radix=n , with n being short for «none». Without this part, the command would output an ugly numerical address prefix on the left side. This is useful for large dumps, but for a short string it is unnecessary.
  • The -t x1 flag is short for —format=x1 , with the x being short for «hexadecimal» and the 1 meaning 1 byte.

The above-outlined solution and the solution in the comments doesn’t consider line breaks that one of the tools produces when a certain amount of characters is reached.

If you want to do this and remove the spaces you need:

echo -n "Hello" | od -A n -t x1 | sed 's/ *//g' 

The first two commands in the pipeline are well explained by @TMS in his answer, as edited by @James. The last command differs from @TMS comment in that it is both correct and has been tested. The explanation is:

  • sed is a stream editor.
  • s is the substitute command.
  • / opens a regular expression — any character may be used. / is conventional, but inconvenient for processing, say, XML or path names.
  • / or the alternate character you chose, closes the regular expression and opens the substitution string.
  • In / */ the * matches any sequence of the previous character (in this case, a space).
  • / or the alternate character you chose, closes the substitution string. In this case, the substitution string // is empty, i.e. the match is deleted.
  • g is the option to do this substitution globally on each line instead of just once for each line.
  • The quotes keep the command parser from getting confused — the whole sequence is passed to sed as the first option, namely, a sed script.

@TMS brain child ( sed ‘s/^ *//’ ) only strips spaces from the beginning of each line ( ^ matches the beginning of the line — ‘pattern space’ in sed -speak).

Читайте также:  Realtek semiconductor corp driver linux

If you additionally want to remove newlines, the easiest way is to append

to the command pipes. It functions as follows:

  • | feeds the previously processed stream to this command’s standard input.
  • tr is the translate command.
  • -d specifies deleting the match characters.
  • Quotes list your match characters — in this case just newline ( \n ). Translate only matches single characters, not sequences.

sed is uniquely retarded when dealing with newlines. This is because sed is one of the oldest unix commands — it was created before people really knew what they were doing. Pervasive legacy software keeps it from being fixed. I know this because I was born before unix was born.

The historical origin of the problem was the idea that a newline was a line separator, not part of the line. It was therefore stripped by line processing utilities and reinserted by output utilities. The trouble is, this makes assumptions about the structure of user data and imposes unnatural restrictions in many settings. sed ‘s inability to easily remove newlines is one of the most common examples of that malformed ideology causing grief.

It is possible to remove newlines with sed — it is just that all solutions I know about make sed process the whole file at once, which chokes for very large files, defeating the purpose of a stream editor. Any solution that retains line processing, if it is possible, would be an unreadable rat’s nest of multiple pipes.

If you insist on using sed try:

-z tells sed to use nulls as line separators.

Internally, a string in C is terminated with a null. The -z option is also a result of legacy, provided as a convenience for C programmers who might like to use a temporary file filled with C -strings and uncluttered by newlines. They can then easily read and process one string at a time. Again, the early assumptions about use cases impose artificial restrictions on user data.

If you omit the g option, this command removes only the first newline. With the -z option sed interprets the entire file as one line (unless there are stray nulls embedded in the file), terminated by a null and so this also chokes on large files.

sed 's/^/\x00/' | sed -z 's/\n//' | sed 's/\x00//' 

might work. The first command puts a null at the front of each line on a line by line basis, resulting in \n\x00 ending every line. The second command removes one newline from each line, now delimited by nulls — there will be only one newline by virtue of the first command. All that is left are the spurious nulls. So far so good. The broken idea here is that the pipe will feed the last command on a line by line basis, since that is how the stream was built. Actually, the last command, as written, will only remove one null since now the entire file has no newlines and is therefore one line.

Читайте также:  Упаковать файл в линукс

Simple pipe implementation uses an intermediate temporary file and all input is processed and fed to the file. The next command may be running in another thread, concurrently reading that file, but it just sees the stream as a whole (albeit incomplete) and has no awareness of the chunk boundaries feeding the file. Even if the pipe is a memory buffer, the next command sees the stream as a whole. The defect is inextricably baked into sed .

To make this approach work, you need a g option on the last command, so again, it chokes on large files.

The bottom line is this: don’t use sed to process newlines.

Источник

How to convert text or character to hex in korn shell

Does the unix korn shell provide a function to convert text or character to hex eg. hex 31 32 37 33 34 35 35 36 36 37 value of characters 1273455667 Here i have written the code to display but i don’t know how to store the result in a variable

str=1273455667 for (( i=0; i < $; i++ )) do c=$ if [[ $c == ' ' ]] then printf "[%s] %X\n" " " \'\ \' else printf "[%s] %X\n" "$c" \'$c\' fi done 

4 Answers 4

If you have hexdump (which is pretty likely), you could do something like this:

$ echo "Hello, world!" | hexdump -v -e '/1 "[%_c]: "' -e '/1 "%02X\n"' [H]: 48 [e]: 65 [l]: 6C [l]: 6C [o]: 6F [,]: 2C [ ]: 20 [w]: 77 [o]: 6F [r]: 72 [l]: 6C [d]: 64 [!]: 21 [\n]: 0A 

For a simple space separated hex dump into a variable:

$ v=$(printf %s 1273455667 | hexdump -v -e '/1 "%02X "') $ echo "$v" 31 32 37 33 34 35 35 36 36 37 
str=1273455667 unset hex_values for (( i=0; i < $; i++ )); do c=$ hex_values+=($(printf "%X" "'$c")) done echo "$" 

Note that in ksh93 , printf '%X\n' "'$character" gives you the character's code point, not the hex values of the bytes that form that character in the current encoding. In that regard, it's different from od -An -vtx1 .

For instance, in UTF-8 locales, for € (encoded as e2 82 ac in UTF-8), it would give 20ac (the unicode code point for the Euro symbol), not e2 82 ac.

You can obtain the same behaviour as in od if you force the locale to C (with export LC_ALL=C ).

Here, we're using command substitution ( $(. ) ) to retrieve the output of the command. In ksh93 , when the command is built-in, that doesn't involve creating a pipe and forking a subshell, so it's relatively efficient.

Источник

Convert String to Hexadecimal on Command Line

Conversion hex string into ascii in bash command line

$ echo 54657374696e672031203220330 | xxd -r -p
Testing 1 2 3$

-r tells it to convert hex to ascii as opposed to its normal mode of doing the opposite

-p tells it to use a plain format.

bash ascii to hex

$ str="hello"
$ hex=$(xxd -pu $ echo "$hex"
6C6C6568A6F
$ hex=$(hexdump -e '"%X"' $ echo "$hex"
6C6C6568A6F

Careful with the '"%X"' ; it has both single quotes and double quotes.

Читайте также:  Find all images linux

Convert binary data to hexadecimal in a shell script

% xxd -l 16 -p /dev/random
193f6c54814f0576bc27d51ab39081dc

Convert hex string to ASCII string in Windows batch file

Use certutil tool, see certutil /? for more info.

setlocal enabledelayedexpansion
set "hex=4C6F67696300000000000000"
echo !hex!> temp.hex
call certutil -decodehex temp.hex str.txt >nul
set /p str=echo:
( del temp.hex & del str.txt )>nul
echo Your decoded string is:"!str!".
endlocal
exit /b 0

Is there any way to convert the hex code from string to hex colour which discord.py supports?

TypeError: Expected discord.Colour, int, or Embed.Empty

An int would suffice, no need for extra conversions.

@client.command()
async def testing(ctx):
# color = lgd.hexConvertor(colorCollection.find(<>,))
c = "0xFFFFFF"
int_colour = int(c,16)
await ctx.send(embed = discord.Embed(description = "testing",color = int_colour))

Ascii/Hex convert in bash

The reason is because hexdump by default prints out 16-bit integers, not bytes. If your system has them, hd (or hexdump -C ) or xxd will provide less surprising outputs - if not, od -t x1 is a POSIX-standard way to get byte-by-byte hex output. You can use od -t x1c to show both the byte hex values and the corresponding letters.

If you have xxd (which ships with vim), you can use xxd -r to convert back from hex (from the same format xxd produces). If you just have plain hex (just the '4161', which is produced by xxd -p ) you can use xxd -r -p to convert back.

DOS command to format string to hex value

Forget about Peak Oil, what about Peak DOS? It's time to look to the future. And the future is PowerShell.

Источник

printing the hex value of a character in bash

In C, printf ("%2X\n", 'A'); prints 41 . In bash, printf "%2X\n" 'A' prints bash: printf: A: invalid number . How can I print 41 using printf in bash like in C ? Please do not recommend using external tools like od because this is too slow: I'm seeking a bash-internal method.

Use printf "%X\n" \"A\" -- note, ASCII, not Unicode. See blog.fpmurphy.com/2010/12/… See also unix.stackexchange.com/questions/12273/… for converse

@DrMoishePippik What do you mean by "ASCII, not Unicode"? Do you actually mean "Unicode, not UTF-8" or so?

Thank you, @DrMoishePippik, that works, but how is that feature called ? Would you like to add an answer which I could accept ?

2 Answers 2

In printf in the bash manual, you'll find:

Arguments to non-string format specifiers are treated as C language constants, except that a leading plus or minus sign is allowed, and if the leading character is a single or double quote, the value is the ASCII value of the following character.

These shell functions encapsulate char_to_hex and hex_to_char. Function names stolen from perl

# ord: the ascii value of a character # $ ord "A" #=> 65 # ord() < printf "%d" "\"$1" ># chr: the character represented by the given ASCII decimal value # $ chr 65 #=> A # chr()
$ ord A 65 $ printf '%02X\n' "$(ord A)" 41 

Источник

Оцените статью
Adblock
detector