Accumulation of byte array conversion to string

Source: Internet
Author: User
Tags manual writing

. Net EncryptionAlgorithmThe returned data is of the byte [] type, which makes it very troublesome to store. The simplest way is to convert byte [] to a string for storage. Of course, if the data volume is large, it should be different.

So I will convert byte [] into a string for a simple accumulation and analysis. Currently, there are three simple solutions for conversion.

1. Convert. tobase64string and convert. frombase64string

This method is simple and practical. The converted string contains: 26 English letters in upper and lower case, +,/, and 0 ~ 9, a total of 64 characters. "=" Usually appears at the end ". The reason is simple.

Principle: Describes 6-bit binary data with 64 characters.

Method: concatenate byte data and group the data in 6 bits. Use 64 characters to describe the data in 6 bits. If the group is less than 6 bits, use "=" to complete the data.

 

2. Write by yourselfCode, With hexadecimal characters 0 ~ 9. ~ F to describe the data.

Principle: use 16 characters to describe the 4bit binary. That is to say, a byte must be described using two hexadecimal characters.

Method: use characters to describe the first half and the second half of each byte.

This method requires manual writing, and the code is quite simple. You can use crtl + C Ctrl + v.

 Public   Static   String Bytetostring (Byte [] Bytes)
{
Stringbuilder strbuilder = New Stringbuilder ();
Foreach ( Byte BT In Bytes)
{
Strbuilder. appendformat ( "{0: X2 }" , BT );
}
Return Strbuilder. tostring ();
}
Public Static Byte [] Stringtobyte (String Str)
{
Byte [] Bytes = New Byte [Str. Length/2];
For ( Int I = 0; I <Str. Length/2; I ++)
{
Int Btvalue = convert. toint32 (Str. substring (I * 2, 2), 16 );
Bytes [I] = ( Byte ) Btvalue;
}
Return Bytes;
}
 

3. Encoding encoding is used to automatically convert encoding. getencoding ("encoding format"). getstring () and encoding. getencoding ("encoding format"). getbytes ()

Principle: force conversion using editing rules.

Mode: Use the encoding method to edit the data. The converted byte data or string may contain various strange or invisible characters, which is inconvenient to store.

 

 

 

Summary:

We recommend that you use the first method to convert strings. However, if the format is required, such as in a custom machine language, "+" "/" is not allowed, the second method is considered.

If you convert data to byte or string for memory operations, the third method is a good choice.

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.