count(byte[]) 为每个项目并将其写入 uint[]
count(byte[]) for each item and writing it to an uint[]
我正在尝试计算一个字节在我的字节数组中出现了多少次以将其写入 uint[] 所以我的输入是。一个写 abcabcabd 的 byte[] arrayToConvert = {97, 98, 99, 97, 98, 99, 97, 98, 100};
。
我试图使用 uint[]
实现的是:
97 = 3 times
98 = 3 times
99 = 2 times
100 = 1 time
所以我想在我的 Class 中这样做:
public static uint[] mCount(byte[] aCount)
{
for (int i = 0; i < aCount.Length; i++)
{
for (int j = i; j < aCount.Length; j++)
{
if (aCount[i] == aCount[j])
{
// somewhere arround here I think I must create the uint[] to return.
// but for this I would need to know howmany different bytes there are.
// not to forget I need to get my counter working to safe howmany of wich byte there are.
uint[] returncount = new uint[ !! number of different bytes !! ];
// foreach to fill the ^ array.
count = count + 1;
}
}
}
return returncount;
}
所以在这一点上我完全卡住了。因此,如果有人可以将我推向正确的方向,那就太好了。或者告诉我在哪里可以读到这方面的内容以便更好地学习。因为我真的好像找不到我能理解的解释。
提前致谢,祝编码愉快!
首先,您应该注意到一个字节的范围是从 0 到 255。
我认为最好的方法之一是声明一个大小为 256 的 int(类型在这里并不重要)数组并将每个元素初始化为 0。
然后,只需将输入数组中的每个元素用作新创建数组的索引并递增其值即可。
最后,int 数组的每个元素都将包含其索引在输入中的出现。
例如:
var aCount = new[] {97, 98, 99, 97, 98, 99, 97, 98, 100};
var occurrences = new int[256];
for (int i = 0; i < aCount.Length; i++)
{
var byteElement = aCount[i];
occurrences[byteElement]++;
}
for (int i = 0; i < occurrences.Length; i++)
if (occurrences[i] != 0)
Console.WriteLine($"{i} = {occurrences[i]} times");
我正在尝试计算一个字节在我的字节数组中出现了多少次以将其写入 uint[] 所以我的输入是。一个写 abcabcabd 的 byte[] arrayToConvert = {97, 98, 99, 97, 98, 99, 97, 98, 100};
。
我试图使用 uint[]
实现的是:
97 = 3 times
98 = 3 times
99 = 2 times
100 = 1 time
所以我想在我的 Class 中这样做:
public static uint[] mCount(byte[] aCount)
{
for (int i = 0; i < aCount.Length; i++)
{
for (int j = i; j < aCount.Length; j++)
{
if (aCount[i] == aCount[j])
{
// somewhere arround here I think I must create the uint[] to return.
// but for this I would need to know howmany different bytes there are.
// not to forget I need to get my counter working to safe howmany of wich byte there are.
uint[] returncount = new uint[ !! number of different bytes !! ];
// foreach to fill the ^ array.
count = count + 1;
}
}
}
return returncount;
}
所以在这一点上我完全卡住了。因此,如果有人可以将我推向正确的方向,那就太好了。或者告诉我在哪里可以读到这方面的内容以便更好地学习。因为我真的好像找不到我能理解的解释。
提前致谢,祝编码愉快!
首先,您应该注意到一个字节的范围是从 0 到 255。
我认为最好的方法之一是声明一个大小为 256 的 int(类型在这里并不重要)数组并将每个元素初始化为 0。
然后,只需将输入数组中的每个元素用作新创建数组的索引并递增其值即可。 最后,int 数组的每个元素都将包含其索引在输入中的出现。
例如:
var aCount = new[] {97, 98, 99, 97, 98, 99, 97, 98, 100};
var occurrences = new int[256];
for (int i = 0; i < aCount.Length; i++)
{
var byteElement = aCount[i];
occurrences[byteElement]++;
}
for (int i = 0; i < occurrences.Length; i++)
if (occurrences[i] != 0)
Console.WriteLine($"{i} = {occurrences[i]} times");