473,786 Members | 2,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

binary data to a std::string

180 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char *argv[])
  2. {
  3.     ByteArray byteArray;   //is an array of bytes
  4.     ifstream ifs ( "m1_enc.ct", ios::binary );  //binary file    
  5.     if ( !ifs.is_open () )
  6.           { 
  7.                  return -1;
  8.           }    
  9.     while( !ifs.eof() )
  10.     {
  11.         //read binary data from file and put into byteArray
  12.     }
  13.     ifs.close();
  14.     //How to achieve this below code?; something like function toStdString()
  15.     string str = QString(byteArray).toStdString() ; //Unable to achieve
  16.     return 0;
Thanks
Aug 14 '07 #1
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
Expand|Select|Wrap|Line Numbers
  1.  while( !ifs.eof() )
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.
Aug 14 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Use the power of the C++ Standard LIbrary. A stringstream can help you out.

Expand|Select|Wrap|Line Numbers
  1. unsigned int var = 1234;
  2.     bitset<32> b(var);
  3.     stringstream ss;
  4.     ss << b;
  5.     string str;
  6.     ss >> str;
  7.     cout << str << endl;
  8.  
Aug 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2319
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);
6
7576
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
10
2386
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 */
6
2592
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
7
19233
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
4
2565
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ÿþÎ%ÿþ¹»ÿþÊ
5
28475
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.
29
23220
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
5
3404
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 =
0
9647
marktang
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...
0
9496
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,...
0
10363
Oralloy
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...
0
10164
jinu1996
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...
0
9961
tracyyun
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...
1
7512
isladogs
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...
0
5397
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...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.