Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
I am writing a code which convert an array of byte (it is from a file) to string , then convert string into array of byte again and save it into a new file ...

this is my Console Class , It work without error but result is not correct .

class Program
    public static bool SaveData(string FileName, byte[] Data)
        BinaryWriter Writer = null;
        string Name = FileName;

            // Create a new stream to write to the file
            Writer = new BinaryWriter(File.OpenWrite(Name));

            // Writer raw data
            return false;

        return true;
    static void Main(string[] args)
        byte[] bytes = File.ReadAllBytes(@"C:\Users\UserName\Desktop\File.jpg");
        string result = System.Text.Encoding.UTF8.GetString(bytes);
        byte[] bytes2 = System.Text.Encoding.UTF8.GetBytes(result);

        SaveData(@"C:\Users\UserName\Desktop\File.jpg", bytes2);


why ?! how can I solve it ?! I think some of characters(byte) lose during this convert . Is there any way to convert a file into string and use it another time ?!
CHill60 16-May-14 6:32am    
Why are you converting it into a string just to convert back to a byte array??
What do you mean by "result is not correct"?
MohammadSina Karvandi 16-May-14 6:56am    
because my boss said to me "I want to have a jpg file in ascii format" . I told him it is wrong but he does not accept it . :D It is a mockery work , but ...
Emre Ataseven 16-May-14 8:12am    
What do you want to do with ascii of jpg? do u want to apply a special filter?


You should be using Base64 encoding instead. Please have a look at this[^] article.

Share this answer
meitei_lairen 19-May-17 23:49pm    
I just wanted to store byte array into sting. Actually I have video file that was around 50-100 MB using mysql. The problem is that string can't hold byte array data. How could I rectify this issue?
When you convert Byte values to Char (or vice versa) you are applying a translation to them: not all byte values translate to char values, so you get "defaulted" values for some characters. And not all characters translate to distinct bytes either...

So if you take bytes, convert then to a string, and then convert that back to bytes, the chances are very good that you won't get the original byte values back again - you may not even get the same number of bytes! Particularly with a file type such as jpeg, this can completely ruin your image...

What are you trying to do that you think this is a good idea?
Share this answer
MohammadSina Karvandi 16-May-14 7:02am    
thank you for your answer ... yeah I know , you are right . but my Company manager said to me "I want to have a jpg file in ascii format" . I told him it is wrong but he does not accept it . I am searching to solve it .
OriginalGriff 16-May-14 7:14am do realize that UTF8 is not ASCII? ASCII is 7 bit, and will make the problem even worse as it will discard half of your data... :laugh:

I suspect that what the manger has asked for is not what he wants: I would guess that what he actually wants is something he can transfer as text and regenerate as a JPG file at the other end, which is a very different beastie - and pretty easy to do as well.

Go and ask him what he wants to do with it - explain that ASCII will destroy the file if you have to - and find out what he actually needs. Then I'll explain how to do it! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900