2

I have question that I am having a hard time understanding what the code might look like so I will explain the best I can. I am trying to view and search a NUL byte and replace it with with another NUL type byte, but the computer needs to be able to tell the difference between the different NUL bytes. an Example would be Hex code 00 would equal NUL and hex code 01 equals SOH. lets say I wanted to create code to replace those with each other. code example

TextFile1 = Line.Replace('NUL','SOH')
TextFile2.write(TextFile1)

Yes I have read a LOT of different posts just trying to understand to put it into working code. first problem is I can't just copy and paste the output of hex 00 into the python module it just won't paste. reading on that shows 0x00 type formats are used to represent that but I'm having issues finding the correct representation for python 3.x

Print (\x00)
output = nothing shows   #I'm trying to get output of 'NUL' or as hex would show '.' either works fine --Edited

so how to get the module to understand that I'm trying to represent HEX 00 or 'NUL' and represent as '.' and do the same for SOH, Not just limited to those types of NUL characters but just using those as exmple because I want to use all 256 HEX characters. but beable to tell the difference when pasting into another program just like a hex editor would do. maybe I need to get the two programs on the same encoding type not really sure. I just need a very simple example text as how I would search and replace none representable Hexadecimal characters and find and replace them in notepad or notepad++, from what I have read, only notepad++ has the ability to do so.

  • 1
    `'NUL'` is just the string `'NUL'`, and `0x00` is the number `0` in hexadecimal. Did you mean `'\x00'`? – tobias_k Mar 11 '17 at 20:38
  • yes, exactly I can't find the right encoding or what ever it may be to create a simple find and replace function using all 256 hexadecimal options. Because some of the options cann't be displayed conventionally. So How can I work around that. getting both python and notepad++ to understand what is what. – LookingForAnswer Mar 11 '17 at 20:42

2 Answers2

3

If you are on Python 3, you should really work with bytes objects. Python 3 strings are sequences of unicode code points. To work with byte-strings, use bytes (which is pretty much the same as a Python 2 string, which used the "sequence of bytes" model).

>>> bytes([97, 98, 99])
b'abc'
>>>

Note, to write a bytes literal, prepend a b before the opening quote in your string.

To answer your question, to find the representation of 0x00 and 0x01 just look at:

>>> bytes([0x00, 0x01])
b'\x00\x01'

Note, 0x00 and 0 are the same type, they are just different literal syntaxes (hex literal versus decimal literal).

>>> bytes([0, 1])
b'\x00\x01'

I have no idea what you mean with regards to Notepad++.

Here is an example, though, of replacing a null byte with something else:

>>> byte_string = bytes([97, 98, 0, 99])
>>> byte_string
b'ab\x00c'
>>> print(byte_string)
b'ab\x00c'
>>> byte_string.replace(b'\x00', b'NONE')
b'abNONEc'
>>> print(byte_string.replace(b'\x00', b'NONE'))
b'abNONEc'
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • ok try to display or "print (\x00)" on python nothing shows. try to view the hex code of 00 on a hex editor or personal hexing application what ever you chose. you will get a representation character of '.' or on notepad ++ you will get a 'NUL' I'm trying to create application that will replace those NUL characters with another NUL character. – LookingForAnswer Mar 11 '17 at 20:49
  • 1
    so @LookingForAnswer are you looking for only `0x00` or just anything that isn't **printable** in which case you may want to use something like `"\x00".isprintable()` – Tadhg McDonald-Jensen Mar 11 '17 at 20:54
  • Yes printable works, but it shows a space. so lets say I put that into a program like notepad its not going to know the difference between that space and another space like a hex editor would. I'm Ultimately trying to get 256 unique characters just like a hex program would. making it work between two programs python and another program. knowing the unique 256 character sets. – LookingForAnswer Mar 11 '17 at 21:03
  • "printable shows a space" what do you mean? And *again* you should be working with `bytes` objects, not `str`,which *will show* `b"\x00"` when printed – juanpa.arrivillaga Mar 11 '17 at 21:03
  • It sound like he wants to see the string "nul" instead of the character \x00. In other words for any non printable *character* he wants to automatically print a special *string*. The notepad++ example probably relates to how their text editor control interprets a nul byte char and displays a big black box with the text 'nul' inside of it – pinkfloydx33 Mar 11 '17 at 21:07
  • type \x00".isprintable() then type print(\x00) output =' ' Making that a unique across 2 platforms not sure if I would need an encoding to make sure they both know that space is only for \x00 and not \x02 because both of those options will print a space is what I'm getting at. Possibilities show 256 different possibilities. what is a clever way to make those unique across 2 programs. – LookingForAnswer Mar 11 '17 at 21:10
  • 1
    A hexeditor doesn't just magically show a "." it iterates over the bytes and prints a representation of them that gets calculated by the program. Change the bytes, recalculate and display. It's in their logic. They see a non printable, they output a space. They see the hex for 97, they print an 'a' . I don't know python but psuedocode is `if (!char.is_printable) then print(replacementDict[char]) else print(char)` – pinkfloydx33 Mar 11 '17 at 21:14
  • @pinkfloydx33 yes, you understand what I'm talking about. and @ juanpa if you put that into a text document the amount of bytes will not add up to 1 how it should be represented when saved. I'm trying to find out how to get python and notepad to have 1 byte representation for a NUL character much like a hex editor would. I think thats explains it best lol – LookingForAnswer Mar 11 '17 at 21:14
  • What? Yes, it will be one byte. **Are you sure you are working with `bytes` objects and not `str`?** – juanpa.arrivillaga Mar 11 '17 at 21:15
  • @pinkfloyd thank you for that lol. put 02 into a hex editor you will get a "." now paste that into a notepad and it shows as a space. now copy back to hex editor and it retains its hex value of 02 and shows as a '.' again. even though notepad displayed as a space. its being represented differently but holds that same hex spot. – LookingForAnswer Mar 11 '17 at 21:23
  • 1
    If you're copying the "." from the hex editor, what more than likely is happening is that the editor captures the control-c or has its own copy command that actually copies the real representation and not the "." When you copy out of notepad and paste back to the hex editor it performs its calculations of the data on the fly again. It sounds like you are confusing a great many things. There's no platforms here that you can conform to. There's bytes and how they get displayed – pinkfloydx33 Mar 11 '17 at 21:27
  • @pinkfloydx33 could you assist with what code would look like if I wanted to replace the black Block NUL with the Black Block SOH using python on NotePad++. – LookingForAnswer Mar 11 '17 at 21:46
  • 1
    No because like I said I don't know python, but more because what you're asking for doesn't make any sort of sense – pinkfloydx33 Mar 11 '17 at 22:28
2

another equivalent way to get the value of \x00 in python is chr(0) i like that way a little better over the literal versions

Kyle Roux
  • 736
  • 5
  • 11