将字节数组初始化为某个值,而不是默认的null?

Initialize a byte array to a certain value, other than the default null?(将字节数组初始化为某个值,而不是默认的null?)

本文介绍了将字节数组初始化为某个值,而不是默认的null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙着将一个用 C++ 完成的旧项目改写为 C#.

I'm busy rewriting an old project that was done in C++, to C#.

我的任务是重写程序,使其功能尽可能接近原始程序.

My task is to rewrite the program so that it functions as close to the original as possible.

在一堆文件处理过程中,之前编写此程序的开发人员创建了一个结构,其中包含大量字段,这些字段对应于文件必须写入的设置格式,因此所有这些工作已经为我完成了.

During a bunch of file-handling the previous developer who wrote this program creates a structure containing a ton of fields that correspond to the set format that a file has to be written in, so all that work is already done for me.

这些字段都是字节数组.然后 C++ 代码所做的是使用 memset 将整个结构设置为所有空格字符 (0x20).一行代码.很简单.

These fields are all byte arrays. What the C++ code then does is use memset to set this entire structure to all spaces characters (0x20). One line of code. Easy.

这是非常重要的,因为该文件最终进入的实用程序需要这种格式的文件.我必须做的是将此结构更改为 C# 中的一个类,但我找不到一种方法可以轻松地将这些字节数组中的每一个初始化为所有空格字符.

This is very important as the utility that this file eventually goes to is expecting the file in this format. What I've had to do is change this struct to a class in C#, but I cannot find a way to easily initialize each of these byte arrays to all space characters.

我最终不得不在类构造函数中这样做:

What I've ended up having to do is this in the class constructor:

//Initialize all of the variables to spaces.
int index = 0;
foreach (byte b in UserCode)
{
    UserCode[index] = 0x20;
    index++;
}

这很好用,但我确信必须有更简单的方法来做到这一点.当数组在构造函数中设置为 UserCode = new byte[6] 时,字节数组会自动初始化为默认的空值.有没有办法让它在声明时变成所有空格,这样当我调用我的类的构造函数时,它会像这样立即初始化?还是一些类似 memset 的函数?

This works fine, but I'm sure there must be a simpler way to do this. When the array is set to UserCode = new byte[6] in the constructor the byte array gets automatically initialized to the default null values. Is there no way that I can make it become all spaces upon declaration, so that when I call my class' constructor that it is initialized straight away like this? Or some memset-like function?

推荐答案

对于小数组使用数组初始化语法:

For small arrays use array initialisation syntax:

var sevenItems = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };

对于较大的数组,使用标准的 for 循环.这是最易读和最有效的方法:

For larger arrays use a standard for loop. This is the most readable and efficient way to do it:

var sevenThousandItems = new byte[7000];
for (int i = 0; i < sevenThousandItems.Length; i++)
{
    sevenThousandItems[i] = 0x20;
}

当然,如果您经常需要这样做,那么您可以创建一个帮助方法来帮助您保持代码简洁:

Of course, if you need to do this a lot then you could create a helper method to help keep your code concise:

byte[] sevenItems = CreateSpecialByteArray(7);
byte[] sevenThousandItems = CreateSpecialByteArray(7000);

// ...

public static byte[] CreateSpecialByteArray(int length)
{
    var arr = new byte[length];
    for (int i = 0; i < arr.Length; i++)
    {
        arr[i] = 0x20;
    }
    return arr;
}

这篇关于将字节数组初始化为某个值,而不是默认的null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将字节数组初始化为某个值,而不是默认的null?

基础教程推荐