C#将值插入到arr [英] C# insert values to arr

查看:101
本文介绍了C#将值插入到arr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有来自字节类型=字节[]的arr =新字节[4];
我想插入到编曲,从枚举选择时选择的数量为4位

例如:arr = enumX.y; y = 1011;

如何转换此值(y)并将其插入arr?
我想让arr看起来:arr [0] = 1,arr [1] = 0,arr [2] = 1,arr [3] = 1

在此先感谢

Hi,
I have arr from type byte = byte[] arr=new byte[4];
I want to insert to the arr select from enum when the number of selection is 4 digits

for example: arr=enumX.y; y=1011;

How can I convert and insert this value (y) to the arr?
I want the arr to look : arr[0]=1,arr[1]=0,arr[2]=1,arr[3]=1

Thanks in advance

推荐答案

也许您可以告诉我们您要达到的目标.但是您需要这样的机会很高:

Maybe you can tell us what you want to achive by this. But chances are high you need something like this:

System.BitConverter.GetBytes(enumX.y);



此函数有一些重载,应为您提供常见值类型的单个字节(您的枚举基于整数,如果您未指定其他基本类型,则默认值为int32).

这有帮助吗?


那么,您想要将枚举值转换为单个数字,并将其存储到Byte数组中吗?我找到了一种转换为字符串并将其解析为字节的方法.也许你喜欢吗?



this functions has some overloads and should give you the single bytes for the common value types (your enum is based on an integer, if you didn''t specify another base type the default is int32).

Does this help?


So what you want is to convert the enum values to single digits, and store them into the Byte array? I found a way by converting to a string and parse it as Bytes. maybe you like it?

using System;

namespace EnumValueToDigits
{
    class Program
    {
        public enum SomeEnum
        {
            Y = 1011,
            Z = 1001
        }

        static void Main(string[] args)
        {
            SomeEnum z = SomeEnum.Z;

            byte[] abyZ = EnumDigitsToByteArray(z);

            foreach (byte by in abyZ)
                Console.Write(by);

            Console.ReadKey(false);
        }

        static byte[] EnumDigitsToByteArray(SomeEnum e)
        {
            string strEnumValue = ((int) e).ToString();

            byte[] abyDigits = new byte[strEnumValue.Length];
            for (int i = 0; i < strEnumValue.Length; i++)
            {
                abyDigits[i] = byte.Parse(strEnumValue[i].ToString());
            }
            return abyDigits;
        }
    }
}


尝试此解决方案-您的数字(y)可以除以10,然后将结果加到byte []的末尾.
祝你好运)
Try this solution - your number(y) you can divide by 10 and add result ot the end of byte[].
Good luck)


这篇关于C#将值插入到arr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆