转换音乐文件到字节数组c#像matlab
本文关键字:matlab 数组 到字节 音乐 文件 转换 | 更新日期: 2023-09-27 18:04:08
我想将任何音乐文件转换为字节数组,并在c#中打印结果,就像在MATLAB中一样。
I tried this;
System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);
long _TotalBytes = new System.IO.FileInfo(_FileName).Length;
_Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes);
和要打印到控制台的代码:
Console.WriteLine( byteArrayToString(fileToByteArray("Penguins.jpg")) );
方法的代码是:
private static string byteArrayToString(byte[] p)
{
string result = System.Text.ASCIIEncoding.ASCII.GetString(p);
return result;
}
当我运行这段代码,控制台变得疯狂与不相关的字符,但我想有一个数组像MATLAB的输出。
我该怎么做呢?
谢谢。
如果您只想要任何文件的字节数组,只需执行以下操作:
byte[] byteArrayFile = File.ReadAllBytes(source);
//source is the path to the file
你真的需要字符吗?如果你只想要数字,用数字构建一个字符串。我不知道matlab格式,但是对于像[N1,N2,N3]这样的行向量
private static string ByteArrayToString(byte[] p)
{
StringBuilder sb = new StringBuilder("[");
for(int i=0; i<p.Length; i++)
{
sb.Append(p[i]);
if (i < p.Length - 1)
sb.Append(',');
}
sb.Append("]");
return sb.ToString();
}