Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

asp.net - Converting a Byte[] from a Session back to a List<String> in c#

I try to save a List in a Session by converting it into a Byte[]. But when i try to Convert it back to a List i only get some random Numbers. This is the only way i can save the list with the languages in it.

Below the Code where i convert and save it in the session

   private void AllowedLanguages(Userdata User)
        {
            List<string> languages = new List<string>();            

            if (User.Deutsch == true) { languages.Add("Deutsch"); }
            if (User.Englisch == true) { languages.Add("Englisch"); }
            if (User.Franz?sisch == true) { languages.Add("Franz?sisch"); }
            if (User.Italienisch == true) { languages.Add("Italienisch"); }
            if (User.Japanisch == true) { languages.Add("Japanisch"); }
            if (User.Mandarin == true) { languages.Add("Mandarin"); }
            if (User.Polnisch == true) { languages.Add("Polnisch"); }
            if (User.Portugiesisch == true) { languages.Add("Portugiesisch"); }
            if (User.Spanisch == true) { languages.Add("Spanisch"); }

            byte[] LanguagesInBytes = languages
             .SelectMany(s => System.Text.Encoding.ASCII.GetBytes(s))
             .ToArray();

            HttpContext.Session.Set("AllowedLanguages", LanguagesInBytes);
        }

Here is the Code where i try to get it back into an Arraylist or any other type of list with strings is possible

            var LanguagesByte = HttpContext.Session.Get("AllowedLanguages");

            for (int i = 0; i < LanguagesByte.Length; i++)
            {
                String str = new String(LanguagesByte[i].ToString());
                Languages.Add(str);
            }

The List should look like before after i convert it back to display it on a page. Is this even the right way to do it or am i completly wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Currently you're sorting the encoded values as one long byte array, and when you decode them you don't know where they start or end - so if you add a delimiter, you can do this: here I'm using :

byte[] LanguagesInBytes = languages
  .SelectMany(s => System.Text.Encoding.ASCII.GetBytes(s + ""))
  .ToArray();

Then, to decode them:

List<string> output = new List<string>();
string language = string.Empty;
for (int i = 0; i < LanguagesInBytes.Length; i++) {
    var item = LanguagesInBytes[i];
    if (item == 0) {
        output.Add(language);
        language = string.Empty;
    }
    else {
        language += System.Text.Encoding.ASCII.GetString(new[] { item });
    }   
}

Also note that storing the values as Ascii means the extended characters like ? are lost in the process.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...