簡體   English   中英

將十六進制字符串轉換為十進制

[英]Conversion of Hex string to decimal

伙計們,我需要一些幫助將這個 c 程序轉換為 python,因為我不是任何 ac 人。 我正在嘗試轉換這個十六進制字符串。 我被告知以下十六進制字符串是一系列位置和負載整數值。 開發人員的確切詞是:

0x00703D450080474500704A4500E04B4500E04B4500D04A4500E0484500B0464500C044450090434500404345009043450000444500F0434500F0424500F0404500403E4500703B45000039450020374500F03545006035450090354500A0364500E0384500403C450090404500C0444500A04745008047450030434500D0394500702B4500A0184500A002450080D5440000A54400006C440040144400008943000050C100808AC3004003C400803EC4008077C4002096C40040ADC400A0BFC40040CCC40020D3C40040D5C40020D4C40040D1C40000CEC400C0CAC40080C7C40020C4C40000C1C40020BEC40040BCC40060BBC40060BBC400C0BBC400E0BBC40060BBC40080BAC400C0B9C400C0B9C400C0BAC400C0BCC40040BFC400E0C1C40020C4C400A0C5C40060C5C40080C2C400E0BAC40000ADC4002097C400C072C400402AC40080B4C3000014C200808443000007440080444400C07C440040994400E0B3440000CE4400A0E6440000FD440050084500E0104500901845001020450090274500302F4500A0364500703D45295C5A42B81E55423D0A5242C3F54E4233334C4248E149427B144842A47046425C8F44420AD7414214AE3D4214AE3742CDCC2F42E17A2642A4701C425C8F12428FC20942E17A0242A470F94114AEEF419A99E541CDCCD8419A99C741B81EB14100009641EC517041EC5134413333FB40EC51A040C3F5384052B8BE3F3333333F0AD7A33E7B142E3EAE47E13D8FC2753D0AD7233C000000000AD7233D7B142E3EA470BD3E9A99193F295C4F3F8FC2753F713D8A3FF6289C3F9A99B93FCDCCEC3F1F851B4048E14A40000080409A999940C3F5B0408FC2C54048E1DA40CDCCF440D7A30C415C8F2641CDCC484148E1724133339141F628AA416666C241D7A3D841F628EC413333FD416666064233330E42CDCC16428FC22042B81E2C425C8F3842333345423D0A514200005B42A4706242333367425C8F6942F6286A42CDCC69423D0A69426666684285EB67421F856742F628674214AE66427B146642A470654248E16442EC5164420AD76342AE476342D7A362420AD76142C3F5604200006042C3F55E428FC25D42AE475C42295C5A42

“十六進制字符串是一系列 2 字節整數,成對的 Position 和 Load。前 2 個字節是 Position1,下一個 2=Load1,下一個 2=Position2,下一個 2=Load2,等等……一個字節是 2 個 Hex人物。 ”

這是開發人員提供給我的 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;
}

我正在為此苦苦掙扎,這讓我發瘋,因為我相信它應該很簡單。 這是我寫的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()


看起來描述與 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));

所有負載都來自陣列的前面,所有位置都來自“midIndex”之后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM