Convert binary file->utf8->binary file

Convert binary file->utf8->binary file

Post by Joey Le » Sat, 23 Apr 2005 18:59:34

Does anyone know how I am able to write a utf-8 encoded binary string into
binary file?
Currently I am given a UTF-8 string which was read from a gif image.

Here are my functions...

public Byte[] GetDocument(string DocumentName)
string strdocPath;
strdocPath = DocumentName;
FileStream objfilestream = new
int len = (int)objfilestream.Length;
Byte[] documentcontents = new Byte[len];
return documentcontents;

public static string FromUTF8ByteArray(Byte[] characters)
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
string constructedString = encoding.GetString(characters);
return (constructedString);

public class RawEncoding
public static Byte[] GetBytes(string text)
Byte[] result = new Byte[text.Length];
for(int i = 0; i < text.Length; ++i)
result[i] = (Byte)text[i];
return result;

Byte[] utf8Bytes =
Byte[] finalBytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode,
string fileName = "test.gif";
FileStream fst = new FileStream(file_name, FileMode.Create);
fst.Write(finalBytes, 0, finalBytes.Length);


Convert binary file->utf8->binary file

Post by Morten Wen » Sat, 23 Apr 2005 19:22:43

Hi Joey,

I'm not sure you can safely convert a binary file to UTF8 without loss of data, but the procedure to convert the data to a file is to retrieve the bytes and feed them to a FileStream

string utf8String = // some value

byte[] data = Encoding.UTF8.GetBytes(s);
FileStream fs = File.Create("NewFile.gif");
fs.Write(data, 0, data.Length);

Happy coding!
Morten Wennevik [C# MVP]


Convert binary file->utf8->binary file

Post by Jon Skeet » Tue, 26 Apr 2005 15:16:33

That's a really, really bad idea. You can't just read arbitrary data
and assume it's a valid UTF-8 encoded string.

If you wish to turn arbitrary data into text, I suggest you use Base64
(see Convert.ToBase64String and Convert.FromBase64String).

Jon Skeet - < XXXX@XXXXX.COM > ~skeet
If replying to the group, please do not mail me too