将数据字符串转换为byte [] [英] Convert data string to byte[]

查看:66
本文介绍了将数据字符串转换为byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi ..

如何将数据字符串转换为byte []。我在c#中下载文件。但我得到一个错误不能隐式转换类型''字符串''''字节[]''我怎么能解决这个问题...



这是我的样本编码

  string  filename = filename; 

字节 [] bytes =附件;
尝试
{
Response.Clear();
Response.AddHeader( Content-Disposition attachment; filename = \ + filename + \ );
Response.AddHeader( Content-Length,bytes.Length.ToString()) ;
Response.ContentType = application / octet-stream;
Response.BinaryWrite(bytes);
Response.Flush();
}
catch (例外情况)
{
Response.ContentType = text / plain;
Response.Write(ex.Message);
}
最后
{
Response.End();
}



i在字节错误[] bytes =附件





先谢谢

解决方案

string to byte []:

  string  s = ... 
byte [] bytes = System.Text.Encoding.ASCII .GetBytes(一个或多个);

根据您的文件写入内容,您可能希望使用其他编码选项之一而不是ASCII。也许是Unicode,UTF7或UTF8。






使用 Encoding.GetBytes方法 [ ^ ]

  byte  [] array =编码。 yourEncoding  .GetBytes(stringData); 



更改 yourEncoding 到你想要使用的编码中。

例如:

  byte  [] array = Encoding.UTF8.GetBytes(stringData); 





另一种方式:



  byte  [] array = Encoding.GetEncoding(  encodingName)。GetBytes(stringData); 



例如:

  byte  [] array = Encoding.GetEncoding(  utf-32) .GetBytes(stringData); 



希望这有帮助。




byte [] bytes =附件;

hi..
how to convert data string to byte[]. i doing downloading file in c#. but i got an error cannot implicit convert type ''string'' to ''byte[]'' how can i solve this problem...

this is my sample coding

string filename = filename;

           Byte[] bytes = attachment;
           try
           {
               Response.Clear();
               Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
               Response.AddHeader("Content-Length", bytes.Length.ToString());
               Response.ContentType = "application/octet-stream";
               Response.BinaryWrite(bytes);
               Response.Flush();
           }
           catch (Exception ex)
           {
               Response.ContentType = "text/plain";
               Response.Write(ex.Message);
           }
           finally
           {
               Response.End();
           }


i got an error at Byte[] bytes = attachment


Thanks in advance

解决方案

string to byte[]:

string s = ...
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);

Depending on what your file was written with, you may want to use one of the other Encoding options instead of ASCII. Unicode, UTF7 or UTF8 perhaps.


Hi,

Use the Encoding.GetBytes Method[^]

byte[] array = Encoding.yourEncoding.GetBytes(stringData);


Change yourEncoding into the encoding you want to use.
For example:

byte[] array = Encoding.UTF8.GetBytes(stringData);



Another way:

byte[] array = Encoding.GetEncoding("encodingName").GetBytes(stringData);


For example:

byte[] array = Encoding.GetEncoding("utf-32").GetBytes(stringData);


Hope this helps.


write
byte[] bytes = attachment;


这篇关于将数据字符串转换为byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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