473,671 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert binary file->utf8->binary file


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(str ing DocumentName)
{
string strdocPath;
strdocPath = DocumentName;
FileStream objfilestream = new
FileStream(strd ocPath,FileMode .Open,FileAcces s.Read);
int len = (int)objfilestr eam.Length;
Byte[] documentcontent s = new Byte[len];
objfilestream.R ead(documentcon tents,0,len);
objfilestream.C lose();
return documentcontent s;
}
public static string FromUTF8ByteArr ay(Byte[] characters)
{
System.Text.UTF 8Encoding encoding = new System.Text.UTF 8Encoding();
string constructedStri ng = encoding.GetStr ing(characters) ;
return (constructedStr ing);
}

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 =
RawEncoding.Get Bytes(FromUTF8B yteArray(GetDoc ument(FILE_NAME )));
Byte[] finalBytes = Encoding.Conver t(Encoding.UTF8 , Encoding.Unicod e,
utf8Bytes);
string fileName = "test.gif";
FileStream fst = new FileStream(file _name, FileMode.Create );
fst.Write(final Bytes, 0, finalBytes.Leng th);
fst.Flush();
fst.Close();

Thanks

Nov 17 '05 #1
2 11200
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.G etBytes(s);
FileStream fs = File.Create("Ne wFile.gif");
fs.Write(data, 0, data.Length);
fs.Close();
--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2
Joey Lee <jo*********@ho tmail.com> wrote:
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.ToBase6 4String and Convert.FromBas e64String).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
18322
by: Spamtrap | last post by:
I only work in Perl occasionaly, and have been searching for a solution for a conversion, and everything I found seems much too complex. All I need to do is take a simple text file and copy it, however some specific lines are in fact in UTF8 as printed garbagy characters and they need to be converted to Unicode, so that the new text file can be imported into a desktop program and into some Word documents. For the moment I would be...
2
12101
by: Mark Anderson | last post by:
Hi, I've a problem with code that should produce a Windows(ANSI) encoded text file but doesn't. Server is IIS 5 on Win 2k, with ASP ver? My ASP uses data from an upstream HTML form on a UTF-8 encoded page. The latter is output by a server-side system and I can't alter that source format. Here's some of the ASP code (may wrap): ' Following vars are longer strings but of this type - just more Request
2
9916
by: geekboyed | last post by:
I'm outputting a file from a mySQL server that contains 1 column that has input it many languages. The base definition for the column is UTF8. I output the data into a flat file and bcp it to a sql2000 server, and the multi-byte data does not survive the translation. Any ideas on how to input a UTF8 flat file into the server using BCP? I've tried the -n -N -w options -- no luck.
2
3036
by: Craig | last post by:
I have the need to write a byte of information to a specific location in a text file. eg. the file looks something like this. FYYNN Line 1 Line 2 <eof>
1
12682
by: Hooyoo | last post by:
I read the content from a file encoded with UTF8 like this: byte data = binaryReader.ReadBytes(file.Length); And next step, I want to transform data to ASCII format, How can I do this? You know, Using Encoding.UTF8.GetString(data) can get a ASCII string, but I need a byte. I hope you get my point. Any suggestion will be appreciated.
4
4883
by: EmeraldShield | last post by:
(Dot Net 2 C# application - using Encoding.UTF8 with a StreamReader) I have a very strange problem that I cannot explain with a UTF8 Readline() although this could exist in other types of encoding, I have not tried them. Our application wrote this sequence to a UTF8 file. Now I am loading it back and the text is not coming back in the same as it went out. DATA: from: processfrom checkemail failed: 501 syntax error in parameters:...
1
10043
by: nalinibala | last post by:
I'm having issues using XmlTextWriter, saving it out to a file with UTF8 encoding, and seeing "human unreadable" characters show up *right before* the XML declaration. I need to have the XML declaration state "encoding = utf-8", but also get rid of the dirty characters. I was hoping that using System.Text.Encoding.UTF8 in the constructor of the XmlTextWriter class would prove nice and simple, but I'm finding out I'm having some issues with...
1
11100
by: John Nagle | last post by:
I have a large (gigabytes) file which is encoded in UTF-8 and then compressed with gzip. I'd like to read it with the "gzip" module and "utf8" decoding. The obvious approach is fd = gzip.open(fname, 'rb',encoding='utf8') But "gzip.open" doesn't support an "encoding" parameter. (It probably should, for consistency.) Is there some way to do this? Is it possible to express "unzip, then decode utf8" via "codecs.open"?
3
7472
by: Jake | last post by:
I am writing a script that allows people to upload a CSV file for processing and I am having trouble with accented characters. If a UTF-8 encoded file is uploaded, everything works fine. The problem happens when someone uploads a non UTF8 file that happens to have an accented character inside, like é. In this case, the character is converted to a funky symbol, like �. I noticed this explanation in the PHP documentation for the...
7
92442
by: firepol | last post by:
Hello there, I am dealing with files encoded in UTF8 and I can't find a way to convert them into ANSI. I've already searched in google for this since a while, and I'm not achieving the result I want to achieve if I use the code I've found on the web, which is the following: Example of test.txt (save it as UTF8): "éàèüöä" string filePath = "c:\\test.txt"; StreamReader fileStream = new StreamReader(filePath); string fileContent =...
0
8919
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8821
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8599
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8670
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6230
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4409
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2813
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2052
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.