我正在读Hex的值,但...... [英] Am reading a value in Hex back but...

查看:72
本文介绍了我正在读Hex的值,但......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

一个问题,一个排序思路的方法我正在读取一个设备的十六进制值,它以奇数格式为值0-15,0到AI可以使用以下方法解析它没有问题:

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);



当字符串被反转时。我相信我的逆转方法导致了我现在看到的问题,当我得到的数字是25时它返回19000000

19 00 00 00

87 65 43 21

需要显示为
$ b $ 00 00 00 19或位模式

21 43 65 87

给25我使用字符串生成器来反转使用0-15的模式,因为只有字符串是A0000000才能得到0000000A。我现在正在考虑以下几行


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



这似乎将数字分成两位数据块并以正确的顺序显示(所以25显示为19! )我想我可以用


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);



这个问题与昨天有关从这个字符串中获取价值的简单方法我会在午餐后给出一些回复,感谢这有助于让鸭子排成一行。



Glenn


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

推荐答案

我不确定我是否理解你的问题。不过,我会尝试回答。



您可以使用以下内容将十六进制字符串转换为整数:

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)



如果strYourHexValue包含00000019,则intValue将包含25.



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



< b> 学习和学习的例子:


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());


这篇关于我正在读Hex的值,但......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆