-
-
Save dumpmycode/4ada2223db2f7f6683219ff634a7800a to your computer and use it in GitHub Desktop.
Misc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Miscellaneous snippets | |
| # Author: op | |
| ### BASH * remove spaces in binary string, then convert it to hex, and add 0x format for every 2 hex chars. | |
| ### output sent to xxd for hex to ascii conversion | |
| echo "obase=16; ibase=2; `./.trash/bin | sed 's/ //g'` " | bc | sed 's/../0x\0/g' | xxd -r | |
| ### GDB * Only disassemble MAIN section of binary using gdb | |
| gdb -batch -ex 'set disassembly-flavor intel' -ex 'file /home/leviathan2/printfile' -ex 'disassemble main' | |
| ########################## Python ################################ | |
| """ | |
| # get largest palindrome number from 3digit * 3digit | |
| numlist = [] | |
| for i in xrange(100,999): | |
| for o in xrange(100,999): | |
| num = str(i*o) | |
| if num == num[::-1]: | |
| numlist.append(int(num)) | |
| print max(numlist) | |
| # the loops above can be shortened using list comprehension like: | |
| numlist = [i*o for i in xrange(100,999) for o in xrange(100,999) if str(i*o) == str(i*o)[::-1]] | |
| print max(numlist) | |
| """ | |
| ### import binascii as bin, hexlify takes ascii input and spit out hex equivalent value. | |
| ### unhexlify takes hex value and spit out ascii equivalent representation if available. | |
| bin.hexlify('a') | |
| ='61' | |
| bin.unhexlify('61') | |
| ='a' | |
| ### convert ascii to binary 8 digit long, zfill(zero fill leading zeros) | |
| bin(int(binascii.hexlify('a'), 16)).zfill(8)[2:] | |
| ### convert hex string(data), 2 hex digit at a time to ascii string, then join them together. | |
| ''.join(chr(int(data[i:i+2], 16)) for i in xrange(0, len(data), 2)) | |
| ### reverse hex string. e.g. little endian | |
| ''.join(reversed([data[i:i+2] for i in xrange(0, len(data), 2)])) | |
| ### This prints out 00, 01, 02, 03, ..., 10 | |
| for i in xrange(1,11): | |
| print('{0:02}'.format(i)) | |
| ### To print 0000, 0001, 0002, 0003, ..., 9999 | |
| for i in xrange(10000): | |
| print('{0:04}'.format(i)) | |
| ### Time your python script. This default_timer will use the default timer function in python library on Windows/Linux | |
| from timeit import default_timer as timer | |
| start = timer() | |
| # do something | |
| end = timer() | |
| print(end - start) | |
| ### More simple timer | |
| import time | |
| start = time.time() | |
| # do something | |
| end = time.time() | |
| print('{} seconds'.format(end - start)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment