Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friend:

I have a string that comes from a textbox. I want to write this string in the format of ASCI to a file. I use various method but i do not get any result.
for example:

C#
private static Encoding encoding = Encoding.ASCII;

private static byte[] CSTB( string cod, Encoding enc )
{
    byte[] transformacion = enc.GetBytes( cod );
    return transformacion;
}

private void btn_CreateNewCard_Click( object sender , EventArgs e )
{
FileInfo finfo = new FileInfo( path );


    using(FileStream fs = finfo .Open( FileMode .Open ,FileAccess .Write ,   FileShare .ReadWrite ))
    try
    {

        fs .Write( CSTB( "Moha",encoding  ),0, CSTB( "Moha",encoding  ).Lenght);
     }
}


Is that my code correct?
Why I see the original text ("Moha") when I click my button?

Yours
Posted
Updated 9-Mar-12 5:40am
v4

Why you want to write the file in ascii format?

Try this.
File.WriteAllText(pathOfFile, "Moha", Encoding.ASCII);
 
Share this answer
 
Comments
Mmohmmad 9-Mar-12 12:00pm    
Dear Mate:

Thanks for your reply. I want to write it as a ASCII in order to hide its content from the user that opens the file. Do you have any suggestion?

I try to write the string as bytes but when i write, it completey is as the original string.
[no name] 9-Mar-12 12:10pm    
What makes you think that writing an ASCII string to a file hides it?
Mmohmmad 9-Mar-12 14:20pm    
Dear Aday:

I think there is a misconception. I do not mean hide the string in the file.
I mean that make string not underestandable or at least hard to identify.
[no name] 9-Mar-12 14:47pm    
Yes I get that. Writing as ASCII string to a file does not make it not understandable to the user. You have to do something else other than writing the string as ASCII values.
Mmohmmad 10-Mar-12 0:52am    
Dear My Friend Aday:

Would you please suggest me a method to do this or introduce me a book to handle this?


Thanks.
first understand what ASCII mean.read this link.


if u are expecting output like 4D-4F-6F-6E.........
u r getting each character's hex value.

read this to get hex value of each character in ascii.


To achieve above using c# try this code:


<pre lang="c#">
string EncodedString=BitConverter.ToString(Encoding.ASCII.GetBytes(<ur string>));
 
Share this answer
 
v2

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