A method for implementing string access binary data in C + + _c language

Source: Internet
Author: User

This article describes the C + + implementation of string access to binary data method, shared for everyone to reference. The specific methods are analyzed as follows:

In general, the STL string is very powerful and comfortable to use, this time in the code involved in the use of string access to binary data, here to record, for future reference.

First of all, mention the string in the STL reference: http://www.cplusplus.com/reference/string/string/, not understand the friend can see.

In data transmission, the buffer of binary data is generally stored with a large array of system presets, rather than STL string, for example:

const int MAX_LENGTH = 1024 * 1024;
unsigned char data[max_length];

Because binary data may contain 0x00 (ie: ' "), just the string end flag ...

If our code is written as follows:

Char Data[max_length];
size_t length = Sockclient.read_some (Boost::asio::buffer (data), EC);
String strdata (data);

I can only say that this processing string should be fine, if it is binary, it will be truncated by the constructor of string, causing strdata and data to be inconsistent.

In fact, a simple demo can explain the problem, such as the following code:

#include <string>
#include <iostream>
using namespace std;

int main ()
{
    char data[] = {' A ', ' B ', 0x00, ' C ', ' d '};
    String str1 (data), STR2 (data,sizeof (data));
    cout<<str1<<endl;
    Cout<<str1.size () <<endl;
    cout<<str2<<endl;
    Cout<<str2.size () <<endl;
    return 0;
}

The operation effect is as follows:

Ab

2

Abcd

5 

It is not difficult to see from the results of the run that the STR2 approach ensures that the data in the string is consistent with the data in the original. This is because using different constructors makes the structure completely different, and this can be understood by looking at the specific constructor descriptions from the URLs I gave earlier. Here we go back to the previous question, if we want to save the binary, we should do the following:

Char Data[max_length];
size_t length = Sockclient.read_some (Boost::asio::buffer (data), EC);
String Strdata (Data,length);

If you want to remove the data, it is also simple (this also takes the socket data to send and receive as an example):

......
Deal with strdata ...
Boost::asio::write (Sockclient, Boost::asio::buffer (Strdata.c_str (), Strdata.length ()));

The Strdata.c_str () is the data, and Strdata.length () is the length of the data to be sent (Strdata.size () can be used, of course).

Of course, we use string to access binary data, but also just for easy operation, feeling this is not too good, there should be a lot of friends do not advocate this approach, here to provide a thought, we feel good on the use, feel bad on the laugh, hehe ...

I hope this article will help you with the C + + program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.