Thread: Counting a stream of non-null terminated bytes

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    6

    Question Counting a stream of non-null terminated bytes

    Hello,I want to count the number of bytes in a stream that contains nulls. I know I can't use strlen() for this, so is there an alternative?
    Code:
    char *bytes = "\x11\x12\x13\x00\x12\x13\x14\x15";
    I want to be able to say there's 8 bytes here, not 3 as strlen gives me.Thank you!

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by sockets12 View Post
    Hello,I want to count the number of bytes in a stream that contains nulls. I know I can't use strlen() for this, so is there an alternative?
    Code:
    char *bytes = "\x11\x12\x13\x00\x12\x13\x14\x15";
    I want to be able to say there's 8 bytes here, not 3 as strlen gives me.Thank you!
    Am not sure why you hack strings like this; this perl like syntax looks very ugly to me.

    First, strlen() returns the number of characters in a string but it happens that the size of a 'char' is one byte.If you want to get the size of an object use the sizeof operator.

    Code:
    sizeof(bytes)
    But in this case, that will not work work as 'bytes' is a pointer

    AFAIK there is not going to be a null byte in a C string unless you explicitly insert it like you are doing.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by sockets12 View Post
    Hello,I want to count the number of bytes in a stream that contains nulls. I know I can't use strlen() for this, so is there an alternative?
    Code:
    char *bytes = "\x11\x12\x13\x00\x12\x13\x14\x15";
    I want to be able to say there's 8 bytes here, not 3 as strlen gives me.Thank you!
    Then you will have to track the size separately. You have two options. The first is to make bytes a char array
    Code:
    unsigned char bytes[] = {0x11, 0x12, 0x13, 0x00, 0x12, 0x13, 0x14, 0x15};
    sizeof(bytes);
    That has the disadvantage that, when passed to a function, bytes becomes a simple unsigned char *, so you can't use sizeof on it anymore, you have to pass in the size as another parameter to the function. But it has the advantage that you don't waste 4 (or 8 or whatever) bytes on a len member all the time. So long as you can use sizeof, which is computed at compile time, you save space.

    The second is to make a struct that has a length member
    Code:
    struct byte_array {
        unsigned char *data;
        size_t len;
    };
    struct byte_array bytes = {"\x11\x12\x13\x00\x12\x13\x14\x15", 8};
    bytes.data;
    bytes.len;
    Upside, you always have the length available. Downside, it takes up extra space. Another downside is that, if you make data point to a string literal (a.k.a. a string constant), then you can't modify the data (it's constant). You need to point it to modifiable data (e.g. an array) or malloc memory and copy the contents -- just remember to free it.

    That's it. Arrays in C don't actually carry their length with them, nor do pointers contain any info about the memory pointed to, other than the starting address. It's your job to track that if you care. Furthermore, the C string functions definitely wont work. By definition, a string in C is a sequence of contiguous bytes ending in a byte with a zero value (a null byte). Thus things like strlen, strcpy, etc will definitely break.

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    6
    That makes sense! Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NULL terminated arrays of struct
    By ttw in forum C Programming
    Replies: 3
    Last Post: 06-14-2012, 07:46 AM
  2. Non null-terminated strings
    By Casey G in forum C++ Programming
    Replies: 5
    Last Post: 05-10-2011, 07:47 PM
  3. un-terminated stream
    By WDT in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-01-2005, 07:08 PM
  4. null terminated
    By TeQno in forum C Programming
    Replies: 5
    Last Post: 06-06-2003, 06:10 PM
  5. Null Terminated Arrays
    By sean in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 06-17-2002, 11:39 AM

Tags for this Thread