Hot answers tagged

35

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


34

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") ...


21

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


18

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


15

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 ...


12

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


12

Using printf: $ printf '%02x ' {0..255} The format string %02x says to format the output as a zero-filled, two-digit, lower-case, hexadecimal number. If you want upper-case, use %02X. Bash only understands base 10 integer ranges or alphabetical character ranges in brace expansions of intervals.


10

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, ...


9

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

If you just want to view changes, not edit them, you can convert the files to hex with one program and then diff the output with any graphical diff program you want. It is probably only practical if there are only changed (not inserted) bytes between the files. As a one-liner: meld <(hexdump -C file1.bin) <(hexdump -C file2.bin) And here's a ...


8

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


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'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 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. ...


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

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. ...


5

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


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: ...


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'


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

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

e4 75 is indeed an illegal utf8 sequence. In utf8, a byte with the highest nibble equal to 0xe introduces a three byte sequence. The second byte of such a sequence cannot be 0x75, because the high order nibble of that second byte (0x7) is not between 0x8 and 0xb. This explains why iconv rejects that file as invalid utf8. Perhaps it's already iso8859-1? For ...


4

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


4

perl -nE 'say reverse /(..)/g' This reverts each hexadecimal line: /(..)/g buils a list with the captured matches


4

(for the sake of completeness) $ echo 030201 | grep -o .. | tac | paste -sd '' - 010203


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