C language and NULL character problem

This is a discussion on C language and NULL character problem within the C Programming forums, part of the General Programming Boards category; Hi, I'm wondering if you can help me with my problem. I've got an external device which is sending some ...

  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
    22,259
    Oops, I forgot to mention that I moved this to the C board since ndru_w stated C instead of C++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:23 AM
  2. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 11: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, 03:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21