将字符串转换为字节

时间:2017-01-08 12:36:36

标签: c# parsing can-bus j1939

我有一个J1939 CAN原始字符串,格式如下:

CAN:0B00FEE99CF002000CEF02000B00FEE81A9A9F60FFFFB8570B00FEE042522500425225000B00FEE5E0530100C89F0400

此字符串包含一些分为3部分的CAN消息,例如

0B 00FEE99 CF002000CEF0200

1)PGN数据长度,以字节为单位0B

2)PGN号码(长度为3个字节)0FEE99

3)PGN数据CF002000CEF0200

目前,我正在使用子字符串来解析3个部分但我没有得到正确的值。我想我可能犯了一个错误,因为我没有将字符串转换为字节数组。 这是我的代码的一部分:

int CANStrLength = 24;
int CANIndex = CANDataIndex(rawDataElements);
int CANStrIndex = rawDataElements[CANIndex].IndexOf("CAN:");
string CANmessage = rawDataElements[CANIndex].Substring(CANStrIndex + 4).Split(',').First();
Console.WriteLine("\nIn rawDataElements[{0}]: {1}\nLength of CAN data: {2}", CANIndex, CANmessage, CANmessage.Length);

int numberOfCANMessages = CANmessage.Length / CANStrLength;
Console.WriteLine("There are {0} CAN messages", numberOfCANMessages);

List<string> CANMessages = SplitIntoParts(CANmessage, CANStrLength);
for (int i = 0; i < numberOfCANMessages; i++)
{
    int pgnDataLength = Convert.ToInt32(CANMessages[i].Substring(0, 2), 16);
    int pgnNumber = Convert.ToInt32(CANMessages[i].Substring(2, 6), 16);
    long CANData = Convert.ToInt64(CANMessages[i].Substring(8), 16);
    Console.WriteLine();
    Console.WriteLine(CANMessages[i]);

    switch (pgnNumber)
    {
        // fuel consumption */
        case 65257:
            string totalFuelUsedStr = CANMessages[i].Substring(8).Substring(8, 4);
            double totalFuelUsed = Convert.ToInt32(totalFuelUsedStr, 16) * 0.5;
            Console.WriteLine("Total Fuel Used: {0}L, {1}gal", totalFuelUsed, (int)(totalFuelUsed* 0.26));
            break;

2 个答案:

答案 0 :(得分:0)

您可能需要在此处检查以将十六进制字符串转换为字节数组:How can I convert a hex string to a byte array?

然后你应该使用MemoryStream / BinaryReader。 (使用保存为LSB的int转换十六进制字符串,不会产生与将十六进制直接转换为整数相同的值,因为那样您将像MSB一样解析它)。请参阅Endianness上的wiki。

所以要么使用BinaryReader将字节转换为int,要么使用BitConverter

您可以尝试类似:(PSEUDO)

int pgnDataLength;
int pgnNumber;
long CANData;

// this function is found on the stackoverflow link above
byte[] data = StringToByteArray(hex);

using(memStream = new MemoryStream(data))
{
    var reader = new BinaryReader(memStream);

    pgnDataLength = reader.ReadInt32();
    pgnNumber = reader.ReadInt32();
    CANData = reader.ReadInt64();
}

答案 1 :(得分:0)

你需要转换为字节是正确的。您的索引关闭,因为一个字节是两个半字节。见下面的代码

            string input = "0B00FEE99CF002000CEF02000B00FEE81A9A9F60FFFFB8570B00FEE042522500425225000B00FEE5E0530100C89F0400";
            List<byte> bytes = new List<byte>();
            for (int i = 0; i < input.Length; i += 2)
            {
                byte newByte = byte.Parse(input.Substring(i,2), System.Globalization.NumberStyles.HexNumber);
                bytes.Add(newByte);

            }