Reading timestamp from 2 word of 32bits each - pcapng file

3 views (last 30 days)
Dear fellow Matlab user
I'm trying to read the timestamp of an enhanced packet from a pcapng file using the fread matlab function. the timestamp is stored in 2 word -32 bits each (2 times uint32).
Let assume that :
TimeStamp_HIGH = '0x16DA0D25'
TimeStamp_LOW = '0x73258DA1'
I expect to get :
TimeStamp = '0x16DA0D2573258DA1';
I first tried to read the timestamp as 1 word using :
TimeStamp = fread(fid,1,'uint64','ieee-le'); % the file is encoded in little endian
TimeStamp_HEX = dec2hex(TimeStamp)
TimeStamp_HEX =
'73258DA116DA0C00'
Unfortunatly, because of the little endian, the LOW word comes first. the other way around would be perfect.
I've found a work around that consits of reading the 2 words separetly, to convert them into string and then to concatenate them into 1 big word.
TimeStamp_H = fread(fid,1,'uint32');
TimeStamp_L = fread(fid,1,'uint32');
TimeStamp_str = strcat(dec2hex(TimeStamp_H), dec2hex(TimeStamp_L));
TimeStamp =hex2dec(TimeStamp_str);
abs_date =datetime(TimeStamp,'ConvertFrom','epochtime','TicksPerSecond',1e9);
It works but :
  • It is a quick and dirty fix,
  • it is slow,
  • I get a warning from the hex2dec because the resolution is bigger than the flintmax (I dont mind losing a bit of accuray)
Do you have any ideas how I could do that without having to convert it into strings ?
Thanks in advance.
Edouard

Answers (1)

VINAYAK LUHA
VINAYAK LUHA on 27 Sep 2023
Hi Ed,
It is my understanding that you want to combine two 32-bit words into one 64-bit word in an efficient way.
Find a possible workaround-
  1. Read the two 32-bit words separately and convert them to 64-bit words.
  2. Shift the bits in the “high word” to the left by 32 bits add the “low word”.
  3. Convert the resultant decimal number to HEX.
Find the code snippet below for your reference:
TimeStamp_HIGH = 0x16DA0D25
TimeStamp_LOW = 0x73258DA1
TimeStamp = bitshift(uint64(TimeStamp_HIGH), 32)+uint64(TimeStamp_LOW);
TimeStamp_HEX = dec2hex(TimeStamp)
Since bit manipulations occur at binary level, this approach is comparatively efficient in terms of space and memory.
Hope this helps.
Regards,
Vinayak Luha

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!