user1323981
user1323981

Reputation:

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 below code

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);
    }
}

But how to get "mnop" back ? What I am missing?

Even C#: How can I safely convert a byte array into a string and back? gave the answer as U3lzdGVtLkJ5dGVbXQ==

Thanks.

Upvotes: 8

Views: 22860

Answers (3)

yamen
yamen

Reputation: 15618

The original request wasn't converting the byt to a string - it was just using it as bytes. So this line:

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

Was actually going to this URL:

Page2.aspx?BytArray=System.Byte[]

You need to change that line to:

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

And then on the way back replace all 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);

With just this:

var byteArray = Convert.FromBase64String(Request.QueryString["BytArray"]);

var x2 = Encoding.UTF8.GetString(byteArray);

Upvotes: 1

Guffa
Guffa

Reputation: 700800

You can't. The code in the first page doesn't sent the value of the byte array, but the data type. Request.QueryString["BytArray"] returns System.Byte[], so it's impossible to get the content of the original byte arrray back.

You can't send bytes as data in the URL without further encoding them. You can for example 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 it's possible to get them:

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

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

You cannot send raw bytes as query string. Try Base64 encoding it 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 retrieve 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 of the whole exercise of converting to byte arrays when you can directly send the string value of the text box as is (after url encoding it of course). If this is some form of a way to hide the real value from the user I hope you are well aware that Base64 is not encryption, it's encoding.

Upvotes: 10

Related Questions