C# 使用ReadByte()读取泛型类型

C# 使用ReadByte()读取泛型类型,c#,algorithm,generics,data-structures,generic-programming,C#,Algorithm,Generics,Data Structures,Generic Programming,我是一名c#初学者,试图读取二进制文件中的符号。我在c#中使用ReadByte()读取这个二进制文件的唯一参数 使用(var stream=newbinaryreader(System.IO.File.OpenRead(args[0])) 在那之后我会 while (stream.BaseStream.Position < stream.BaseStream.Length) { int symbol = stream.ReadByte(); //And then i d

我是一名c#初学者,试图读取二进制文件中的符号。我在c#中使用ReadByte()读取这个二进制文件的唯一参数

使用(var stream=newbinaryreader(System.IO.File.OpenRead(args[0]))

在那之后我会

 while (stream.BaseStream.Position < stream.BaseStream.Length) 
 {
   int symbol = stream.ReadByte(); 
   //And then i do my stuff
 }   
错误是:(为了调试,我试图在构造函数中打印check1和check2,但它只打印“check1”而不是“check2”)。请在输出结束前:

hp@ubuntu:~/Desktop/Internship_Xav/templatescplus$ mono test.exe toto.bin 
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1
check1

Unhandled Exception: System.ArgumentException: Destination array is not long enough to copy all the items in the collection. Check array index and length.
  at System.BitConverter.PutBytes (System.Byte* dst, System.Byte[] src, Int32 start_index, Int32 count) [0x00000] in <filename unknown>:0 
  at System.BitConverter.ToInt64 (System.Byte[] value, Int32 startIndex) [0x00000] in <filename unknown>:0 
  at shekhar_final_version_Csharp.Huffman`1[System.Int64]..ctor (System.String[] args, System.Func`3 converter) [0x00000] in <filename unknown>:0 
  at shekhar_final_version_Csharp.MyClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: Destination array is not long enough to copy all the items in the collection. Check array index and length.
  at System.BitConverter.PutBytes (System.Byte* dst, System.Byte[] src, Int32 start_index, Int32 count) [0x00000] in <filename unknown>:0 
  at System.BitConverter.ToInt64 (System.Byte[] value, Int32 startIndex) [0x00000] in <filename unknown>:0 
  at shekhar_final_version_Csharp.Huffman`1[System.Int64]..ctor (System.String[] args, System.Func`3 converter) [0x00000] in <filename unknown>:0 
  at shekhar_final_version_Csharp.MyClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
hp@ubuntu:~/Desktop/Internship_Xav/templatescplus$
hp@ubuntu:~/Desktop/interporation\u Xav/templatescplus$mono test.exe toto.bin
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
支票1
未处理的异常:System.ArgumentException:目标数组不够长,无法复制集合中的所有项。检查数组索引和长度。
在System.BitConverter.PutBytes处(System.Byte*dst,System.Byte[]src,Int32开始索引,Int32计数)[0x00000]in:0
在System.BitConverter.ToInt64(System.Byte[]值,Int32 startIndex)[0x00000]中:0
在shekhar_最终版本_Csharp.Huffman`1[System.Int64]…ctor(System.String[]args,System.Func`3转换器)[0x00000]中:0
在shekhar_最终版本_Csharp.MyClass.Main(System.String[]args)[0x00000]中:0
[错误]致命的未处理异常:System.ArgumentException:目标数组不够长,无法复制集合中的所有项。检查数组索引和长度。
在System.BitConverter.PutBytes处(System.Byte*dst,System.Byte[]src,Int32开始索引,Int32计数)[0x00000]in:0
在System.BitConverter.ToInt64(System.Byte[]值,Int32 startIndex)[0x00000]中:0
在shekhar_最终版本_Csharp.Huffman`1[System.Int64]…ctor(System.String[]args,System.Func`3转换器)[0x00000]中:0
在shekhar_最终版本_Csharp.MyClass.Main(System.String[]args)[0x00000]中:0
hp@ubuntu:~/Desktop/interporation\u Xav/templatescplus$

您必须将转换函数和数字类型的大小传递到
A的构造函数中

public A(string[] args, int size, Func<byte[], int, T> converter) { ... }
 A<long> ObjSym = new A<long>(args, sizeof(long), BitConverter.ToInt64)
然后,您可以从读卡器读取所需的字节数并进行转换:

byte[] bytes = stream.ReadBytes(size);
T processingValue = converter(bytes, 0);
您可以使用
Equals
检查符号:

if (pt.symbol.Equals(processingValue))
如果进一步约束类型参数以实现
IEquatable
,则可以避免在相等检查期间装箱:

公共类A,其中T:struct、IComparable、IEquatable
{
公共类节点
{
公共T符号;
公共节点下一步;
公共国际频率;
}
公共节点前端;
公共A(字符串[]参数,Func转换器)
{
int size=Marshal.SizeOf(typeof(T));
Front=null;
使用(var stream=newbinaryreader(System.IO.File.OpenRead(args[0]))
{                    
while(stream.BaseStream.Position
“二进制符号的形式为(11010101等),“感谢您消除了我的困惑。”您总是想将一个字节转换为
T
,还是根据类型从多个字节读取它?关于您的注释“//这是问题所在”,您可以。您可以改用
EqualityComparer.Default.Equals(pt.symbol,processingValue)
。不确定这是否是您试图在问题中得到回答的问题。@Lee无论您指定的整数类型的长度是多少(uint/long等),我的代码都可以工作,只要是8位的倍数,代码就永远不会进入内部,而无
pt.symbol
null
则抛出
null引用异常。我建议改为使用
EqualityComparer.Default.Equals(pt.symbol,processingValue)
。@ChrisSinclair-看起来
T
总是一个结构,所以你可以添加另一个约束。@Lee什么是8位的倍数?@user234839-我不确定你的意思。@Lee:嗯,是的,可能,但听起来像“int/uint/long等”意味着它不一定与结构绑定。如果是这样的话,那么是的,user234839可以将其约束为structs,而不必担心这一点。
int size = Marshal.SizeOf(typeof(T));
byte[] bytes = stream.ReadBytes(size);
T processingValue = converter(bytes, 0);
if (pt.symbol.Equals(processingValue))
public class A<T> where T : struct, IComparable <T>, IEquatable<T>
{
    public class Node
    {
        public T symbol;
        public Node next;
        public int freq;
    }

    public Node Front;

    public A(string[] args, Func<byte[], int, T> converter)
    {
        int size = Marshal.SizeOf(typeof(T));
        Front = null;
        using(var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) 
        {                    
            while (stream.BaseStream.Position < stream.BaseStream.Length) 
            {
                byte[] bytes = stream.ReadBytes(size);
                T processingValue = converter(bytes, 0);

                Node pt, temp;
                pt = Front;
                while (pt != null)
                {
                    if (pt.symbol.Equals(processingValue))
                    {
                        pt.freq++;
                        break;
                    }
                    temp = pt;
                    pt = pt.next;
                }
            }
        }
    }
}