Byte array for UTF8 string

I need to convert an array of bytes to a UTF8 string and store the characters in the array.

I am loading an image using a multi-page message. The image is sent as a UTF8 string. I compared the headers with my application and web browser, and the data is the same except for one thing.

When it is sent along with the browser, the content contains many [] characters, where when does my application replace [] with ?. This means that he does not save the characters as he should. Everything else is the same.

Here is the code I have atm

Byte[] fileOpen = File.ReadAllBytes("C:/pic.jpeg"); postData.AppendLine(System.Text.Encoding.UTF8.GetString(fileOpen)); 

Any tips?

+4
source share
3 answers

The image is sent as a UTF8 string.

Why? UTF-8 is text . Raw binary data should not be encoded, but sent directly in bytes.

If your transfer protocol does not allow bytes, the usual way is to encode bytes in Base64.

+6
source

Do not attempt to send data using anything approaching the text API. You did not say what postData , but try to find some part of your API that deals with binary data streams instead of text data. Searches for methods in the AppendBytes or GetStream strings to get a stream to which you can write your data.

Pretending that arbitrary binary data is text is a bad idea - you will lose data.

EDIT: One way that does not lose data (but is still bad) is to process binary data as an ISO-8859-1-encoded document. IIRC has some debate about what exactly ISO-8859-1 contains in provisions 128-159, but most encodings, at least, assume Unicode 128-159.

Your “UTF-8 decoding” of binary data may look like valid data, because for values ​​0-127 they are the same — it is only above that you will have problems. However, you should still avoid processing this binary data as text. This is not a text, but viewing it as a text is just a recipe for disaster.

If you can publish the headers sent by your browser (including the headings of the part of the multipart corresponding to the image), we can hope to help you a little further, but in the end you should find a way to pass any API that you use (this is also useful information), raw binary data without going through the text.

+2
source

John and the other guys say they don’t believe me. I solved it. Converting it to a string caused problems, but writing it directly to the request stream worked.

 public string solveCaptcha(String username, String password) { String boundry = "---------------------------" + DateTime.Now.Ticks.ToString("x"); StringBuilder postData = new StringBuilder(); postData.AppendLine("--" + boundry); postData.AppendLine("Content-Disposition: form-data; name=\"function\""); postData.AppendLine(""); postData.AppendLine("picture2"); postData.AppendLine("--" + boundry); postData.AppendLine("Content-Disposition: form-data; name=\"username\""); postData.AppendLine(""); postData.AppendLine(username); postData.AppendLine("--" + boundry); postData.AppendLine("Content-Disposition: form-data; name=\"password\""); postData.AppendLine(""); postData.AppendLine(password); postData.AppendLine("--" + boundry); postData.AppendLine("Content-Disposition: form-data; name=\"pict\"; filename=\"pic.jpeg\""); postData.AppendLine("Content-Type: image/pjpeg"); postData.AppendLine(""); StringBuilder postData2 = new StringBuilder(); postData2.AppendLine("\n--" + boundry); postData2.AppendLine("Content-Disposition: form-data; name=\"pict_to\""); postData2.AppendLine(""); postData2.AppendLine("0"); postData2.AppendLine("--" + boundry); postData2.AppendLine("Content-Disposition: form-data; name=\"pict_type\""); postData2.AppendLine(""); postData2.AppendLine("0"); postData2.AppendLine("--" + boundry + "--"); Byte[] fileOpen = File.ReadAllBytes("C:/pic.jpeg"); byte[] buffer = Encoding.ASCII.GetBytes(postData.ToString()); byte[] buffer2 = Encoding.ASCII.GetBytes(postData2.ToString()); HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://poster.decaptcher.com/"); request.ContentType = "multipart/form-data; boundary=" + boundry; request.ContentLength = buffer.Length + buffer2.Length + fileOpen.Length; request.Method = "POST"; String source = ""; using (Stream PostData = request.GetRequestStream()) { PostData.Write(buffer, 0, buffer.Length); PostData.Write(fileOpen, 0, fileOpen.Length); PostData.Write(buffer2, 0, buffer2.Length); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Byte[] rBuf = new Byte[8192]; Stream resStream = response.GetResponseStream(); string tmpString = null; int count = 0; do { count = resStream.Read(rBuf, 0, rBuf.Length); if (count != 0) { tmpString = Encoding.ASCII.GetString(rBuf, 0, count); source += tmpString; } } while (count > 0); } } MessageBox.Show(source); // Do something with the source return source; } 

If you have a deCaptcher account, verify it yourself. If necessary, I will post a video about this to prove my point.

0
source

Source: https://habr.com/ru/post/1301947/


All Articles