Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Hi All,
Bit of a question, bit of a method of ordering thoughts I am reading back a hex value from a device it comes in an odd format for values 0-15, 0 to A I can parse it fine with no problems using:
C#
intCredit_Value_Reverse = int.Parse(txtValueCredit.Text, System.Globalization.NumberStyles.HexNumber);

that is when the string is reversed. I believe my method for reversing is causing the problems I see now when the number I should get is 25 it returns 19000000
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 I have used a string builder to reverse the pattern which worked with 0-15 as only the string was A0000000 to give 0000000A. I am thinking along the following lines at the moment
C#
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;
            }
         //   MessageBox.Show(value_split.Substring(i, 2));
            Credit_Value_Correct[a] += value_split.Substring(i, 2);
            a++;
            // Number_val = Convert.ToInt16(value_split.Substring(i, 2));

         //   if (!(Number_val % 0 == 0))
         //   {

          //  }
        }

      //  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];
    }

This would appear to split the number into two bit data chunks and displays them in the right order (so 25 appears as 19!) I think I can then do the conversion need with
C#
int myInt = int.Parse(textBox1.Text, System.Globalization.NumberStyles.HexNumber);

This question is related to yesterdays "Is There An Easy Way of Getting Values From This String" I will give some of those response a go after lunch, thanks this has helped get ducks in a line.

Glenn
Posted
Comments
Joezer BH 24-Jan-13 8:28am    
What's the question?
glennPattonWork3 24-Jan-13 8:35am    
Sorry, thinking while typing makes it less painful at times!
sjelen 24-Jan-13 12:16pm    
What is painful, thinking or typing? ;)
glennPattonWork3 24-Jan-13 12:21pm    
A little of both at the moment I managed to burn on finger tip yesterday!

1 solution

I am not sure if I understand your question. Nevertheless, I will attempt an answer.

You can convert a Hexadecimal string to an Integer using the following:
Dim intValue As Integer = Convert.ToInt32(strYourHexValue, 16)

If strYourHexValue contains "00000019", intValue will contain 25.

See http://msdn.microsoft.com/EN-US/library/f1cbtwff(v=VS.110,d=hv.2).aspx

Example to study and learn from:
string strInput = "19000000";
// "78563412"
string strOutput = "";
int intOutput = 0;
int x = 0;
//
// Ensure an even number of digits
if (strInput.Length % 2 != 0) {
	Interaction.MsgBox("Input string must be an even number of hexadecimal characters");
	System.Environment.Exit(0);
}
//
// Reverse hex characters
for (x = strInput.Length - 2; x >= 0; x += -2) {
	strOutput += strInput.Substring(x, 2);
}
//
// Convert to integer
try {
	// Use Try block in case strInput did not contain Hexadecimal characters
	intOutput = Convert.ToInt32(strOutput, 16);
} catch (Exception ex) {
	Interaction.MsgBox(ex.Message);
	System.Environment.Exit(0);
}
Interaction.MsgBox(strOutput + "=" + intOutput.ToString());
 
Share this answer
 
v5
Comments
glennPattonWork3 24-Jan-13 8:34am    
Thanks, sorry for the question being a bit hard to understand it is just that I was thinking while typing, the string appears as 1900000 I need to reverse it so it appears as 000019 rather than a straight reverse which I did yesterday.
Mike Meinz 24-Jan-13 9:08am    
See the example I added to Solution 1.
glennPattonWork3 24-Jan-13 10:44am    
Sorry for that, but looking at it you gave VB to answer a C# question, here starts a holy war! Thanks got the problem sorted!
glennPattonWork3 24-Jan-13 10:44am    
Thanks to all I think I have solved it?
Mike Meinz 24-Jan-13 10:52am    
I changed the example to C# for you.

I used Free Converter Tool on the Developer Fusion web site.

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