Taste the flexibility of. Net (C #) from the output byte array

Source: Internet
Author: User

When dealing with computers, it is inevitable that a byte array is often output. This article describes how to output a byte array in. Net:

 

The quickest way:

String bitconverter. tostring (byte []) method. (Partial output of byte array supported by overload)

 

Code:

Console. writeline (bitconverter. tostring (New byte [] {1, 3, 54,123,199,200,215,255 }));

Output:

01-03-36-7b-c7-c8-d7-ff

 

The disadvantage is that it is impossible to customize the intermediate delimiter (or remove the intermediate delimiter ).

If you need to modify (or remove) the delimiter, the simplest method is to replace it directly:

Console. writeline (bitconverter. tostring (New byte [] {1, 13, 54}). Replace ("-",""));

 

However, it is better to define one by yourself:

// Custom output character array method

Static string getbytesstring (byte [] bytes, int index, int count, string SEP)

{

VaR sb = new stringbuilder ();

For (INT I = index; I <count-1; I ++)

{

SB. append (Bytes [I]. tostring ("X2") + SEP );

}

SB. append (Bytes [index + count-1]. tostring ("X2 "));

Return sb. tostring ();

}

 

The above getbytesstring method looks simple and easy to understand. Of course, there are other methods to make it more concise. Let's take a look at the modified getbytesstring and use string. Join and LINQ to get the above Code in one sentence:

// Customize the output character array method (more concise)

Static string getbytesstring (byte [] bytes, int index, int count, string SEP)

{

Return string. Join (SEP, bytes. Skip (INDEX). Take (count). Select (B => B. tostring ("X2 ")));

}

 

As mentioned above, we can try to solve this problem with pure LINQ. Let's look at the following code (a bit cool ):

// Customize the output character array method (pure LINQ. Except for string connection, formatting, and returning strings, these nines are not possible)

Static string getbytesstring (byte [] bytes, int index, int count, string SEP)

{

Return new string (bytes. skip (index ). take (count-1 ). select (B => B. tostring ("X2") + SEP ). selectcenters (I => I. tochararray ()). concat (bytes. skip (index + count-1 ). take (1 ). selectmany (B => B. tostring ("X2 "). tochararray ())). toarray ());

}

 

The above code can output the result correctly, for example:

Console. writeline (getbytesstring (New byte [] {34, 5, 1, 13, 54}, 2, 3, "= "));

Output:

01 = 0d = 36

 

Finally, you can use the inheritance function. net iformatprovider and icustomformatter customize the formatter to provide the output to the byte array, specify the delimiter through the format string, and finally use the string. format to define the custom formatter. The format string is used to output the byte array. Code:

/// <Summary>

/// Custom formatter

/// </Summary>

Class myformatter: iformatprovider, icustomformatter

{

Public object getformat (type formattertype)

{

If (formattertype = typeof (icustomformatter ))

Return this;

Return NULL;

}

Public string format (string format, object Arg, iformatprovider formatprovider)

{

// Determine whether it is a byte array and return corresponding results based on the format string

If (Arg is byte [])

Return getbytesstring (byte []) Arg, format );

Throw new notsupportedexception ();

}

 

Static string getbytesstring (byte [] bytes, string SEP)

{

Return string. Join (SEP, bytes. Select (B => B. tostring ("X2 ")));

}

 

}

 

Public class Program

{

Static void main (string [] ARGs)

{

VaR bytes = new byte [] {34, 13, 43, 92,222 };

// Use string. format to define a formatter (iformatprovider)

Console. writeline (string. format (New myformatter (), "{0: = |=}\ n {1:-} \ n {2}", bytes ));

}

}

Output:

22 = | = 0d = | = 2b = | = 5C = | = de

22-0d-2b-5c-de

220d2b5cde

The results are correct.

 

I have not tested the performance of the above methods. Of course, some methods do not know much about the performance at first glance (for example, the pure LINQ method may be slightly slower to replace strings ).

However, my biggest feeling is that the flexibility of. NET is so ubiquitous that it is interesting to have a good taste.

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.