Convert byte array to string in Asp.net

In Page1.aspx I have

byte[] byt = System.Text.Encoding.UTF8.GetBytes(TextBox1.Text); Response.Redirect("Page2.aspx?BytArray=" + byt,false); 

The value of TextBox1 is "mnop".

Now in Page2.aspx I have the code below

 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { var byteArray = System.Text.Encoding.UTF8.GetBytes(Request.QueryString["BytArray"]); var x1 = System.Convert.ToBase64String(byteArray, 0, byteArray.Length); var x2 = Encoding.UTF8.GetString(byteArray); } } 
  • x1 output U3lzdGVtLkJ5dGVbXQ ==

  • x2 output of System.Byte []

But how to return "mnop"? What am I missing?

Even C #: how can I safely convert an array of bytes to a string and vice versa? gave the answer as U3lzdGVtLkJ5dGVbXQ ==

Thanks.

+4
source share
3 answers

You cannot send raw bytes as a query string. Try Base64 instead:

 byte[] byt = System.Text.Encoding.UTF8.GetBytes(TextBox1.Text); string encoded = HttpUtility.UrlEncode(Convert.ToBase64String(byt)); Response.Redirect("Page2.aspx?BytArray=" + encoded, false); 

and then bring it back:

 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { byte[] byteArray = Convert.FromBase64String(Request.QueryString["BytArray"]); string value = System.Text.Encoding.UTF8.GetString(byteArray); } } 

But I really don’t see the point in the whole exercise of converting to byte arrays, when you can directly send the string value of the text field as it is (after encoding it, of course). If this is some way of hiding the real value from the user, I hope you are well aware that Base64 is not a cipher, it encodes.

+6
source

The initial request did not convert byt to string - it just used it as bytes. So this line:

 Response.Redirect("Page2.aspx?BytArray=" + byt,false); 

Actually there was this URL:

 Page2.aspx?BytArray=System.Byte[] 

You need to change this line to:

 Response.Redirect("Page2.aspx?BytArray=" + HttpUtility.UrlEncode(System.Convert.ToBase64String(byt)), false); 

And then on the way back, replace all of this:

 var byteArray = System.Text.Encoding.UTF8.GetBytes(Request.QueryString["BytArray"]); var x1 = System.Convert.ToBase64String(byteArray, 0, byteArray.Length); var x2 = Encoding.UTF8.GetString(byteArray); 

Only with this:

 var byteArray = Convert.FromBase64String(Request.QueryString["BytArray"]); var x2 = Encoding.UTF8.GetString(byteArray); 
+1
source

You can not. The code on the first page does not send the value of a byte array, but a data type. Request.QueryString["BytArray"] returns System.Byte[] , so it is not possible to return the contents of the original byte.

You cannot send bytes as data in URLs without further encoding them. For example, you can use base64:

 byte[] byt = System.Text.Encoding.UTF8.GetBytes(TextBox1.Text); Response.Redirect("Page2.aspx?BytArray=" + Server.UrlEncode(Convert.ToBase64String(byt)), false); 

Now the URL contains the actual bytes, so you can get them:

 byte[] byteArray = Convert.FromBase64String(Request.QueryString["BytArray"]); string x = System.Text.Encoding.UTF8.GetString(byteArray); 
0
source

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


All Articles