Hot answers tagged

51

With bash (or any shell, provided the printf command is available (a standard POSIX command often built in the shells)): printf '%x\n' 85 ​​​​​​​​​​​​​​​​​ With zsh, you can also do: dec=85 hex=$(([##16]dec)) That works for bases from 2 to 36 (with 0-9a-z case insensitive as the digits). With ksh93, you can use: dec=85 base54=${ printf %..54 "$dec&...


36

Like this: echo $(( 0xA ^ 0xF )) Or if you want the answer in hex: printf '0x%X\n' $(( 0xA ^ 0xF )) On a side note, calc(1) does support xor as a function: $ calc base(16) 0xa xor(0x22, 0x33) 0x11


22

With any POSIX shell: $ printf '%#x\n' "$((0x11 ^ 0x22))" 0x33


21

You can; you just need to break the range {0..F} into two separate ranges {0..9} and {A..F}: $ printf '%s\n' {{0..9},{A..F}}{{0..9},{A..F}} 00 01 ... FE EF


18

Why the second command outputs a different value? For historical reasons, dd considers x to be a multiplication operator. So 0x3 is evaluated to be 0. Is it possible to pass the skip|seek offset to dd as an hexadecimal value? Not directly, as far as I know. As well as multiplication using the operator x, you can suffix any number with b to mean "multiply ...


15

Using printf: $ printf '%.2x\n' {0..255} The format string %.2x says to format the output as a zero-filled, two-digit, lower-case, hexadecimal number (%02x would have done the same). If you want upper-case, use %.2X. Bash only understands base 10 integer ranges or ranges between ASCII characters in brace expansions of intervals.


14

gdb has powerful expression calculator: gdb -q -ex 'print/x 0xA ^ 0xF' -ex q A shell function: calc_gdb() { gdb -q -ex "print/x $*" -ex q;} calc_gdb 0xA ^ 0xF $1 = 0x5


14

You could hexify D83ACD2E, pack it into a (Network byte order!) 32-bit integer, then print the (unsigned!) character components of that integer joined by dots. (This is also possible if somewhat more verbose in assembly.) $ perl -e 'printf "%v*d\n", ".", pack "N", hex shift' D83ACD2E 216.58.205.46 $ With fewer complications the decimal flag to gethostip ...


11

You can convert it to binary, reverse the bytes, optionally remove trailing newlines rev <2.24, and convert it back: $ xxd -revert -plain <<< 030201 | LC_ALL=C rev | tr -d '\n' | xxd -plain 010203 Using $ bash --version | head -n1 GNU bash, version 4.3.42(1)-release (x86_64-redhat-linux-gnu) $ xxd -version xxd V1.10 27oct98 by Juergen Weigert $...


10

If your system has a rev command. hex=030201 new_hex=$(printf %s "$hex" | dd conv=swab 2> /dev/null | rev) If it has a tac or tail -r command: new_hex=$(echo "$hex" | fold -w 2 | tac | paste -sd '\0' -) With zsh: setopt extendedglob new_hex=${(j::)${(s::Oa)${hex//(#b)(?)(?)/$match[2]$match[1]}}} (like in the dd approach: swap pairs of characters, ...


10

With fold + tac + tr : $ echo 030201|fold -w2|tac|tr -d "\n" 010203 fold - split every 2 byte tac - reverse cat tr - remove newlines


8

You can extract the byte values in the text segment with: $ objcopy -O binary -j .text f.o fo The -O binary option: objcopy can be used to generate a raw binary file by using an output target of binary (e.g., use -O binary). When objcopy generates a raw binary file, it will essentially produce a memory dump of the contents of the input object file. ...


8

Probably a better way to do this but I've come up with this solution which converts the number to decimal and then back to hex (and manually adds the 0x): printf '0x%x\n' "$((16#00080000))" Which you could write as: printf '0x%x\n' "$((16#$(expr substr "$SUPERBLOCK" 64 8)))"


7

It means that one or more lines were suppressed, because they are identical to the previous line; in this case, it means that the line starting at 00001e0 is all zeroes, same as that starting at 00001d0. To determine the number of deleted lines, you need to look at the addresses involved and the length of each line; in this case, a single line was deleted. ...


7

It's possible but it isn't nice: echo {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F} As far as I can tell bash has no notion of hex ranges.


7

You may be able to use glibc's getent here: $ getent ahostsv4 0xD83ACD2E | { read ip rest && getent hosts "$ip"; } 216.58.205.46 mil04s24-in-f46.1e100.net Another perl approach: $ perl -MSocket -le '($n)=gethostbyaddr(inet_aton("0xD83ACD2E"), AF_INET); print $n' mil04s24-in-f46.1e100.net


6

Use printf: $ printf "%d %x\n" $((16#55)) $((10#85)) 85 55 To assign the value to a variable use command substitution: $ x=$( printf "%x" 85 ) ; echo $x 55


6

This should do: for i in {1..5}; do printf '#%s/Z\n' "$(openssl rand -hex 4)" done >passwords.txt I replaced the multiple calls to echo with a single call to printf. Having the call to openssl wrapped inside a command substitution has the side effect of making the line ending disappear, and that newline character was the cause of the badly-placed ...


5

Bash has a printf builtin, which can around the same as we could learn in C. The syntax a little bit differs. printf '\x2f' If you don't need to worry about higher-level data consistency problems, you can simply convert an url by this function: function deUrl() { printf "${1//%/\\x}" } (It converts every % to a \x, then prints it with printf.)


5

Or just perform it within xxd itself ? $ xxd -l 32 2.png && xxd -s -32 2.png 0000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452 .PNG........IHDR 0000010: 0000 0182 0000 018b 0806 0000 00ca f595 ................ 000a1b3: 8c73 a854 7b3e b0fe 3526 fd03 d868 7f6e .s.T{>..5&...h.n 000a1c3: 763e 9a4e 0000 0000 4945 4e44 ae42 6082 v>.N....IEND....


5

regex2=$'\x28' is exactly equivalent to regex2='(', the shell processes the $'...' quotes when the assignment happens. And ( by itself is an invalid regex, so [[ =~ ]] reports an error by returning an exit status of 2: $ re='('; [[ "(" =~ $re ]]; echo "$?" 2 (Of course within an if statement you can't tell the difference between an exit code of 1 for "no ...


5

There are two more or less standard (and ancient) command-line unix tools that offer very easy ways to convert numbers between different bases: $ { echo '16'; echo i; echo 00080000; echo p; } | dc 524288 $ { echo 'ibase=16'; echo 00080000; } | bc 524288 For normal human use I very much prefer bc, but when writing a program that generates code, especially ...


5

sed -r 'H;$!d;x;s:\n::g;:l;s:(\\x..)(.*)\1:\2:;tl' allHexChars.txt allowedChars.txt > missingChars.txt The above GNU sed script assumes two things as I understood them from the question: inside the files no hex character is listed more than one time the first file contains all the hex characters from the second file To visualize the differences, use: ...


5

The standard naming practice for executables is to give them the name of the command they’re supposed to implement: ls, cat... There is no provision for extensions which end up ignored from the command line. To check what a file contains before feeding it to cat, run file on it: $ file /bin/ls /bin/ls: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV)...


4

You're running od on a little-endian machine. >>> 0x1f1b 7963


4

Really easy: Add the hex specification "\x" each two characters. Use echo -e for hex to be interpreted. Also you can use printf because echo does different things in different shells. Using your same example data: echo -e "\xff\xff\xff\xff\xff\xff\x00\x17\x31\x3f\xd3\xa9\x00\x17\x31\x3f\xd3\xa9\x00\x17\x31\x3f\xd3\xa9\x00\x17\x31\x3f\xd3\xa9\x00\x17\x31\...


4

Such entities can be decoded with this python one-liner: $ python -c "import urllib, sys; print urllib.unquote(sys.argv[1])" "%2f" / The code is not limited to single characters. It will accept more complex strings: $ python -c "import urllib, sys; print urllib.unquote(sys.argv[1])" "%2d and %2f" - and / Python's urllib.unquote is documented here.


4

The problem is that openssl is being 'helpful' and placing a newline at the end of the random string. You can remove that by piping it through tr openssl rand -hex 4 | tr -d '\n'


Only top voted, non community-wiki answers of a minimum length are eligible