Community
    • Login

    A Workaround for 0xFF and NUL bytes to Hex

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    1 Posts 1 Posters 23 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Lance MarchettiL Online
      Lance Marchetti
      last edited by PeterJones

      Re: Unicode 'ÿ' – problem converting to Hex 'FF'

      Wow, I can’t believe it’s been 2 years since I ran into this issue…anyway I finally figured out a working fix.

      To fix the signed integer anomaly, we apply a bitwise AND mask (& 0xFF). This converts Scintilla’s negative integers back into true unsigned byte values (0 to 255) before string formatting. The entire conversion happens entirely in memory and replaces the target range in a single, atomic undo action.

      How to Use:
      Open a binary file, highlight the bytes you want to convert, then run the script. All ÿ and NUL bytes will successfully be converted.

      Cheers :)

      # -*- coding: utf-8 -*-
      # Notepad++ PythonScript: Convert selected bytes to hex, skip 0xFF (ÿ) and 0x00 (NUL). Substite with FF and 00 on second pass.
      
      from Npp import *
      
      def main():
          editor.beginUndoAction()
          try:
              start = editor.getSelectionStart()
              end = editor.getSelectionEnd()
              if start == end:
                  return
      
              hex_parts = []
              
              for pos in range(start, end):
                  raw_b = editor.getCharAt(pos)
                  b = raw_b & 0xFF  # Keep the unsigned 0-255 byte conversion
                      
                  if b == 0xFF:
                      # Direct conversion: No more placeholder strings needed!
                      hex_parts.append("FF")  
                  else:
                      # Flawless 2-digit Hex formatting for NUL (00) and other bytes
                      hex_parts.append("%02X" % b)  
                      
              # Write the finalized, pure hex block directly to the document
              editor.setTargetStart(start)
              editor.setTargetEnd(end)
              editor.replaceTarget(''.join(hex_parts))
              
          finally:
              editor.endUndoAction()
      
      if __name__ == '__main__':
          main()
      
      1 Reply Last reply Reply Quote 0

      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
      • First post
        Last post
      The Community of users of the Notepad++ text editor.
      Powered by NodeBB | Contributors