Script To Fix ASCII->Hex Byte Limit
-
Hi All, I was always battling to convert more than 100KB with the ASCII to HEX Converter plugin, due to a memory restriction issue, “Bad Allocation”. So here’s a helpful Python script I asked AI to assist me with.
1.) Highlight all the bytes you want converted
2.) Execute the script.
3.) It will first check that Encoding is set to Windows-1252
4.) Then proceeds to convert every 100KB in chunks to completion.from Npp import notepad, editor, console import time WIN1252_ID = 45022 ASCII_TO_HEX_ID = 22070 MAX_CHUNK_SIZE = 100228 # max bytes per chunk allowed by plugin def switch_encoding(): notepad.menuCommand(WIN1252_ID) console.write("Switched encoding to Windows-1252\n") time.sleep(0.5) # wait for encoding switch def convert_chunk(start_pos, length): end_pos = start_pos + length editor.setSelection(start_pos, end_pos) console.write("Selected chunk from {} to {} (length {})\n".format(start_pos, end_pos, length)) notepad.menuCommand(ASCII_TO_HEX_ID) console.write("Converted chunk ASCII -> HEX\n") time.sleep(0.7) # wait for conversion to complete def main(): switch_encoding() total_length = editor.getLength() console.write("Total document length: {}\n".format(total_length)) current_pos = 0 while current_pos < total_length: # Calculate chunk size (do not exceed MAX_CHUNK_SIZE or remaining length) chunk_size = min(MAX_CHUNK_SIZE, total_length - current_pos) # Convert chunk convert_chunk(current_pos, chunk_size) # After conversion, the selected chunk is replaced by hex text, # which is roughly 3x the original length (2 hex chars + space per byte), # so the document length increases. # To find new current_pos, we get selection end after conversion. sel_start = editor.getSelectionStart() sel_end = editor.getSelectionEnd() converted_length = sel_end - sel_start # Move current_pos forward by the length of converted hex text current_pos = sel_end # Update total_length for next iteration total_length = editor.getLength() console.write("Updated current_pos: {}, total_length: {}\n".format(current_pos, total_length)) console.write("Completed chunked ASCII to HEX conversion.\n") if __name__ == "__main__": main()Save as ascii2hex.py in: \plugins\PythonScript\scripts\Samples
-
@LanceMarchetti said:
here’s a helpful Python script I asked AI to assist me with.
Posting AI generated code is a violation of forum policy, FYI.
@LanceMarchetti said:
Save as ascii2hex.py in: \plugins\PythonScript\scripts\Samples
This is not the best advice; it’s not a “sample”.
Just save it in the normal location for scripts, i.e.,...\plugins\Config\PythonScript\scripts -
@LanceMarchetti said in Script To Fix ASCII->Hex Byte Limit:
So here’s a helpful Python script I asked AI to assist me with
If the prompt was similar to your explanation of what the script does, then once again the AI has done a poor job.
Firstly, it acts on the whole file, not just the selected part and secondly and far more critically, your ASCII_TO_HEX_ID definition is incorrect and Npp will therefore execute an arbitrary command. -
@Alan-Kilborn My apologies, I forgot that AI code should not be posted. Thanks
-
It occurs to me that I could implement a feature in HugeFiles that would load a chunk of a file, run a plugin command on that chunk, append it to a new file, and repeat for each chunk.
Such a feature would AFAICT eliminate the need for scripts like the one posted above.
If this post gets at least 4 upvotes, I will consider implementing this feature. I expect it would be a lot of work but potentially useful.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login