本文介绍了c#bitconverter.ToString转换为十六进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 BitConverter.ToString(bytes)将字符串转换为十六进制字符串,我将其进一步转换为整数或浮点数。

但是输入流由0组成,表明字节值为0.所以假设我有一个整数,它由2个字节的输入表示,从位置x开始,第一个由 EE 组成,而第二个字节为 00 。现在当我使用 BitConverter.ToString(bytes,x,2).Replace( - ,)时,输出为 EE00 ,其整数值为 60928 ,但在这种情况下,输出应该是 238 字节 EE 为整数。

但在其他情况下,2个字节可能是 EE01 ,其整数值 60929 在这种情况下是正确的。



任何建议如何解决我的问题?

由于有人说这个问题令人困惑,我会重申我的问题我有很长的十六进制字符串作为输入。在十六进制字符串中,

1)前12个字节表示字符串。
2)接下来的11个字节也代表了其他一些字符串。
3)接下来1个字节表示整数。
4)接下来的3个字节表示整数。
5)接下来的4个字节表示整数。
6)接下来的4个字节表示浮点数。
7)接下来的7个字节表示字符串。
8)接下来的5个字节表示整数。

因此,对于第4种情况,如果字节是ee 00 00,那么我应该忽略0并将ee转换为整数。但是如果它是ee 00 ee,那么我应该将ee00ee转换为整数。我每次都会遵循上面提到的相同模式。 解决方案

我的问题已解决。我正在犯Endianness的错误。我收到的数据是EE 00,在转换为整数之前,我应该将它作为00 EE。感谢所有为我提供解决方案的人,并抱歉错过了这个重要的问题。


I am using BitConverter.ToString(bytes) for converting by string to hexadecimal string which I further convert it into integer or float.

But the input stream consist of 0 to show that byte value is 0. So suppose I have an integer which is represented by 2 bytes of input starting at position x and the first consist of EE while 2nd byte is 00. Now when I use BitConverter.ToString(bytes, x, 2).Replace ("-","") I get output as EE00 whose integer value is 60928 but in this case the output should be 238 that is converting only first byte EE to integer.

But in some other case the 2 bytes might be EE01 whose integer value will 60929 which is correct in this case.

Any suggestion how can I solve my problem?

Since some people are saying that question is confusing I will restate my problem I have long hexadecimal string as input. In hexadecimal string the

1) First 12 bytes represent string.2) next 11 bytes also represent some other string.3) Next 1 byte represent integer.4) Next 3 bytes represent integer.5) Next 4 bytes represent integer.6) Next 4 bytes represent float.7) Next 7 bytes represent string.8) Next 5 bytes represent integer.

So for 4th case if bytes are ee 00 00 then I should neglect 0's and convert ee to integer. But if it ee 00 ee then I should convert ee00ee to integer. Also every time I will be following same pattern as mentioned above.

解决方案

My problem has been solved. I was making a mistake of Endianness. I was receiving the data as EE 00 and I should have taken it as 00 EE before converting to integer. Thanks to all who gave me solution for my problem and sorry for missing out this important fact from question.

这篇关于c#bitconverter.ToString转换为十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 22:09