Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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:
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
    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
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 24-Jan-13 1:59am
Comments
Edo Tzumer at 24-Jan-13 8:28am
   
What's the question?
glennPattonWork at 24-Jan-13 8:35am
   
Sorry, thinking while typing makes it less painful at times!
sjelen at 24-Jan-13 12:16pm
   
What is painful, thinking or typing? ;)
glennPattonWork at 24-Jan-13 12:21pm
   
A little of both at the moment I managed to burn on finger tip yesterday!

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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());
 
  Permalink  
v5
Comments
glennPattonWork at 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 at 24-Jan-13 9:08am
   
See the example I added to Solution 1.
glennPattonWork at 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!
glennPattonWork at 24-Jan-13 10:44am
   
Thanks to all I think I have solved it?
Mike Meinz at 24-Jan-13 10:52am
   
I changed the example to C# for you. I used <a href="http://www.developerfusion.com/tools/convert/vb-to-csharp/" target="new">Free Converter Tool</a> on the Developer Fusion web site.
glennPattonWork at 24-Jan-13 11:32am
   
Oh thanks for that link and sorry I wasted your time! Glenn
Mike Meinz at 24-Jan-13 11:43am
   
Not a waste. An opportunity for a teaching moment!

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

  Print Answers RSS
0 Dnyaneshwar@Pune 1,300
1 Sergey Alexandrovich Kryukov 660
2 OriginalGriff 329
3 ravikhoda 300
4 Manas Bhardwaj 240
0 Sergey Alexandrovich Kryukov 11,194
1 OriginalGriff 6,867
2 Peter Leow 4,795
3 Abhinav S 3,838
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web03 | 2.8.140421.2 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid