Search code examples
pythonpython-3.xtype-conversiondata-conversion

Remove commas from a list and convert Hexadecimal into Decimal value


I am trying to extract variable length Hexadecimal value from BLF log file starting indexed at position [7]. I can successfully extract the variable length 'hexadecimal' values in a list. The problem is removing the commas between the hexadecimal values from each extracted list.

Below is the BLF file from which I am extracting the variable length hexadecimal values:

['Timestamp:', '1546626931.138813', 'ID:', '0764', 'S', 'DLC:', '8', '00', '00', '00', '00', '00', '00', '00', '00', 'Channel:', '0']
['Timestamp:', '1546626931.138954', 'ID:', '0365', 'S', 'DLC:', '8', '00', '00', '00', '80', 'db', '80', 'a2', '7f', 'Channel:', '1']
['Timestamp:', '1546626931.139053', 'ID:', '0765', 'S', 'DLC:', '6', 'ae', '05', '00', '00', '05', '00', 'Channel:', '1']
['Timestamp:', '1546626931.139697', 'ID:', '022a', 'S', 'DLC:', '4', '40', 'c0', '50', '6c', 'Channel:', '1']
.
.
.
.

The output I am getting in a file is as follows:

['00', '00', '00', '00', '00', '00', '00', '00']
['00', '00', '00', '80', 'db', '80', 'a2', '7f']
['ae', '05', '00', '00', '05', '00']
['40', 'c0', '50', '6c']

But I what I want is as below is to first remove the commas from the original list amd then convert the hexadecimal values shown below into decimal:

['0000000000000000']
['00000080db80a27f']
['ae0500000500']
['40c0506c']

My code is below:

import can
import csv
import datetime
# import timestamp as timestamp

filename = open('C:\\Users\\xyz\\Downloads\\BLF File\\hex_Decimal.csv', "w")
log1 = can.BLFReader('C:\\Users\\xyz\\Downloads\\BLF File\\test.blf')


#Extracting Hexadecimal and convert into decimal
for time in log1:
    time = str(time).split()
    data=str(time[7:(7 + int(time[6]))])
    "".join(data)
    print(data)

I am unable to remove the commas from the list and convert the list of hexadecimal number into decimal values inside the list. Any help is appreciated. Thank you!


Solution

  • If the type of the time messages extracted from the BLF file is just a list of strings, you are done:

    ...
    #Extracting Hexadecimal and convert into decimal
    for time in log1:
        data = int(''.join(time[7:7+int(time[6])]), 16)
        print([data])
    

    You should get:

    [0]
    [553438454399]
    [191336498070784]
    [1086345324]