Convert binary file->utf8->binary file

J

Joey Lee

Hi,
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
FileStream(strdocPath,FileMode.Open,FileAccess.Read);
int len = (int)objfilestream.Length;
Byte[] documentcontents = new Byte[len];
objfilestream.Read(documentcontents,0,len);
objfilestream.Close();
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 = (Byte)text;
}
return result;
}
}

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

Thanks
 
M

Morten Wennevik

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);
fs.Close();
 
J

Jon Skeet [C# MVP]

Joey Lee said:
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.

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top