Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I have a method that will check a character input, and according to the entered character will return an array of bytes consisting of 1's and 0's
(i.e) in case 2 is passed to the method I will get.
b[0] = 0
b[1] = 0
b[2] = 0
b[3] = 0
b[4] = 1
b[5] = 0
 
The method I tried to implement is returning the ascii equivalent of the 1's and 0's
Is there another way instead of using the below code?
 
private byte[] determineOutput(char c)
        {
            //byte[] b = new byte[6];
            string s="";
            System.Text.Encoding enc = System.Text.Encoding.ASCII;
 
            switch (c)
            {
                case '0':
                    s = "000000";
                    break;
                case '1':
                    s= "000001";
                    break;
                case '2':
                    s = "000010";
                    break;
                case 'A':
                    s = "001010";
                    break;
                default:
                    Console.WriteLine("Invalid Characther");
                    break;
            }
 
            byte[] b = enc.GetBytes(s);
 
            for (int i = 0; i < b.Length; i++)
            {
                Console.Write(b[i]);
            }
            Console.WriteLine();
 
            return (b);
        }
Posted 18-Nov-12 0:10am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Have you looked at using a BitArray[^] instead?
 
Alternatively, you could do it like this:
byte[] outp = new byte[5];
for (int i = 0; i < 5; i++)
   {
   outp[i] = (byte) inp & 0x20;
   inp << 1;
   }
return outp;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

This method solved the issue
 
private byte[] determineOutput(char c)
        {
            byte[] b = new byte[6];
            //byte b = 11;

            switch (c)
            {
                case '0':
                    b = new byte[6] { 0, 0, 0, 0, 0, 0 };
                    break;
                case '1':
                    b = new byte[6] { 0, 0, 0, 0, 0, 1 };
                    break;
                case '2':
                    b = new byte[6] { 0, 0, 0, 0, 1, 0 };
                    break;
                case 'A':
                    b = new byte[6] { 0, 0, 1, 0, 1, 0 };
                    break;
                default:
                    Console.WriteLine("Invalid Characther");
                    break;
            }
 

            for (int i = 0; i < b.Length; i++)
            {
                Console.Write(b[i]);
            }
            Console.WriteLine();
 
            return (b);
        }
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 335
1 Gihan Liyanage 332
2 OriginalGriff 236
3 ClimerChinna 222
4 vikinghunter 168
0 Sergey Alexandrovich Kryukov 8,243
1 OriginalGriff 7,109
2 CPallini 2,598
3 Richard MacCutchan 1,980
4 Abhinav S 1,778


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100