Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. NULL character problem

NULL character problem

Scheduled Pinned Locked Moved C / C++ / MFC
help
5 Posts 3 Posters 0 Views 1 Watching
  • 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.
  • L Offline
    L Offline
    LighthouseJ
    wrote on last edited by
    #1

    I'm reading data from the serial port with ReadFile(). Now I know that in the middle of the read, I will get a null character (0x00). After the ReadFile statement, the string it's put the characters into only exists until the null character, thinking that position is the null terminating the string, but there's more data that it read past that null which I need. Previously, I'd read one character at a time, if it was a null, it would toss it out. If it wasn't null, it'd copy it to a string. That works but it's too complicated and takes too much time communicating with the serial port. Anyone have an idea I can bypass this null but not have to read each character one at a time. When reading one character at a time, there are some really sticky and annoying timing issues that quickly turn into a minefield for me so any way I can avoid it is fine with me. Thanks in advance, Nate.

    R D 2 Replies Last reply
    0
    • L LighthouseJ

      I'm reading data from the serial port with ReadFile(). Now I know that in the middle of the read, I will get a null character (0x00). After the ReadFile statement, the string it's put the characters into only exists until the null character, thinking that position is the null terminating the string, but there's more data that it read past that null which I need. Previously, I'd read one character at a time, if it was a null, it would toss it out. If it wasn't null, it'd copy it to a string. That works but it's too complicated and takes too much time communicating with the serial port. Anyone have an idea I can bypass this null but not have to read each character one at a time. When reading one character at a time, there are some really sticky and annoying timing issues that quickly turn into a minefield for me so any way I can avoid it is fine with me. Thanks in advance, Nate.

      R Offline
      R Offline
      RicoH
      wrote on last edited by
      #2

      It's not because there is a null character in your string that the rest of your data isn't read from the serial port. What you can do is read the data (all at once) in to a char[] and then copy character per character into a string if you want to... Something like this: int i, index=0; char DataBuffer[], StringBuffer[]; ReadFile(...) for (i=0; i < NumberOfBytesRead; i++) { if (DataBuffer[i] != '\0') StringBuffer[index++] = DataBuffer[i]; } This way you don't have to worry about communication timing and so on. Don't think you are, know you are...

      L 1 Reply Last reply
      0
      • L LighthouseJ

        I'm reading data from the serial port with ReadFile(). Now I know that in the middle of the read, I will get a null character (0x00). After the ReadFile statement, the string it's put the characters into only exists until the null character, thinking that position is the null terminating the string, but there's more data that it read past that null which I need. Previously, I'd read one character at a time, if it was a null, it would toss it out. If it wasn't null, it'd copy it to a string. That works but it's too complicated and takes too much time communicating with the serial port. Anyone have an idea I can bypass this null but not have to read each character one at a time. When reading one character at a time, there are some really sticky and annoying timing issues that quickly turn into a minefield for me so any way I can avoid it is fine with me. Thanks in advance, Nate.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Since the buffer can contain '\0' characters, you cannot use any string-related functions like strlen() or strcpy(). You'll need to use memcpy() instead if you need to transfer data from one variable to another.


        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

        L 1 Reply Last reply
        0
        • D David Crow

          Since the buffer can contain '\0' characters, you cannot use any string-related functions like strlen() or strcpy(). You'll need to use memcpy() instead if you need to transfer data from one variable to another.


          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          L Offline
          L Offline
          LighthouseJ
          wrote on last edited by
          #4

          ah, makes sense because the string functions read until the first null, good call.

          1 Reply Last reply
          0
          • R RicoH

            It's not because there is a null character in your string that the rest of your data isn't read from the serial port. What you can do is read the data (all at once) in to a char[] and then copy character per character into a string if you want to... Something like this: int i, index=0; char DataBuffer[], StringBuffer[]; ReadFile(...) for (i=0; i < NumberOfBytesRead; i++) { if (DataBuffer[i] != '\0') StringBuffer[index++] = DataBuffer[i]; } This way you don't have to worry about communication timing and so on. Don't think you are, know you are...

            L Offline
            L Offline
            LighthouseJ
            wrote on last edited by
            #5

            That's kind of what I was doing, but I was reading in a byte at a time instead of reading them all in one step, then copy the character if it wasn't null. But that looks pretty sharp though, good call, thanks.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups