size of binary string
This is a discussion on size of binary string within the C++ Programming forums, part of the General Programming Boards category; How to get the size of a binary string?
length() and size() both stops at a
-
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;
}
Compiler MSVC++ 2010 with Code::Blocks.
-
Guest

Originally Posted by
Ducky
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:
int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
<1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
(l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
)putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}
-
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.
-
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).
-
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.
Compiler MSVC++ 2010 with Code::Blocks.
-
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).
-
Wouldnt this work to get the size?
Code:
char * binaryStr = RetBinStr(buffer);
while (binaryStr--) { i++; }
Compiler MSVC++ 2010 with Code::Blocks.
-
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).
-
That makes sense, thank you very much Anon!
Compiler MSVC++ 2010 with Code::Blocks.
Popular pages Recent additions
Similar Threads
-
By Shibby3 in forum C# Programming
Replies: 9
Last Post: 07-26-2010, 05:27 AM
-
By nctar in forum C Programming
Replies: 7
Last Post: 05-12-2010, 10:04 AM
-
By Tonto in forum Windows Programming
Replies: 5
Last Post: 12-22-2006, 04:45 PM
-
By maro009 in forum C++ Programming
Replies: 20
Last Post: 05-17-2005, 12:56 PM
-
By Prelude in forum A Brief History of Cprogramming.com
Replies: 16
Last Post: 10-02-2004, 03:00 PM