------------------------------------------------------------------------------- Raw Data Conversion ------------------------------------------------------------------------------- About Perl pack/unpack Perl pack/unpack is for pack/unpack to/from raw binary. As such pack 'H*' will pack a Hexadecimal String. while unpack 'H*' will convert a binary into a hexadecmal string similarly pack 'N' converts a number, 4 byte / 16 bit network number. Decimal to Hexidecimal perl -le 'print unpack("H*", pack("N", 3735928559))' # 16 bit number Hexadecimal to Decimal perl -le 'print unpack("N", pack("H8",substr("0"x8 . "DEADBEEF",-8)));' Decimal to perl -le 'print unpack("B*", pack("N", 3735928559))' # 32bit perl -le 'printf("%b\n", 223)' # variable length perl -le 'printf("%08b\n", 123)' # fixed length Binary to Decimal perl -le 'print ord(pack('B8', '10110110'))' # single character only perl -le '$b="10111"; print oct( "0b$b" )' # up to 32bit # can do larger (with warning) Raw binary string to Hexadecimal string (more in hexidecmial below) perl -0777 -e 'print unpack("H*",<>), "\n"' Other methods... Decimal to Hexadeciment (using printf) printf '0x%08X\n' 3735928559 perl -le 'printf("0x%08X\n", 3735928559)' Hexadecimal to Decimal perl -le 'print hex("DEADBEEF")' ------------------------------------------------------------------------------- About using DC You must specify input base after output base otherwise you will need to specify the output base number using the input base! Hexadecimal to Binary (not raw) hex=81 echo "2o 16i ${hex} p" | dc 10000001 Binary to Hexadecimal binary=00111100 echo "16o 2i ${binary} p" | dc 3C See.. http://wiki.bash-hackers.org/howto/calculate-dc Also http://en.wikipedia.org/wiki/Dc_%28Unix%29 ------------------------------------------------------------------------------- Hexadecimal raw binary to Hexadecimal (For viewing) hexdump - Hexadecimal to Raw binary (with cleanup) perl -pe 'y/A-Fa-f0-9//dc; $_= pack("H*",$_);' Raw binary string to Hexadecimal Lines of 64 hexchars perl -0777 -pe '$_=unpack("H*",$_); s/.{64}/$&\n/g; s/\n?$/\n/;' In C From hex void hex_to_binary(unsigned char *buf, unsigned char *hex) { for( ; sscanf( hex, "%2x", buf++ ) == 1 ; hex+=2 ); *buf = 0; // null terminate -- precaution } In C To Hex void print_hex(unsigned char *buf, int len) { int i; int n; for(i=0,n=0;i 7){ // printf("\n"); // n = 0; //} printf("%02x",buf[i]); //n++; } printf("\n"); } ------------------------------------------------------------------------------- ModHex (used by Yubikey's) Modified (substitute) Hexadecimal Reason... Keyboards do not know its keymaps! It only sends scancodes However all keyboards map certain keys to certain scancodes and these are what yubikeys use. Basically as it can only transmit certain keys so a modified hexadecimal is used to send the Yubikey output. Hexadecimal: 0123 4567 89ab cdef ModHex Code: cbde fghi jkln rtuv Further 'c' is most ambigious in keyboards, so any other key should map to '0' when reading a yubikey As such bde fghi jkln rtuv -maps-> 0x1 - 0xf And all other characters (including 'c') maps to 0x0 ------------------------------------------------------------------------------- Base64 raw binary to base64 (broken into lines) openssl enc -base64 base64 to rawbinary openssl enc -base64 -d Perl Encode perl -MMIME::Base64 -e 'print encode_base64("string")' Perl Decode perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' file Note that uuencode is almost a substitution coding of Base64 as such the perl pack for uuencode can be used for conversion. See "Programming Perl" page 237 (unpack function). Or my own implementation in "b64decode" and "b64encode" scripts. The above is easier to handle. -------------------------------------------------------------------------------