Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a accelerometer generated hex file with a time stamp, it has acceleration data from three axes. The data looks like this:

C1 EB C1 F9 C1 C6


41 F0 81 FA 41 C6


01 F7 C1 FA 81 C6


I need to separate it out like this:

C1EB C1F9 C1C6 

41F0 81FA 41C6 

01F7 C1FA 81C6  

Finally I need to convert it to decimal in Matlab to see the acceleration. Can someone help me with this?

share|improve this question

migrated from Apr 4 '11 at 19:25

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

what language?? – kenny Apr 4 '11 at 18:37
You want to remove every other line and remove the first, third, and fifth space in the remaining lines? That's a job for sed, awk, cut, or your favorite scripting language, not a question for an electrical designer. It might be on-topic if you wanted to modify the firmware of your device to generate that information, but that will probably involve just modifying the formatting of a snprintf() statement. – Kevin Vermeer Apr 4 '11 at 18:39
@kenny: its just a plain file with values in this format, sorry I have no idea what language it is. – Dilip Apr 4 '11 at 18:41
@reemrevnivek: sed,awk,cut?? can you please tell me how to do that and read acceleration from this hex file? – Dilip Apr 4 '11 at 18:42
@Dilip, you can do it in any language you want, probably even Matlab. If you don't know what language you want to use shell rools as suggested by @reemrevnivek. – kenny Apr 4 '11 at 18:44

If each line of a file named 'measurement.txt' looks similiar to

C1 EB C1 F9 C1 C6 12:57:39.046:

a regular expression can be used to extract the hexadecimal numbers (each one existing out of 4 digits), remove the spaces and the transform the number to decimal using MATLAB, with the following code:

data = importdata('measurement.txt',' ');
data_hex = [];
for m = 1 : size(data,1)
    [start_idx, end_idx, extents, matches, tokens, names, splits] = regexp(data{m,1},'([A-F0-9]{2} [A-F0-9]{2}) ([A-F0-9]{2} [A-F0-9]{2}) ([A-F0-9]{2} [A-F0-9]{2}).*');

    for n = 1 : size(tokens{1,1},2)
        data_hex{m,n} = strrep(tokens{1,1}(1,n),' ','');
        data_dec(m,n) = hex2dec(data_hex{m,n});
share|improve this answer
@Peter: I can send you the file if it's possible. The problem is the file is with close to 4000 lines so I cant do it manually. – Dilip Apr 4 '11 at 18:58
I am uncertain whether there is a blank line after each line. If so, my code has to be adapted a little bit. Now it searches for all lines not containing any ':'-character. – Peter Pablo Apr 4 '11 at 19:10
@ peter: thanks it works great but my problem is I need to convert something like this in the file C1 EB C1 F9 C1 C6 12:57:39.046: to C1EB C1F9 C1C6 to 49643 49657 49606. – Dilip Apr 4 '11 at 19:15
@Dilip: Please elaborate on the exact format of the file. So the space separates two hexadecimal numbers? Please edit your first post and indent the content of your file. It will then be displayed as code. – Peter Pablo Apr 4 '11 at 19:20
@Peter: this is how the first few lines look 12:57:18.281: C1 F3 81 F6 81 C5 12:57:18.296: 01 F0 41 F7 01 C6 12:57:18.296: 41 ED C1 F7 01 C6 12:57:18.312: C1 E8 01 F8 01 C6 – Dilip Apr 4 '11 at 19:23

Assuming this logical format for data collection:

12:57:18.281: C1 F3 81 F6 81 C5
12:57:18.296: 01 F0 41 F7 01 C6
12:57:18.296: 41 ED C1 F7 01 C6
12:57:18.312: C1 E8 01 F8 01 C6

This perl program:

  if(s/^(\d+):(\d+):(\d+)\.(\d+):\s*//) {
  if(s/(..)\s+(..)\s*/(hex($1)<<8|hex($2))."\t"/eg) {
  print "$time\t$data\n";

With this command line:

perl < > data.out

Will convert it to a universal tab separated format that anything will work with:

46638.281       49651   33270   33221
46638.296       496     16887   454
46638.296       16877   49655   454
46638.312       49640   504     454

To make any meaningful sense of the data you will need the time axis too. To start from t=0, just subtract the first time entry from all subsequent data points.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .