Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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

C#
System.BitConverter.GetBytes(enumX.y);


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?

C#
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;
        }
    }
}
 
Share this answer
 
v3
Comments
johannesnestler 23-Oct-13 10:58am    
after reading your question again, I think I did a mistake - you don't want the single Bytes sorry for wrong answer. maybe the simple solution suggested by alexander is what you Need!
Member 10304617 23-Oct-13 11:20am    
Thank you
Try this solution - your number(y) you can divide by 10 and add result ot the end of byte[].
Good luck)
 
Share this answer
 
Comments
johannesnestler 23-Oct-13 10:51am    
what? How can this work?
Alexander Dymshyts 23-Oct-13 10:56am    
when you divide 1011 by ten you get 101.1. Then you can round to 101 and get value after point(in this case 1) and add it to the end of byte[]. And so on in loop. In this case you need to run loop 4 times.
johannesnestler 23-Oct-13 10:58am    
ok my fault, should read the question better - sorry
johannesnestler 23-Oct-13 10:57am    
ok my fault, should read the question better - sorry
Alexander Dymshyts 23-Oct-13 10:59am    
Not a problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900