Thread: size of binary string

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    size of binary string

    How to get the size of a binary string?
    length() and size() both stops at a \0 character.

    Code:
    // string::length
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      string str ("Tes\0t string");
      cout << "The length of str is " << str.length() << " characters.\n";
      cout << "The length of str is " << str.size() << " characters.\n";
      return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Ducky View Post
    How to get the size of a binary string?
    length() and size() both stops at a \0 character.

    Code:
    // string::length
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      string str ("Tes\0t string");
      cout << "The length of str is " << str.length() << " characters.\n";
      cout << "The length of str is " << str.size() << " characters.\n";
      return 0;
    }
    Null-terminators don't belong in an std::string. What are you trying to do?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I believe a string can handle binary strings just fine - including a 0 terminator. I'm not completely sure about this, and I think it's "nicer" to use a vector of chars for this, but that's a matter of opinion. Can those who actually know the C++ standard confirm binary strings are legal in the C++ standard (I'm actually 99.9% sure about this though)?

    Anyway, the problem with your code is that you use a C-string argument as constructor. And guess how it finds the length of that C-string? By finding a 0-terminator! But you can also pass another argument, being the number of characters in the character array, as such:
    Code:
    std::string test("x\x00ab", 4);
    That will create a string with 4 characters.

    And to others: what do you think is better for binary strings, vectors or strings, or does it not matter at all? And why do you think so?
    Last edited by EVOEx; 08-07-2010 at 09:59 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The character '\0' has no special meaning to std::string. The problem it is that it does have a special meaning for C-style strings which is what the argument is.

    I don't think I'd use a std::string for the storage of a binary string (e.g from a binary file).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Ok, first of all thanks to all of you for your kind help!

    I have a function that returns a char* as a binary string of unknown size.
    Now i need to write this string to a file but for that first i need to know its size.

    First i tried to do it in C but then i thought it's maybe easier in C++.

    So maybe i should count it one by one by incrementing a pointer or use a vector?
    Last edited by Ducky; 08-07-2010 at 11:13 AM.
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If your program returns a char* pointing to a buffer of unknown size, then there is nothing you can do.

    You have to keep the size around, and that's what a vector does.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Wouldnt this work to get the size?

    Code:
    char * binaryStr = RetBinStr(buffer);
    
    while (binaryStr--) { i++; }
    Using Windows 10 with Code Blocks and MingW.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So you think it will stop once the pointer reaches NULL and that is the size of the returned data?

    You can test your idea. I'd suppose the result is a crash sooner or later.

    ---

    Perhaps the function writes to the passed buffer and the return value points to the end of the range? Yes, in such a case, the length of the binary string would be:

    Code:
    char* end = RetBinStr(buffer);
    size_t length = end - buffer;
    If that is not so, you can't find out the size any more.
    Last edited by anon; 08-07-2010 at 12:03 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    That makes sense, thank you very much Anon!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM