Results 1 to 3 of 3

Thread: [RESOLVED] [2.0] String from List<byte>

  1. #1

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Resolved [RESOLVED] [2.0] String from List<byte>

    I have a List of bytes that I want to convert to a string. Currently I am doing this:
    Code:
    // myList is a generic List<byte>
    
    string myString = "";
    
    foreach (byte byt in myList)
    {
        myString += (char)byt;
    }
    
    ConsoleWriteLine(myString);
    Is there a better alternative I can use that will convert all the bytes in one go without the foreach loop?

    At the moment the list will hold a max. of around 25 bytes.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] String from List<byte>

    This is untested but I think it should work:
    vb Code:
    1. string myString = System.Text.Encoding.ASCII.GetString(myList.ToArray());
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: [2.0] String from List<byte>

    Quote Originally Posted by jmcilhinney
    I think it should work
    Spot on.

    Thanks.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width