Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
This is related to a couple of questions one today one yesterday on various issue regaurding Endianess of some data read in from a board. It will return a value in Hex much head scratching has led to the realisation that it is Hex and in the wrong order. Meaning that the reply of 19000000 is read as

bits 8 7 6 5 4 3 2 1
data 1 9 0 0 0 0 0 0

should be read as:
0 0 0 0 0 0 1 9
2 1 4 3 6 5 8 7
bit order, once it is ordered can be converted from Hex to give me the 25 I am looking for
C#
      /* 19 00 00 00
         87 65 43 21
         need to be displayed as
         00 00 00 19 or bit pattern
         21 43 65 87
         to give 25
 */

      private void button1_Click(object sender, EventArgs e)
      {
          int a = 0;
         // int Number_val = 0;
          string[] Credit_Value_Correct = new string[5];
        //  string Trimmed = null;
          for (int i = 0; i <= value_split.Length; i += 2)
          {
              if (i == value_split.Length)
              {
                  break;
              }
              Credit_Value_Correct[a] += value_split.Substring(i, 2);
              a++;
                      }

        //  MessageBox.Show(value_split);
          //for (int j = 0; j <= 3; j++)
          //{
          //  //  MessageBox.Show("Credit_Value_Correct[" + j.ToString() + "] = " + Credit_Value_Correct[j]);
          //}

          textBox1.Text = Credit_Value_Correct[3] + Credit_Value_Correct[2] + Credit_Value_Correct[1] + Credit_Value_Correct[0];
      }
Value_Int = int.Parse(textBox1.Text, System.Globalization.NumberStyles.HexNumber);
          MessageBox.Show(" Credit is " + Value_Int);

Which works for 25 but I am not sure if it will work higher. If I give it E8030000
my bit of test code will translate to 000003E8 which by my calculator gives 1000
but will this be reliable? and using an array of strings to display it just seems wrong, it works but....
Glenn
Posted
Comments
webmaster442 24-Jan-13 10:24am    
Array.Reverse() function reverses the order of an array type. Take a look into that.
PIEBALDconsult 24-Jan-13 10:43am    
Endianness affects bytes, not bits.
glennPattonWork3 24-Jan-13 10:46am    
Bits, Bytes lets call the whole thing off, go it sorted!
PIEBALDconsult 24-Jan-13 12:12pm    
Oh, good, yeah I saw your Lounge post.

I think I also answered one of these similar questions here.


...But since the question is slightly different and it was interesting to work on, despite the wierdness of your requirements, here is a function that should work nicely:

C#
static int ParseCrazyHexValue(string input)
{
   input = input.Trim();
   if (input.Length % 2 != 0)
       throw new ArgumentOutOfRangeException("Input string must be an even number of chars.");

   string output = string.Empty;
   char[] chars = input.Reverse().ToArray();
   for (int i = 0; i < chars.Length - 1; i += 2)
        output = string.Format("{0}{1}{2}", output, chars[i + 1], chars[i]);

   return Int32.Parse(output, System.Globalization.NumberStyles.HexNumber);
}
 
Share this answer
 
v4
Comments
glennPattonWork3 24-Jan-13 12:10pm    
Thanks for that I ask it several times with slighly different wording due to fact I was working at home then at work this problem was a bit odd but I got working on my own mostly due to sitting down and writing out the problem to get a solution that would work for all values and then find its 10,000 max was a bit of a let down. Thanks for the solution though it should give another check.
Glenn
BC @ CV 24-Jan-13 12:22pm    
Why is there a 10,000 max?
glennPattonWork3 24-Jan-13 12:29pm    
because thats all the meter will take! I really don't know?, but it works!
In that case, why bother messing with the string, just parse it as is and then reverse the bytes:
C#
string input = "E8030000";
uint rawvalue = uint.Parse(input, System.Globalization.NumberStyles.HexNumber);
byte[] b = BitConverter.GetBytes(rawvalue);
// Either use the Linq .Reverse().ToArray():
uint reversed = BitConverter.ToUInt32(b.Reverse().ToArray(), 0);  // reversed is 1000 here
// OR just reverse the bytes yourself:
byte sb = b[0];
b[0] = b[3];
b[3] = sb;
sb = b[1];
b[1] = b[2];
b[2] = sb;
reversed = BitConverter.ToUInt32(b, 0);  // reversed is 1000 here, also
 
Share this answer
 
v2

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