简体   繁体   English

将十六进制字符串转换为十进制

[英]Conversion of Hex string to decimal

Guys i need some help converting this c program to python since i am not ac guy whatsoever.伙计们,我需要一些帮助将这个 c 程序转换为 python,因为我不是任何 ac 人。 I'm attempting to convert this hex string.我正在尝试转换这个十六进制字符串。 I was told the following hex string was a series of position and load integer values.我被告知以下十六进制字符串是一系列位置和负载整数值。 The developers exact words were:开发人员的确切词是:

0x00703D450080474500704A4500E04B4500E04B4500D04A4500E0484500B0464500C044450090434500404345009043450000444500F0434500F0424500F0404500403E4500703B45000039450020374500F03545006035450090354500A0364500E0384500403C450090404500C0444500A04745008047450030434500D0394500702B4500A0184500A002450080D5440000A54400006C440040144400008943000050C100808AC3004003C400803EC4008077C4002096C40040ADC400A0BFC40040CCC40020D3C40040D5C40020D4C40040D1C40000CEC400C0CAC40080C7C40020C4C40000C1C40020BEC40040BCC40060BBC40060BBC400C0BBC400E0BBC40060BBC40080BAC400C0B9C400C0B9C400C0BAC400C0BCC40040BFC400E0C1C40020C4C400A0C5C40060C5C40080C2C400E0BAC40000ADC4002097C400C072C400402AC40080B4C3000014C200808443000007440080444400C07C440040994400E0B3440000CE4400A0E6440000FD440050084500E0104500901845001020450090274500302F4500A0364500703D45295C5A42B81E55423D0A5242C3F54E4233334C4248E149427B144842A47046425C8F44420AD7414214AE3D4214AE3742CDCC2F42E17A2642A4701C425C8F12428FC20942E17A0242A470F94114AEEF419A99E541CDCCD8419A99C741B81EB14100009641EC517041EC5134413333FB40EC51A040C3F5384052B8BE3F3333333F0AD7A33E7B142E3EAE47E13D8FC2753D0AD7233C000000000AD7233D7B142E3EA470BD3E9A99193F295C4F3F8FC2753F713D8A3FF6289C3F9A99B93FCDCCEC3F1F851B4048E14A40000080409A999940C3F5B0408FC2C54048E1DA40CDCCF440D7A30C415C8F2641CDCC484148E1724133339141F628AA416666C241D7A3D841F628EC413333FD416666064233330E42CDCC16428FC22042B81E2C425C8F3842333345423D0A514200005B42A4706242333367425C8F6942F6286A42CDCC69423D0A69426666684285EB67421F856742F628674214AE66427B146642A470654248E16442EC5164420AD76342AE476342D7A362420AD76142C3F5604200006042C3F55E428FC25D42AE475C42295C5A42

"The hex string is a series of 2 byte integers in pairs of Position and Load. The first 2 bytes is Position1, the next 2=Load1, next 2=Position2, next 2=Load2, etc... A byte is 2 Hex characters. " “十六进制字符串是一系列 2 字节整数,成对的 Position 和 Load。前 2 个字节是 Position1,下一个 2=Load1,下一个 2=Position2,下一个 2=Load2,等等……一个字节是 2 个 Hex人物。 ”

Here is the c# that was provided to me by the developer with little context behind it这是开发人员提供给我的 c#,背后几乎没有上下文

public class PositionLoadPoint
{
   public float Position { get; private set; }
   public float Load { get; private set; }
    public PositionLoadPoint(float position, float load)
   {
       Position = position;
       Load = load;
   }
}
This method should return a list of points from an array of bytes:
public static IList<PositionLoadPoint> GetPositionLoadPoints(byte[] bytes)
{
   IList<PositionLoadPoint> result = new List<PositionLoadPoint>();
   int midIndex = bytes.Length / 2;
    for (int i = 0; i < midIndex; i += 4)
   {
       byte[] load = new byte[4];
       byte[] position = new byte[4];
        Array.Copy(bytes, i, load, 0, 4);
       Array.Copy(bytes, midIndex + i, position, 0, 4);
        var point = new PositionLoadPoint(BitConverter.ToSingle(load, 0),
                                          BitConverter.ToSingle(position, 0));
        result.Add(point);
   }
    return result;
}

I'm struggling with this and its driving me crazy because i believe it should be crazy simple.我正在为此苦苦挣扎,这让我发疯,因为我相信它应该很简单。 Here is my python that i wrote, but i do not believe the results are correct since the plot is sporatic!这是我写的python,但我不相信结果是正确的,因为情节是零星的!

#INSERT LIBRARIES
import matplotlib.pyplot as plt

hex_string = '00703D450080474500704A4500E04B4500E04B4500D04A4500E0484500B0464500C044450090434500404345009043450000444500F0434500F0424500F0404500403E4500703B45000039450020374500F03545006035450090354500A0364500E0384500403C450090404500C0444500A04745008047450030434500D0394500702B4500A0184500A002450080D5440000A54400006C440040144400008943000050C100808AC3004003C400803EC4008077C4002096C40040ADC400A0BFC40040CCC40020D3C40040D5C40020D4C40040D1C40000CEC400C0CAC40080C7C40020C4C40000C1C40020BEC40040BCC40060BBC40060BBC400C0BBC400E0BBC40060BBC40080BAC400C0B9C400C0B9C400C0BAC400C0BCC40040BFC400E0C1C40020C4C400A0C5C40060C5C40080C2C400E0BAC40000ADC4002097C400C072C400402AC40080B4C3000014C200808443000007440080444400C07C440040994400E0B3440000CE4400A0E6440000FD440050084500E0104500901845001020450090274500302F4500A0364500703D45295C5A42B81E55423D0A5242C3F54E4233334C4248E149427B144842A47046425C8F44420AD7414214AE3D4214AE3742CDCC2F42E17A2642A4701C425C8F12428FC20942E17A0242A470F94114AEEF419A99E541CDCCD8419A99C741B81EB14100009641EC517041EC5134413333FB40EC51A040C3F5384052B8BE3F3333333F0AD7A33E7B142E3EAE47E13D8FC2753D0AD7233C000000000AD7233D7B142E3EA470BD3E9A99193F295C4F3F8FC2753F713D8A3FF6289C3F9A99B93FCDCCEC3F1F851B4048E14A40000080409A999940C3F5B0408FC2C54048E1DA40CDCCF440D7A30C415C8F2641CDCC484148E1724133339141F628AA416666C241D7A3D841F628EC413333FD416666064233330E42CDCC16428FC22042B81E2C425C8F3842333345423D0A514200005B42A4706242333367425C8F6942F6286A42CDCC69423D0A69426666684285EB67421F856742F628674214AE66427B146642A470654248E16442EC5164420AD76342AE476342D7A362420AD76142C3F5604200006042C3F55E428FC25D42AE475C42295C5A42'

#convert all two digit hex to decimal and place in list
hex_list = []
for i in range(0,len(hex_string),2):
    hex_list.append(int(hex_string[i:i+2],16))

#GROUP TWO CONSECUTIVE DECIMALS IN HEX_LIST TOGETHER UNTIL ALL DECIMALS ARE GROUPED INTO PAIRS WITHIN A LIST
DEC_list_pair = []
for i in range(0,len(hex_list),2):
    DEC_list_pair.append(hex_list[i:i+2])

#Create a x and y axis using the DEC_list_pair_no_duplicates list
x_axis = []
y_axis = []
for i in range(0,len(DEC_list_pair)):
    x_axis.append(DEC_list_pair[i][0])
    y_axis.append(DEC_list_pair[i][1])

#plot x_axis and y_axis
plt.plot(x_axis, y_axis)
plt.show()


Looks like the description doesn't match the C# code.看起来描述与 C# 代码不匹配。

int midIndex = bytes.Length / 2;
for (int i = 0; i < midIndex; i += 4)
{
    // Reading into 'load' from offset i from front of the array
    Array.Copy(bytes, i, load, 0, 4);
    // Reading into 'position' from offset i from midIndex of the array
    Array.Copy(bytes, midIndex + i, position, 0, 4);
    var point = new PositionLoadPoint(BitConverter.ToSingle(load, 0),
                                      BitConverter.ToSingle(position, 0));

All the loads are coming from the front of the array, all the postions are coming after "midIndex".所有负载都来自阵列的前面,所有位置都来自“midIndex”之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM