我使用以下函数将二进制数据转换为文本
public string BinaryToText(byte[] data)
{
MemoryStream stream = new MemoryStream(data);
StreamReader reader = new StreamReader(stream, encoding.UTF8);
string text = reader.ReadTod();
return text;
}
Run Code Online (Sandbox Code Playgroud)
但OutOfMemoryException显示30Mb数据.如何解决这个问题并使用这个或更好的方法转换大于100Mb的数据?
之前已经多次回答了一般方法,但是我的实现方法存在问题,但是我想看看一个好的读者是否可以找到我出错的地方.
代码和测试是;
[TestMethod]
public void FloatConversion()
{
// Set up some test data
int repetitions = 100000;
Random rand = new Random();
float[] testSetOfFloats = new float[repetitions];
for (int count = 0; count < repetitions; count++)
{
testSetOfFloats[count] = rand.NextFloat(0, float.MaxValue);
}
// Convert the floats into a byte array
byte[] floatsAsByteArray = new byte[repetitions * 4]; // 4 bytes for a Single
for (int count = 0; count < repetitions; count++)
{
byte[] floatAsBytes = BitConverter.GetBytes(testSetOfFloats[count]);
floatAsBytes.CopyTo(floatsAsByteArray, count * …Run Code Online (Sandbox Code Playgroud) 在C++中,我可以使用指针算法来获取从数组的起始位置到结尾的所有内容.你在C#做什么来完成同样的事情?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static string GetText(byte[] value, uint startPos)
{
// TODO - What goes here?
return "";
}
static void Main(string[] args)
{
byte[] value = new byte[] { 0x41, 0x42, 0x42, 0x42 };
string result = GetText(value, 1);
Console.WriteLine(result);
}
}
}
Run Code Online (Sandbox Code Playgroud)
期待输出:BBB
在通过过滤将Datatable转换为Dataview时,查询我的SQL数据库以获取数据到Dataset,因为作为Linq的新手,我收到了基本的转换错误.
码:
DataView cktDv = (from clounceform in dsclounceForms.Tables["Table2"].AsEnumerable()
where clounceform.Field<string>("Form_FileType_ID").Equals("5")
select clounceform).CopyToDataTable().AsDataView();
Run Code Online (Sandbox Code Playgroud)
例外是
Unable to cast object of type 'System.Byte' to type 'System.String'.
System.Data.DataSetExtensions
at System.Data.DataRowExtensions.UnboxT`1.ReferenceField(Object value)
at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
at ClounceFormsSuite.Design.CreateFormDesign.<>c.<CreateForm>b__12_0(DataRow bounceform) in myprojectfolderpath\CreateFormDesign.xaml.cs:line 115
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Data.DataTableExtensions.LoadTableFromEnumerable[T](IEnumerable`1 source, DataTable table, Nullable`1 options, FillErrorEventHandler errorHandler)
at System.Data.DataTableExtensions.CopyToDataTable[T](IEnumerable`1 source)
at ClounceFormsSuite.Design.CreateFormDesign.CreateForm(String[] cktFileContent, String filePath, String formID) in myprojectfolderpath\CreateFormDesign.xaml.cs:line 114
at ClounceFormsSuite.Design.CreateFormDesign.UserControl_Loaded(Object sender, RoutedEventArgs e) in myprojectfolderpath\CreateFormDesign.xaml.cs:line 70
Run Code Online (Sandbox Code Playgroud)
我无法继续进行我实际上将字符串转换为int并给出了不同的错误,这会误导我更多当尝试转换为int时
DataView cktDataView = (from clounceform in dsclounceForms.Tables["Table2"].AsEnumerable()
where …Run Code Online (Sandbox Code Playgroud)