Hex: Difference between revisions
Jump to navigation
Jump to search
(→Python) |
(→Python) |
||
Line 339: | Line 339: | ||
|hex string to binary string |
|hex string to binary string |
||
|a='68656c6c6f' |
|a='68656c6c6f' |
||
a='deadbeef' |
|||
|b='hello' |
|b='hello' |
||
b='\xde\xad\xbe\xef' |
|||
| |
| |
||
<source lang=python> |
<source lang=python> |
||
a.decode("hex") # built-in |
a.decode("hex") # built-in. Python 2.x only |
||
import binascii |
import binascii |
||
binascii.a2b_hex(a) # binascii module |
binascii.a2b_hex(a) # binascii module |
||
binascii.unhexlify(a) |
binascii.unhexlify(a) |
||
bytes.fromhex(a) # Python 3.x only - IGNORE SPACES, IF ANY |
|||
</source> |
|||
|- |
|||
|colspan=4| |
|||
;Array |
|||
|- |
|||
|binary string to array |
|||
|a='hello' |
|||
a='\xde\xad\xbe\xef' |
|||
|b=[0x68,0x65,0x6c,0x6c,0x6f] |
|||
b=[0xde,0xad,0xbe,0xef] |
|||
| |
|||
<source lang=python> |
|||
map(ord,a) |
|||
</source> |
|||
|- |
|||
|hex string to array |
|||
|a='68656c6c6f' |
|||
a='deadbeef' |
|||
|b=[0x68,0x65,0x6c,0x6c,0x6f] |
|||
b=[0xde,0xad,0xbe,0xef] |
|||
| |
|||
<source lang=python> |
|||
bytearray.fromhex(a) # Python 2.x or 3.x - IGNORE SPACES, IF ANY |
|||
</source> |
</source> |
||
|} |
|} |
Revision as of 16:03, 10 February 2015
References
Many examples come from cplusplus.com and stackoverflow.com.
Shell
See also Linux Commands.
Convert 66 6f 6f → "foo"
hexdump -e '"%2x"' <myfile> # Convert myfile into a long hexadecimal string - ! See DOUBLE-QUOTED parameter
hexdump -C <myfile> # Canonical hex + ascii display (16 bytes)
hd <myfile> # (idem)
echo 202122 | xxd -r -p # Convert hexdump to a binary string
echo -e '\x66\x6f\x6f' # 'foo'
or to convert hex to decimal [1]
echo $((0x15a))
printf '%d\n' 0x15a
perl -e 'printf ("%d\n", 0x15a)'
echo 'ibase=16;obase=A;15A' | bc
Convert "foo" → 66 6f 6f
xxd -g8 -c32 <file> # Output 32 bytes, grouped in 8-byte columns
xxd -p -c64 <file> # Output 64 bytes per line, plain (postscript) mode
echo -n ' !"' | xxd -p # Convert binary string to hexdump - DON'T FORGET -n
Or to convert decimal to hex
printf '%x\n' 346
perl -e 'printf ("%x\n", 346)'
echo 'ibase=10;obase=16;346' | bc
hex editors
hex diffs
diff -u <(hexdump -v -C file1) <(hexdump -v -C file2)
C / C ++
Read an integer from an hex string
Homebrew
const char _hex[] = "0123456789ABCDEF";
// Change a 0x0A into 'A'
char itoh(int nibble)
{
return _hex[nibble];
}
// Change a 'A' into 0x0A
int htoi(char hex)
{
int isletter = (hex & 0x40) >> 6;
return (isletter | (isletter << 3)) + ( hex & 0x0F );
}
// Change "A010" into 0xA010
template<class T>
T hextoint_BE(string h)
{
T i=0;
for(string::const_iterator it=h.begin(); it!=h.end(); ++it)
{
i=(i<<4)|htoi(*it);
}
return i;
}
// Change "A010" into "\xA0\x10"
string hextostring_BE(const string& h)
{
string x;
for(unsigned int i=0; (i+1)<h.length(); i+=2) {
x += (htoi(h[i]) << 4)+ htoi(h[i+1]);
}
return x;
}
// Change 0xA010 into "A010"
template<class T>
string inttohex_BE(T i, unsigned int size)
{
string sh;
while( i > 0 )
{
sh = itoh(i&0xF) + sh;
i>>=4;
}
if(!size)
return sh;
if(size<sh.length())
return string(sh,sh.length()-size,size);
else
return string(size-sh.length(),'0')+sh;
}
// Change "\xA0\x10" into "A010"
string stringtohex_BE(const string& s, unsigned int size)
{
string h;
for(string::const_iterator it=s.begin();it!=s.end();++it)
{
h += itoh((*it & 0xF0) >> 4);
h += itoh((*it & 0x0F) >> 0);
}
if(!size)
return h;
if(size<h.length())
return string(h,h.length()-size,size);
else
return string(size-h.length(),'0')+h;
}
Using atol
/* atol example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
long int li;
char szInput [256];
printf ("Enter a long number: ");
gets ( szInput );
li = atol (szInput);
printf ("The value entered is %d. The double is %d.\n",li,li*2);
return 0;
}
Using strtol, strtoll
strtol
(resp. strtoll
) returns LONG_MIN, LONG_MAX (resp. LLONG_MIN, LLONGMAX) in case of under/overflow.
/* strtol example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
char * pEnd;
long int li1, li2, li3, li4;
li1 = strtol (szNumbers,&pEnd,10);
li2 = strtol (pEnd,&pEnd,16);
li3 = strtol (pEnd,&pEnd,2);
li4 = strtol (pEnd,NULL,0);
printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n",
li1, li2, li3, li4);
return 0;
}
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
string s = "abcd";
char * p;
long n = strtol( s.c_str(), & p, 16 );
if ( * p != 0 ) {
cout << "not a number" << endl;
}
else {
cout << n << endl;
}
}
Using strtoul, strtoull
Use strtoul
(resp. strtoull
) when value to read is greater than numeric_limits<long>::max()
(resp. numeric_limits<long long>::max()
).
/* strtoul example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char szInput [256];
unsigned long ul;
printf ("Enter an unsigned number: ");
fgets (szInput,256,stdin);
ul = strtoul (szInput,NULL,0);
printf ("Value entered: %lu. Its double: %lu\n",ul,ul*2);
return 0;
}
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
string s = "fffefffe";
char * p;
long n = strtoul( s.c_str(), & p, 16 );
if ( * p != 0 ) {
cout << "not a number" << endl;
} else {
cout << n << endl;
}
}
Using sscanf
#include <stdio.h>
main()
{
char s[] = "fffffffe";
int x;
sscanf(s, "%x", &x);
printf("%u\n", x);
}
Using stringstream
#include <sstream>
#include <iostream>
int main() {
unsigned int x;
std::stringstream ss;
ss << std::hex << "fffefffe";
ss >> x;
// output it as a signed type
std::cout << static_cast<int>(x) << std::endl;
}
Python
| |||
int to hex | a=1234 | b='4d2' |
b='%x' % a
|
int to hex (with 0x prefix)
|
a=1234 | b='4d2' |
b=hex(a)
b='0x%x'%a
|
int to hex (padded) | a=1234 | b='000004d2' |
b='%08x' % a
|
int to hex (padded, caps) | a=1234 | b='000004D2' |
b='%08X' % a
|
int to hex char | a=0x41 | b='\x41' |
b=chr(0x41)
|
hex to int | a='000004D2' | b=1234 |
b=int(a,16)
|
hex (with 0x prefix) to int
|
a='0x000004D2' | b=1234 |
b=int(a,0) # base 0, autodetect
|
| |||
binary string to hex string | a='hello' | b='68656c6c6f' |
a.encode("hex") # built-in
import binascii
binascii.b2a_hex(a) # binascii module
binascii.hexlify(a)
|
hex string to binary string | a='68656c6c6f'
a='deadbeef' |
b='hello'
b='\xde\xad\xbe\xef' |
a.decode("hex") # built-in. Python 2.x only
import binascii
binascii.a2b_hex(a) # binascii module
binascii.unhexlify(a)
bytes.fromhex(a) # Python 3.x only - IGNORE SPACES, IF ANY
|
| |||
binary string to array | a='hello'
a='\xde\xad\xbe\xef' |
b=[0x68,0x65,0x6c,0x6c,0x6f]
b=[0xde,0xad,0xbe,0xef] |
map(ord,a)
|
hex string to array | a='68656c6c6f'
a='deadbeef' |
b=[0x68,0x65,0x6c,0x6c,0x6f]
b=[0xde,0xad,0xbe,0xef] |
bytearray.fromhex(a) # Python 2.x or 3.x - IGNORE SPACES, IF ANY
|