Thread: C language and NULL character problem

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    C language and NULL character problem

    Hi,
    I'm wondering if you can help me with my problem. I've got an external device which is sending some data by UART. I'm reading it to the char array. And unfortunatelly this device sends NULL characters inside the data (for the device is just something like NOP instruction but in C it's not so ) and because of that I'm loosing all data after that NULL character.
    I can't change the device so I'm asking if there's some possibility to change the interpretation of NULL in C?
    Regards

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The symbol in C by the name NULL is different from the character constant NUL (in C, usually represented as '\0'). All your text uses the former, but actually means the latter.

    Either way, you can't change the behaviour of standard string functions, etc.

    You can use other mechanisms to store "strings of text" that are allowing ANY and ALL characters, NUL and others. The concept here is to NOT use a marker to mark the end of a string, but to track the length instead.

    I believe the "std::string" class will handle strings that contain NUL - as long as you don't use the function c_str() to pass the string to something that expects a NUL-terminated string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Oops, I forgot to mention that I moved this to the C board since ndru_w stated C instead of C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    I believe the "std::string" class will handle strings that contain NUL - as long as you don't use the function c_str() to pass the string to something that expects a NUL-terminated string.

    --
    Mats
    Since he's posting in the C forum, I'm guessing he's not using C++.

    When you receive the data, doesn't it also send the size of the data? Then you'd know where the end is. As for C string functions, you'll probably have to mimic their functionality, but pass the size directly instead of having them assume '\0' as the end...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    Since he's posting in the C forum, I'm guessing he's not using C++.

    When you receive the data, doesn't it also send the size of the data? Then you'd know where the end is. As for C string functions, you'll probably have to mimic their functionality, but pass the size directly instead of having them assume '\0' as the end...
    Actually, when I read the post, it was in the C++ forum, Laserlight moved it after this.

    But I agree, there is no reason why one couldn't do one of two things:
    1. When receiving a NUL filler, replace it wiht some other code [e.g. a two-byte sequence of "\0" - that also means that any existing "\" in the input must be replaced with "\\"]. When operating on the data later on, remove the "\" and replace 0 with NUL, as suitable.

    2. Writing some "replacement" string functions that use a string length as part of the data passed around. If it's C, a struct with a pointer to char and an integer lenght would be fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 10:07 AM
  3. Menu like file, edit, help, etc...
    By Livijn in forum Windows Programming
    Replies: 44
    Last Post: 01-23-2007, 05:49 PM
  4. Menu in C, like File, Edit, Window, etc...
    By Livijn in forum Windows Programming
    Replies: 5
    Last Post: 01-18-2007, 05:49 PM
  5. C programming-Linked list problem
    By male4329 in forum C Programming
    Replies: 18
    Last Post: 06-02-2005, 02:05 AM