Skip to content

Instantly share code, notes, and snippets.

@meisinger
Last active February 12, 2020 21:45
Show Gist options
  • Save meisinger/da9585c8ef87788044667a6cf2e6b5c1 to your computer and use it in GitHub Desktop.
Save meisinger/da9585c8ef87788044667a6cf2e6b5c1 to your computer and use it in GitHub Desktop.
Given a file, generates the HEX equivalent byte array
def parse_commands(commands):
hex_commands = []
for command in commands:
item = command.strip()
hex_value = ' '.join([str(hex(ord(i)))[2:4] for i in item])
hex_commands.append(item + ' = ' + '"' + hex_value + '"')
return hex_commands
def read_commands():
with open('commands.txt', 'r') as file:
commands = file.readlines()
return commands
def write_commands(commands):
with open('hex_commands.txt', 'w') as file:
file.write('\n'.join(commands))
if __name__ == '__main__':
commands = read_commands()
hex_commands = parse_commands(commands)
write_commands(hex_commands)
def parse_commands(commands):
hex_commands = []
for command in commands:
item = command.strip()
b_array = bytearray(item.encode())
hex_value = ', '.join([str(i) for i in b_array])
hex_commands.append(item + ' = ' + '"' + hex_value + '"')
# hex_commands.append(' '.join([str(hex(ord(i)))[2:4] for i in command.strip()]))
return hex_commands
def generate_consts(commands):
consts = []
for command in commands:
item = command.strip()
consts.append('public const string ' + item + ' = "' + item + '";')
return consts
def generate_mappings(commands):
mappings = []
for command in commands:
item = command.strip()
b_array = bytearray(item.encode())
hex_value = ', '.join([str(i) for i in b_array])
mappings.append('mapping[RedisCommands.' + item + '] = new[] { new byte[] {' + hex_value + '}};')
return mappings
def read_commands():
with open('commands.txt', 'r') as file:
commands = file.readlines()
return commands
def write_commands(commands):
with open('hex_commands.txt', 'w') as file:
file.write('\n'.join(commands))
def write_consts(commands):
with open('command_consts.txt', 'w') as file:
file.write('\n'.join(commands))
def write_mappings(mappings):
with open('command_mappings.txt', 'w') as file:
file.write('\n'.join(mappings))
if __name__ == '__main__':
commands = read_commands()
hex_commands = parse_commands(commands)
const_commands = generate_consts(commands)
mapping_commands = generate_mappings(commands)
write_commands(hex_commands)
write_consts(const_commands)
write_mappings(mapping_commands)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment