im looking to check a binary value at a certin bit and determin if that bit is a 1 or a 0.

For example:

A = 10000000
B = 01111111

I want to check the first bit and if it is a 1 load it into one array and if the value is 0 load it into another array. the rest of the binary bits dont matter. Any ideas?

Recommended Answers

All 14 Replies

List<string> list1 = new List<string>();
            List<string> list2 = new List<string>();
            string a = "100000";
            string b = "011111";
            string[] array = new string[]{a,b};
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i][0].ToString() == "1")
                    list1.Add(array[i]);
                else
                    list2.Add(array[i]);
            }
List<int> list1 = new List<int>();
List<int> list2 = new List<int>();
a & 0x8000 == 0 ? list1.Add(a) : list2.Add(a);
b & 0x8000 == 0 ? list1.Add(b) : list2.Add(b);

Checks if the high order bit is set.

List<string> list1 = new List<string>();
            List<string> list2 = new List<string>();
            string a = "100000";
            string b = "011111";
            string[] array = new string[]{a,b};
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i][0].ToString() == "1")
                    list1.Add(array[i]);
                else
                    list2.Add(array[i]);
            }

Thank you guys!!!! mmmm say i have an int 99 is the a function like To.String, To.Int32 that will convert it to binary???

Convert.ToString(number, base) will create the string representation of the number in base 2, 8, 10 or 16.

Convert.ToString(number, base) will create the string representation of the number in base 2, 8, 10 or 16.

im looking to load in a file that is like this:

99, 121, 100, 124
125, 65, 35, 56

I want to convert each value which i have magaed to do and load them into
an array in the same postions:

so 99 would be converted and loaded int array position [0,0] and 125 loaded into position [1,0] and so on. Any Ideas??

Easy enough to do, but I'd like to see you make an attempt first since this sounds like homework :)

Easy enough to do, but I'd like to see you make an attempt first since this sounds like homework :)

lol yeh dont worry ive been trying quiet a bit. i just cant get the hang of arrays. rite i couldnt get it into a two dimentional so i thought i could load it into an arraylist and then convert it to an array. which isnt really that efficient.

I think i have managed to create the array and convert all the values in the file to binary but i dont know how to get those values into an array??

string Sbox = File.ReadAllText(@"C:\Users\Mark\Desktop\University\Final Year Project\SboxOutput.csv");
            string[] Sbox_Lines = Sbox.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            int row = 1000;
            int col = 256;

            int[,] SboxBinary = new int[row,col];

            for (int i = 0; i < Sbox_Lines.Length; i++)
            {
                
                string[] lineValues = Sbox_Lines[i].Split(',');

                for ( int j = 0; j < lineValues.Length; j++)
                    {
                   
                    int intvalue = Int32.Parse(lineValues[col]);
                    string binaryvalue = "";
                    binaryvalue = Convert.ToString(intvalue, 2);
                    string padvalue = binaryvalue.PadLeft(8, '0');            
                      
                    }
            }

Any help would be greatful thanks!!

Is it a hex dump you want?

You are almost there, you just need one more line at line 21. You need to set an array value to your result. Take a look at your code and think "Which variable here represents the row I'm on in the file" and "which variable here represents the column I'm in for that row". Once you can answer that, the rest is easy :)

You are almost there, you just need one more line at line 21. You need to set an array value to your result. Take a look at your code and think "Which variable here represents the row I'm on in the file" and "which variable here represents the column I'm in for that row". Once you can answer that, the rest is easy :)

Hay thanks very much i got it working . .Sort of. This is what i got . .

public static void Main(string[] args)
        {
            string Sbox = File.ReadAllText(@"C:\Users\Mark\Desktop\University\Final Year Project\TEST FILE2.csv");
            string[] Sbox_Lines = Sbox.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            int row = 1000;
            int col = 256;

            string[,] SboxBinary = new string[row,col];

            for (int i = 0; i < Sbox_Lines.Length; i++)
            {
                
                string[] lineValues = Sbox_Lines[i].Split(',');

                for ( int j = 0; j < lineValues.Length; j++)
                    {
                   
                    int intvalue = Int32.Parse(lineValues[j]);
                    string binaryvalue = "";
                    binaryvalue = Convert.ToString(intvalue, 2);
                    string padvalue = binaryvalue.PadLeft(8, '0');
                    SboxBinary[i, j] = padvalue;
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Mark\Desktop\University\Final Year Project\SboxBinary.csv", true))
                    {
                        file.WriteLine(SboxBinary[i, j]);
                    }
                    }
                   
            }
            Console.ReadKey();
        }

Th only problem i have is when i write to the file it prints the results in the file in one big long column instead of like the original array.Have you got any hints on how to do this? i want to take the value u out and convert it but i want it to go into an array in the same position it came from.

eg 99 is parsed from the file at position [0,0]

i want to convert it and put it into an array at position [0,0]
and then print the array to a file but in the position [0,0] and so on. Any ideas?

Hay thanks very much i got it working . .Sort of. This is what i got . .

public static void Main(string[] args)
        {
            string Sbox = File.ReadAllText(@"C:\Users\Mark\Desktop\University\Final Year Project\TEST FILE2.csv");
            string[] Sbox_Lines = Sbox.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            int row = 1000;
            int col = 256;

            string[,] SboxBinary = new string[row,col];

            for (int i = 0; i < Sbox_Lines.Length; i++)
            {
                
                string[] lineValues = Sbox_Lines[i].Split(',');

                for ( int j = 0; j < lineValues.Length; j++)
                    {
                   
                    int intvalue = Int32.Parse(lineValues[j]);
                    string binaryvalue = "";
                    binaryvalue = Convert.ToString(intvalue, 2);
                    string padvalue = binaryvalue.PadLeft(8, '0');
                    SboxBinary[i, j] = padvalue;
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Mark\Desktop\University\Final Year Project\SboxBinary.csv", true))
                    {
                        file.WriteLine(SboxBinary[i, j]);
                    }
                    }
                   
            }
            Console.ReadKey();
        }

Th only problem i have is when i write to the file it prints the results in the file in one big long column instead of like the original array.Have you got any hints on how to do this? i want to take the value u out and convert it but i want it to go into an array in the same position it came from.

eg 99 is parsed from the file at position [0,0]

i want to convert it and put it into an array at position [0,0]
and then print the array to a file but in the position [0,0] and so on. Any ideas?

Right foget the last post lol i think i have that. im looking to check the most significant bit of each of the values and determin if its a 1 or a 0. is there a funchion which checks for the most significant bit?

check this link

Yeh i was on that page. But thank you. Problem i dont really understand whats happening there.

Yeh i was on that page. But thank you. Problem i dont really understand whats happening there.

Ok so this is what ive got so far. unfortunatly it is not seperating the binary values correctly . . . Ive tried but i dont know what the problem is..

string Sbox = File.ReadAllText(@"C:\Users\Mark\Desktop\University\Final Year Project\TEST FILE2.csv");
            string[] Sbox_Lines = Sbox.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);


            int row = 1000;
            int col = 256;

            string[,] SboxBinary = new string[row, col];
            List<string> list1 = new List<string>();
            List<string> list2 = new List<string>();

            for (int i = 0; i < Sbox_Lines.Length; i++)
            {

                string[] lineValues = Sbox_Lines[i].Split(',');

                for (int j = 0; j < lineValues.Length; j++)
                {

                    int intvalue = Int32.Parse(lineValues[j]);
                    string binaryvalue = "";
                    binaryvalue = Convert.ToString(intvalue, 2);
                    string padvalue = binaryvalue.PadLeft(8, '0');
                    SboxBinary[i, j] = padvalue;


                    String position128 = padvalue.Substring(0,1);
                        if (position128 == "1")
                            list1.Add(SboxBinary[i, j]);
                        else
                            list2.Add(SboxBinary[i, j]);
                        foreach(string value in list1)
                        {
                            Console.WriteLine(value);
                        }

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Mark\Desktop\University\Final Year Project\SboxBinary.csv", true))
                    {
                        file.WriteLine(SboxBinary[i, j]);
                    }
                }


                Console.ReadKey();
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.