Storing a string as UTF8 in C#

前端 未结 4 734
忘了有多久
忘了有多久 2021-02-01 14:30

I\'m doing a lot of string manipulation in C#, and really need the strings to be stored one byte per character. This is because I need gigabytes of text simultaneously in memory

4条回答
  •  星月不相逢
    2021-02-01 15:08

    Well, you could create a wrapper that retrieves the data as UTF-8 bytes and converts pieces as needed to System.String, then vice-versa to push the string back out to memory. The Encoding class will help you out here:

    var utf8 = Encoding.UTF8;
    byte[] utfBytes = utf8.GetBytes(myString);
    
    var myReturnedString = utf8.GetString(utfBytes);
    

提交回复
热议问题