Use string to access binary data

Source: Internet
Author: User

STLOfStringVery powerful and comfortable to use.CodeUsed inStringThe problem of binary data access is recorded here for future reference.

First, I would like to mentionSTLMediumStringReferences:Http://www.cplusplus.com/reference/string/string/,You can check it out.

During data transmissionBufferGenerally, it is stored with a large array preset by the system, insteadSTLOfStringFor example:

 
Const IntMax_length =1024*1024; UnsignedCharData [max_length];

Because binary data may contain0x00(I .e:'\ 0'Is the end sign of the string ......

If our code is written as follows:

CharData [max_length]; size_t Length=Sockclient. read_some (boost: ASIO: buffer (data), EC );StringStrdata (data );

I can only say that this processing string should be okay. If it is binary, it will beStringCausesStrdataAndData.

In fact, a simpleDemoThe problem can be explained, for example, 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  ;} 

Running effect:

AB

2

ABCD

From the running results, it is not difficult to find thatStr2Which guaranteesStringData and originalData. This is because different constructors are used, resulting in totally different structures. This can be understood by viewing the specific constructor description from the url I mentioned earlier. Here we will go back to the previous problem. If you want to save binary data, we should perform the following operations:

CharData [max_length]; size_t Length=Sockclient. read_some (boost: ASIO: buffer (data), EC );StringStrdata (data, length );

If you want to retrieve the data, it is also simple (this also usesSocketFor example ):

 
......//Deal with strdata... Boost: ASIO: Write (sockclient, boost: ASIO: buffer (strdata. c_str (), strdata. Length ()));

HereStrdata. c_str ()That is, data,Strdata. Length ()That is, the length of the data to be sent (of course, you can also useStrdata. Size ()).

Of course, I useStringTo access binary data, it is only for the convenience of operation. I feel that this is not very good. Many friends may not advocate this practice. Here we provide a way of thinking, laugh when you think it's not good ......

Okay, that's all. I hope it will help you.

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.