Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
3.25/5 (4 votes)
See more:
Hi all,

It appears that the binary mucking I did earlier was right (but wrong) it but I
need to go over the binary number as bit field the below code is a rough lash up of how I want the 4 bit values from the Binary number

C#
char[] Input;

          for (int i = 0; i < 4; i++)
          {
              if (Input[i] > 'Z')
                  Input[i] -= 0x20;  // Convert lower case to upper case

              if ((Input[i] >= '0') && (Input[i] <= '9')) // First check to see if the number is 0 to 9
              {
                  Input[i] -= '0';  //If so, then subtract ascii ‘0’ from it

              }

              else if ((Input[i] >= 'A') && (Input[i] <= 'F'))// is this the hex codes A to F?
              {
                  Input[i] -= 0x37;  // Subtract 55 (0x37) so ascii A becomes 10
              }
             else
                  Input[i] = 0;  // Else invalid so substitute 0
          }

If you & with 0x08 and the result is not 0 then bit 3(4) is set

If you & with 0x04 and the result is not 0 then bit 2(3) is set

If you & with 0x02 and the result is not 0 then bit 1(2) is set

If you & with 0x01 and the result is not 0 then bit 0(1) is set

I want to do the old C route of doing it bit wise (a&&2) etc. Man I have to go home!

Glenn
(If the question peaks interest I will rephrase and make it cleaner tomorrow)


Umm it has, so rephrase time I have a character between 0 to 15 the code above (sort of) converted it to try to get 0 to 15 value out. I believe there should be a ConvertTo to do this but....
Glenn
Posted
Updated 14-Jan-13 22:36pm
v2
Comments
Philippe Mori 14-Jan-13 19:00pm    
Such low level manipulations generally does not make much sense in C#. You should use existing function to convert case or numbers or to check the category of a character (number, letter, space...)

In this code, there is nothing with resembles bit operations per se. Refer to the operator list:
http://msdn.microsoft.com/en-us/library/6a71f45d%28v=vs.110%29.aspx[^].

You will need binary shift '>>', '<<', binary AND, OR, XOR and NOT ('&', '|', '^', '~'). See also:
http://blog.typps.com/2007/10/bitwise-operators-in-c-or-xor-and-not.html[^].

—SA
 
Share this answer
 
On the off hand chance you are refering to checking bit flags you could do this in C#:

public enum BitFlag : byte
        {
            First=1,
            Second=2,
            Third=4,
            Fourth=8
        }

        public static string CheckFlag()
        {
            BitFlag bFlag = BitFlag.First | BitFlag.Third;
            StringBuilder result = new StringBuilder();

            if ((bFlag & BitFlag.First) == BitFlag.First)
                result.AppendLine("First");
            if ((bFlag & BitFlag.Second) == BitFlag.Second)
                result.AppendLine("Second");
            if ((bFlag & BitFlag.Third) == BitFlag.Third)
                result.AppendLine("Third");
            if ((bFlag & BitFlag.Fourth) == BitFlag.Fourth)
                result.AppendLine("Fourth");
            return result.ToString();
        }
 
Share this answer
 
v3
Comments
Andreas Gieriet 14-Jan-13 20:06pm    
Typo: it should be First = 1?
Cheers
Andi
BC @ CV 15-Jan-13 9:42am    
Thanks, Correction made.
What you do is something like
C#
int i = int.TryParse(s, System.Globalization.NumberStyles.AllowHexSpecifier, null, out i)
      ? i
      : 0;

This has nothing to do with bit operators.
Andi
 
Share this answer
 
Hi.

I agree with Sergey Alexandrovich Kryukov about no bitwise operation can be found in your example.

However, you can rewrite your loop as:
C#
for (int i = 0; i < 4; i++)
{
    char ch = Input[i];
    ch = char.ToUpperInvariant(ch); // Convert any Lower case to Upper case
    int nValue = "0123456789ABCDEF".IndexOf(ch);
    if (nValue < 0) // -1 means not found: substitute with 0
        nValue = 0;
    Input[i] = nValue;
}

This applies to raw chars input like your example.

If instead you have a String as input, then you can use solution by Andreas Gieriet.

Regards,
Daniele.
 
Share this answer
 

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