) C # convert array to string or string array

Source: Internet
Author: User
Tags array to string

1. Convert the string into a byte array
String imgData = "....,...,....,....";
String [] imgArr = imgData. Split (new char [] {','});
Byte [] bty = Array. ConvertAll <string, byte> (imgArr, delegate (string s) {return byte. Parse (s );});

2. Convert the byte array into a string
There are two main methods:
String. Join (): Concatenates the specified separator String between each element of the specified String array to generate a single concatenation String.
Array. ConvertAll (): converts one type of Array to another type of Array.
For example, the ArraySource (byte) elements in the target array are separated by commas (,) and the output string is used.
String result = String. Join (",", Array. ConvertAll (ArraySource, (Converter <byte, string>) Convert. ToString ));

----------------------------------------------------------------------------
// Image --> Byte [] --> String
Byte [] bytes = File. ReadAllBytes (@ "d: \ a.gif ");
MemoryStream MS = new MemoryStream (bty );
String imgStr = Convert. ToBase64String (ms. ToArray ());

// String --> Byte [] --> Image
Byte [] imgBytes = Convert. FromBase64String (imgStr );
Response. BinaryWrite (imgBytes. ToArray (); // write a binary string to the HTTP output stream

Related Article

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.