Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
 
  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 14-Jan-13 7:35am
Edited 14-Jan-13 22:36pm
v2
Comments
Philippe Mori at 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...)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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();
        }
  Permalink  
v3
Comments
Andreas Gieriet at 14-Jan-13 20:06pm
   
Typo: it should be First = 1?
Cheers
Andi
BC @ CV at 15-Jan-13 9:42am
   
Thanks, Correction made.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

What you do is something like
int i = int.TryParse(s, System.Globalization.NumberStyles.AllowHexSpecifier, null, out i)
      ? i
      : 0;
This has nothing to do with bit operators.
Andi
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi.
 
I agree with Sergey Alexandrovich Kryukov about no bitwise operation can be found in your example.
 
However, you can rewrite your loop as:
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 443
1 Sergey Alexandrovich Kryukov 197
2 PhilLenoir 145
3 RyanDev 125
4 Richard Deeming 120
0 Sergey Alexandrovich Kryukov 6,415
1 OriginalGriff 5,856
2 CPallini 2,473
3 Richard MacCutchan 1,667
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 15 Jan 2013
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