0

I have the following code where I create a file in memory and upload to S3 bucket.

I'm struggling with UTF8 encoding the file. It is being delivered as a UTF16le.

current code:

 // Create the data to write to the stream.
        byte[] memstring = uniEncoding.GetBytes(fileContent);
        using (MemoryStream memStream = new MemoryStream(10000))

I have tried using this but does not work for me:

            byte[] memstring = Encoding.UTF8.GetBytes(fileContent);

Any help is appreciated.

Tim
  • 563
  • 2
  • 6
  • 20
  • 1
    Do the answers on [this question](https://stackoverflow.com/questions/6198744/convert-string-utf-16-to-utf-8-in-c-sharp) help? I know that's related to a string, but... I'm not sure what's in your file contents. That's probably the biggest question. – ps2goat Sep 13 '19 at 21:28
  • There isn't a easy way. ASCII the characters 0 to 0x7F and standard with all encodings. Character 0x80 to 0xFF are different depending on the type of Encoding. All of the character 0x80 to 0xFF also have a unicode equivalent. ASCII is one byte while unicode is two. Encoding styles were created to avoid using two bytes character (unicode). So to go from UTF16 to UTF8 you would need to use an 8 bit encoding that contains all your unicode characters and then convert the two byte unicode character to one byte equivalent. – jdweng Sep 13 '19 at 22:15

1 Answers1

0

I was using in my attempt to use:

byte[] memstring = Encoding.UTF8.GetBytes(fileContent);

I just had an issue in my testing environment.

Tim
  • 563
  • 2
  • 6
  • 20