将字符串以ASCII格式写入数据文件 [英] Write a string as a ASCII format to a data file

查看:106
本文介绍了将字符串以ASCII格式写入数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友:

我有一个来自文本框的字符串.我想将此字符串以ASCI格式写入文件.我使用了各种方法,但没有得到任何结果.
例如:

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:

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);
     }
}



我的代码正确吗?
为什么单击按钮时会看到原始文本("Moha")?

Yours



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

Yours

推荐答案

为什么要以ascii格式写入文件?

试试这个.
Why you want to write the file in ascii format?

Try this.
File.WriteAllText(pathOfFile, "Moha", Encoding.ASCII);


首先了解ASCII的含义.请阅读链接.


如果您期望输出像4D-4F-6F-6E .........
您正在获取每个字符的十六进制值.

阅读以获取ascii中每个字符的十六进制值.


要使用c#实现以上目标,请尝试以下代码:


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


这篇关于将字符串以ASCII格式写入数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆