Last active
April 18, 2016 22:21
-
-
Save socalal/529b7c5e432dd0146af6f0fbe4012797 to your computer and use it in GitHub Desktop.
Convert decimal into hexadecimal and vice versa from a file - socalal
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
#!/usr/bin/python | |
# Created by Alan M. Orther - [email protected] | |
# Used to convert decimal into hexadecimal and vice versa from a file. Can also | |
# be used to create a comma separated list from a file with line separated | |
# values. | |
from sys import argv | |
from collections import Counter | |
# The general error message and then exit script. | |
def error_message(): | |
print " Please use the format: convert [-h or -d] and [-c or -l] and "\ | |
"[FILENAME]\n" | |
print " -h is to convert to hexadecimal and -d is to convert to decimal." | |
print " -c is to separate node IDs at the commas and -l is to separate "\ | |
"by lines.\n" | |
print " --list or --l [FILENAME] This will print a line separated file "\ | |
"as a comma separated list without conversion\n" | |
print " --check or --c [FILENAME] This will print out the first line "\ | |
"of the file and prompt the user for the -h, -d, -c, or -l options.\n" | |
print "--unique and --u [FILENAME]does not covert. It just makes a comma " \ | |
"separated list and compares unique nodes. *Line seperated only*" | |
quit() | |
# Used to split the lists. | |
split = 0 | |
used_split = 0 | |
# Start the count values at zero for unique. | |
total_count = 0 | |
uniq_count = 0 | |
duplicate_count = 0 | |
def split_list(): | |
global split, used_split | |
used_split = 1 | |
the_input = raw_input(" Would you like to split the list up? " | |
"Y or N: ") | |
if the_input in ['y', 'Y', 'yes', 'YES', 'Yes']: | |
split = int(raw_input(" How big would you like the lists? " | |
"Input a number: ")) | |
print "\n" | |
elif the_input in ['n', 'N', 'no', 'NO', 'No']: | |
print " The list will not be split.\n" | |
else: | |
print 'Please input "Y" or "N". ' | |
split_list() | |
# Only typing the script name will show an error message and not error out. | |
if len(argv) < 2: | |
print "\n You did not enter enough information.\n" | |
error_message() | |
# Allow flags to be in one flag | |
elif argv[1] in ['-hl', '-lh', '-hc', '-ch', '-dl', '-dc', '-ld', '-cd']: | |
script_name, flags, filename = argv | |
argv.append(filename) | |
argv[2] = "-%s" % flags[2] | |
argv[1] = "-%s" % flags[1] | |
# ### Create help section if --help flag used & check that enough flags used. | |
# If there are not enough flags, the script will tell you. | |
if len(argv) < 4: | |
# --help in any argv section will print the help screen. | |
if argv[1] == '--help': | |
print "\nThis script is used to convert a files content from decimal "\ | |
"into hexadecimal or hexadecimal into decimal." | |
print "You must set the flags so the script knows what it is "\ | |
"converting and if the values need to be separated per line or "\ | |
"on each comma." | |
print "Usage: convert [-h or -d] and [-c or -l] and [FILENAME]\n" | |
print " -h Convert decimal values in the file to hexadecimal." | |
print " -d Convert hexadecimal values in the file to decimal.\n" | |
print " -c Separate the values based on commas." | |
print " -l Separate the values based on lines.\n" | |
print " --list or --l [FILENAME] This will print a line separated "\ | |
"file as a comma separated list without conversion." | |
print " --check or --c [FILENAME] This will print out the first line "\ | |
"of the file then prompt the user for the -h, -d, -c, or -l "\ | |
"options." | |
print " --unique and --u [FILENAME]does not covert. It just makes a " \ | |
"comma separated list and compares unique nodes. *Line " \ | |
"seperated only*\n" | |
print "EXAMPLE:" | |
print " File content like: 292187, 292696, 293638, 314117, "\ | |
"291151, 292465, 293049, 295729" | |
print " USE: convert -h -c <filename>" | |
print " This will separate the values by commas and convert "\ | |
"decimal into hexadecimal.\n" | |
print "---------------------------------------------------------------" | |
print " File content like: 0x0004755B" | |
print " 0x00047758" | |
print " 0x00047B06" | |
print " 0x0004CB05" | |
print "USE: convert -d -l <filename>" | |
print "This will separate the values per line and convert "\ | |
"hexadecimal into decimal.\n" | |
quit() | |
# --list and --l do not covert. It just makes a comma separated list. | |
elif argv[1] == '--list' or argv[1] == '--l': | |
value_list = [] | |
try: | |
nodes_file = open(argv[2]).readlines() | |
for n in nodes_file: | |
n = n.replace(',', '') | |
n = n.strip() | |
value_list.append(n) | |
except IOError: | |
print "\nERROR: The file " + argv[2] + " does not exist.\n" | |
quit() | |
# Clear any empty lines in the lists. | |
value_list = filter(None, value_list) | |
# Format the list to print without 's | |
print "\n" | |
print '[%s]' % ','.join(map(str, value_list)) | |
# Print how many items in the list. | |
print "\nThere are " + str(len(value_list)) + " values in this list.\n" | |
quit() | |
# --unique and --u does not covert. It makes a comma separated list from | |
# a line seperated file and compares unique nodes. *Line seperated only* | |
elif argv[1] == '--unique' or argv[1] == '--u': | |
value_list = [] | |
try: | |
nodes_file = open(argv[2]).readlines() | |
for n in nodes_file: | |
n = n.replace(',', '') | |
n = n.strip() | |
value_list.append(n) | |
except IOError: | |
print "\nERROR: The file " + argv[2] + " does not exist.\n" | |
quit() | |
# Clear any empty lines in the lists. | |
value_list = filter(None, value_list) | |
# Make lists and sets for comparison. | |
seen = set() | |
uniq = [] | |
duplicates = [] | |
for x in value_list: | |
total_count += 1 | |
if x in seen: | |
duplicate_count += 1 | |
duplicates.append(x) | |
if x not in seen: | |
uniq_count += 1 | |
uniq.append(x) | |
seen.add(x) | |
show_unique = raw_input("\n Would you like to see the unique " | |
"nodes? Y or N: ").lower() | |
show_duplicates = raw_input(" Would you like to see the duplicate " | |
"nodes? Y or N: ").lower() | |
show_dupl_count = raw_input(" Would you like to see the duplicate " | |
"node count? Y or N: ").lower() | |
if show_unique in ['y', 'yes', 'yeah', 'yep']: | |
print '\n' | |
print 'UNIQUE NODES' | |
print '[%s]' % ','.join(map(str, uniq)) | |
print '\n' | |
if show_duplicates in ['y', 'yes', 'yeah', 'yep']: | |
print '\n' | |
print 'DUPLICATE NODES' | |
print '[%s]' % ','.join(map(str, set(duplicates))) | |
print '\n' | |
if show_dupl_count in ['y', 'yes', 'yeah', 'yep']: | |
print '\n' | |
print Counter(duplicates) | |
print '\n' | |
print '\n Total Node IDs: %s Unique Node IDs: %s Duplicate Node ' \ | |
'IDs: %s\n' % (total_count, uniq_count, duplicate_count) | |
quit() | |
elif argv[1] == "--check" or argv[1] == "--c": | |
# Copy argv[2] to argv[3] in the argv list. | |
argv.append(argv[2]) | |
print "\n" | |
# Open the file. | |
the_file = open(argv[2], 'r') | |
# Read the lines of the file and put in a list. | |
data = the_file.readlines() | |
# Go to the first line in the file. | |
the_file.seek(0) | |
# Print the first line. | |
print the_file.readline() | |
# Close the file. | |
the_file.close() | |
print "\n" | |
print " Above is the first line of the file." | |
# Get the flags and make commandline interface. | |
def get_arg1(): | |
argv[1] = raw_input(" Would you like to split the values by " | |
"lines or commas? l or c: ") | |
if argv[1] not in ['l', 'line', 'lines', 'c', 'comma', 'commas']: | |
print ' Please choose "l" or "c".' | |
argv[1] = "" | |
get_arg1() | |
elif argv[1] in ['l', 'line', 'lines']: | |
argv[1] = "-l" | |
elif argv[1] in ['c', 'comma', 'commas']: | |
argv[1] = "-c" | |
get_arg1() | |
def get_arg2(): | |
argv[2] = raw_input(" Are you converting the values to decimal " | |
"or hexadecimal? d or h: ") | |
if argv[2] not in ['d', 'dec', 'decimal', 'h', 'hex', | |
'hexadecimal']: | |
print 'Please choose "d" or "h".' | |
argv[2] = "" | |
get_arg2() | |
elif argv[2] in ['d', 'dec', 'decimal']: | |
argv[2] = "-d" | |
elif argv[2] in ['h', 'hex', 'hexadecimal']: | |
argv[2] = "-h" | |
get_arg2() | |
split_list() | |
# If there are not enough flags and the first doesn't equal "--help" | |
else: | |
print "\n You did not enter enough information.\n" | |
error_message() | |
# Unwrap the commandline arguments. | |
script_name, flag_1, flag_2, file_name = argv | |
# ### Warnings for flag errors and then quit. | |
# Warning if flags the same. | |
if flag_1 == flag_2: | |
print "\n The 2 flags that you entered were the same.\n" | |
error_message() | |
# Warning if the flags are not correct. | |
if flag_1 not in ['-h', '-d', '-c', '-l'] or flag_2 not in \ | |
['-h', '-d', '-c', '-l']: | |
error_message() | |
# Warning if the flags are conflicting. Separate nodes IDs on comma and line. | |
if (flag_1 == '-c' and flag_2 == '-l') or (flag_1 == '-l' and flag_2 == '-c'): | |
print "\n The 2 flags that you entered were conflicting.\n" | |
error_message() | |
# Warning if the flags are conflicting. Asking to convert nodes IDs to hex and | |
# decimal. | |
if (flag_1 == '-h' and flag_2 == '-d') or (flag_1 == '-d' and flag_2 == '-h'): | |
print "\n The 2 flags that you entered were conflicting.\n" | |
error_message() | |
# ### Open file with node IDs and make the file into a list using the flags. | |
# Create empty nodes list to input the node IDs. | |
nodes = [] | |
# -c flag will split node IDs into a list where there are commas and clean them | |
# up with strip. | |
if flag_1 == '-c' or flag_2 == '-c': | |
try: | |
nodes_file = open(file_name).read().split(',') | |
for n in nodes_file: | |
n = n.strip() | |
nodes.append(n) | |
except IOError: | |
print "\nERROR: The file " + file_name + " does not exist.\n" | |
print script_name | |
quit() | |
# -l flag will split node IDs into a list where there are new lines, remove | |
# commas, and clean them up with strip. | |
elif flag_1 == '-l' or flag_2 == '-l': | |
try: | |
nodes_file = open(file_name).readlines() | |
for n in nodes_file: | |
n = n.replace(',', '') | |
n = n.strip() | |
nodes.append(n) | |
except IOError: | |
print "\nERROR: The file " + file_name + " does not exist.\n" | |
quit() | |
# This is a catch all if I forgot something. | |
else: | |
print "ERROR! You messed up really bad somehow! ERROR!" | |
# Clear any empty lines in the lists. | |
nodes_split = filter(None, nodes) | |
# ### Use flags to decide to convert it to hex or decimal format. | |
# Create an empty list named nodes_converted to input converted node IDs. | |
nodes_converted = [] | |
# -h flag will convert decimal to hexadecimal and print to the command line. | |
if flag_1 == '-h' or flag_2 == '-h': | |
if used_split == 0: | |
split_list() | |
for n in nodes_split: | |
try: | |
n = hex(int(n)) | |
nodes_converted.append(n) | |
except ValueError: | |
print "\n --== ERROR ==-- ****** CONVERSION ERROR ******\n"\ | |
"\n You can NOT convert a hexadecimal to a hexadecimal.\n"\ | |
" Please use the --list or --l flag to print a line "\ | |
"separated file as a list of comma separated values without "\ | |
"conversion.\n" | |
quit() | |
if split > 0: | |
total_count = 0 | |
count = 0 | |
split_count = 0 | |
nodes_converted_copy = list(nodes_converted) | |
nodes_converted_split = [] | |
for n in nodes_converted_copy: | |
if split_count == split: | |
print '[%s]' % ','.join(map(str, nodes_converted_split)) | |
print "\nThere are " + str(len(nodes_converted_split)) + " "\ | |
"values in this list." | |
print "--==***** END OF THIS LIST! *****==--" | |
print "\n" | |
nodes_converted_split = [] | |
split_count = 0 | |
count = 0 | |
nodes_converted_split.append(n) | |
total_count += 1 | |
split_count += 1 | |
count += 1 | |
else: | |
nodes_converted_split.append(n) | |
total_count += 1 | |
split_count += 1 | |
count += 1 | |
print '[%s]' % ','.join(map(str, nodes_converted_split)) | |
print "\nThere are " + str(len(nodes_converted_split)) + " values in "\ | |
"this list." | |
print "--==***** END OF THIS LIST! *****==--" | |
print "\n" | |
print "There are " + str(total_count) + " values in all of the lists" \ | |
" combined.\n" | |
quit() | |
print "\n" | |
print '[%s]' % ','.join(map(str, nodes_converted)) | |
# Print how many items in the list. | |
print "\nThere are " + str(len(nodes_converted)) + " values in this list.\n" | |
quit() | |
# -d flag will convert hexadecimal to decimal and then print to command line. | |
elif flag_1 == '-d' or flag_2 == '-d': | |
if used_split == 0: | |
split_list() | |
for n in nodes_split: | |
n = int(n, 16) | |
nodes_converted.append(n) | |
# Check that the number is 6 digits or less. Might be larger for INT to INT. | |
for l in nodes_converted: | |
number_length = len(str(l)) | |
if number_length > 6: | |
print "\n" | |
print " **** ERROR ****\n" | |
print " -= ARE YOU TRYING TO CONVERT INTEGERS INTO "\ | |
"INTEGERS?? =-" | |
print " -= CONFIRM THAT THE FILE YOU ARE CONVERTING IS "\ | |
"HEXADECIMAL. =-\n" | |
quit() | |
if split > 0: | |
total_count = 0 | |
count = 0 | |
split_count = 0 | |
nodes_converted_copy = list(nodes_converted) | |
nodes_converted_split = [] | |
for n in nodes_converted_copy: | |
if split_count == split: | |
print '[%s]' % ','.join(map(str, nodes_converted_split)) | |
print "\nThere are " + str(len(nodes_converted_split)) + " "\ | |
"values in this list." | |
print "--==***** END OF THIS LIST! *****==--" | |
print "\n" | |
nodes_converted_split = [] | |
split_count = 0 | |
count = 0 | |
nodes_converted_split.append(n) | |
total_count += 1 | |
split_count += 1 | |
count += 1 | |
else: | |
nodes_converted_split.append(n) | |
total_count += 1 | |
split_count += 1 | |
count += 1 | |
print '[%s]' % ','.join(map(str, nodes_converted_split)) | |
print "\nThere are " + str(len(nodes_converted_split)) + " values "\ | |
"in this list." | |
print "--==***** END OF THIS LIST! *****==--" | |
print "\n" | |
print "There are " + str(total_count) + " values in all of the lists" \ | |
" combined.\n" | |
quit() | |
print "\n" | |
print '[%s]' % ','.join(map(str, nodes_converted)) | |
# Print how many items in the list. | |
print "\nThere are " + str(len(nodes_converted)) + " values in this list.\n" | |
quit() | |
# Catch-all error and quit. | |
else: | |
print "ERROR:" | |
error_message() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment