How to convert a byte array to a std::string. Byte array contains bytes of data.
See the code below. Help me to write toStdString() function defined in ByteArray class. - int main(int argc, char *argv[])
-
{
-
ByteArray byteArray; //is an array of bytes
-
ifstream ifs ( "m1_enc.ct", ios::binary ); //binary file
-
if ( !ifs.is_open () )
-
{
-
return -1;
-
}
-
while( !ifs.eof() )
-
{
-
//read binary data from file and put into byteArray
-
}
-
ifs.close();
-
//How to achieve this below code?; something like function toStdString()
-
string str = QString(byteArray).toStdString() ; //Unable to achieve
-
return 0;
-
}
Thanks
2 5857 oler1s 671
Recognized Expert Contributor
Did you mean QByteArray, which is part of Trolltech's QT? Because you use QString, which is. Since you are effectively asking a question on how to use a third party library, ask your question on forums that are dedicated to it. Here's a google search result: http://www.qtforum.org/
I can tell you that
won't work. Your logic is something like, while you haven't reached the end of the file, keep reading from the file. But eof doesn't check if you have reached the end of the file. Take a look at http://www.cplusplus.com/reference/i...m/ios/eof.html. Eof returns true only when eofbit is set. And eofbit is set only after a failed I/O attempt. But your code does it the other way around. It checks for eof bit first, and then attempts I/O (which could then fail). See the problem?
You realize that you can test an attempt at file input itself? All those I/O operations have return values as well.
Use the power of the C++ Standard LIbrary. A stringstream can help you out. -
unsigned int var = 1234;
-
bitset<32> b(var);
-
stringstream ss;
-
ss << b;
-
string str;
-
ss >> str;
-
cout << str << endl;
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Johannes |
last post by:
Hi,
I tried to pack eight integer values and one string into one binary
string, which I would like to store in a mysql db.
I encountered two problems doing this:
1)
$this->packed = pack('N8A*', $this->value1, $this->value2,
$this->value3, $this->value4, $this->value5, $this->value6,
$this->value7, $this->value8, $this->stringvalue);
|
by: Tarun |
last post by:
Hi All,
I need to find a particular substring in a binary string(some text
appended & prepended to binary data).
I cant use strstr since it terminates on receiving '\0'which can be
there in binary data also I cant use memmem. Is there any other
available function to do this.
Regards
Tarun
|
by: jt |
last post by:
I'm needing to take a binary string start at a certain position and return a
pointer from that postion to the end of the binary stirng.
something like this:
char bstr;
char *pos;
pos=mid(bstr,35); / *return a pointer of the rest of the binary string
starting at element 35 */
|
by: young_leaf |
last post by:
Using STL how do I convert a variable to a binary string but with
special case which
for example BYTE x, i need only the first 3 bits of this byte, is that
possible?
to be more specific,
from that byte i want to generate (so valid values for x will be 0-5)
000
001
|
by: elliotng.ee |
last post by:
I have a text file that contains a header 32-bit binary. For example,
the text file could be:
%%This is the input text
%%test.txt
Date: Tue Dec 26 14:03:35 2006
00000000000000001111111111111111
11111111111111111111111111111111
00000000000000000000000000000000
11111111111111110000000000000000
| |
by: Freddy Coal |
last post by:
Hi, I'm recovery data from an spectrum analizer, the equipment send me data
in binary, each data in the trace have 4 bytes, I would like convert that to
double, how make that?.
I'm trying extract the data in substrings of 4 chars, and convert this with
Cdbl(my substring), but I get an InvalidCastException in the conversion.
Example of the spectrum data:
ÿþÃÑÿþÉLÿþÃÿþË:ÿþ²øÿþÂ&ÿþÆAÿþ®ÿþÄ5ÿþÎ%ÿþ¹»ÿþÊ
|
by: gezerpunta |
last post by:
Hi
strlen does not return the correct value .I compared the filesize()
and strlen byte size but they are not equal. I must find binary string
length and it must be equal to filesize()
thks.
|
by: aarthi28 |
last post by:
Hi,
I have written this code, and at the end, I am trying to write a
vector of strings into a text file. However, my program is nor
compiling, and it gives me the following error when I try to write to
the file:
error C2679: binary '<<' : no operator found which takes a right-hand
operand of type 'std::string' (or there is no acceptable conversion)
I don't know what I am doing wrong. I have posted my entire program
|
by: andrew.douglas11 |
last post by:
Hello,
I'm looking to display an image in the browser using a binary string
containing all the bytes that make up a GIF image. I've tried all
the standard encodings in System.Text.Encoding, but the images aren't
showing up. I'm using the approach where I'm setting ImageURL =
"GetImage.aspx?id=1". Here's my code from GetImage.aspx:
Dim imageData As String = the binary data representing a .gif file
Dim imageBytes() As Byte =
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |