1

I am trying to read a txt file which has hexadecimal data. I Want to convert them in decimal except one column which I want to convert into binary bits and write them in 8 separate columns. Sample data set

1/4/2010 15:31 <00000> 0x0001 0x0010 0x0014 0x0000 0x0142 0x0001 0x0001 0x0000 0x028F 0x2007 0x0105 0x00AA 0x005A 0xFA8C 0xFACD 0xFAED 0x003B 0xFFA3 0xFFDE 0x0080 0xFEE0 0xFF2E 0x0000 0x0108 1/4/2010 15:31 <00000> 0x0001 0x0010 0x0014 0x0000 0x0143 0x0001 0x0001 0x0000 0x028F 0x2008 0x0105 0x00AA 0x005B 0xFA8C 0xFACC 0xFAEE 0x003C 0xFFA3 0xFFDE 0x0080 0xFEE0 0xFF2E 0x0000 0x0108 1/4/2010 15:31 <00000> 0x0001 0x0010 0x0014 0x0000 0x0144 0x0001 0x0001 0x0000 0x028F 0x2009 0x0105 0x00A9 0x005C 0xFA8C 0xFACC 0xFAF0 0x003B 0xFFA3

clear all; %

[b,pathb]=uigetfile({'*.txt'},'Select the file','C:\Data\2010');

file2=[pathb b];

data=dlmread('file2', '\t', 2, 1);

newdata=hex2dec(data);

Now I do not know how to get rid of 0x in all the values and I need to convert the last column into binary and write in 8 columns.

Any help is highly appreciated. thanks

1 Answer 1

1

Here's a slightly different tack you could try, using TEXTSCAN to read all the data first as strings:

fid = fopen(file2,'rt');                   %# Open the file
str = ['%s %s %s ' repmat('0x%s ',1,24)];  %# Format string for columns
C = textscan(fid,str,'CollectOutput',1);   %# Read all fields as strings
                                           %#   (removing 0x's)
fclose(fid);                               %# Close the file
C = C{1};                                  %# Remove outer cell encapsulation

dates = strcat(C(:,1),{' '},C(:,2));         %# Collect the date strings
decValues = cellfun(@hex2dec,C(:,4:end-1));  %# Convert the first 23 columns to
                                             %#   decimal values
decValues = decValues-65536.*(decValues > 32767);  %# Change from unsigned to
                                                   %#   signed 16 bit values
binValues = cellfun(@(n) dec2bin(hex2dec(n),8),...    %# Convert the last column
                    C(:,end),'UniformOutput',false);  %#   to binary strings

If you have N rows in your file, you should end up with:

  • An N-by-1 cell array dates of date strings (which can be converted to either serial date numbers or date vectors).
  • An N-by-23 array decValues containing the converted decimal values. The values were converted from the range 0 to 65535 (i.e. unsigned 16-bit integer) to -32768 to 32767 (i.e. signed 16-bit integer) using two's complement.
  • An N-by-1 cell array binValues containing the converted binary values. Each cell contains a 1-by-8 character string of zeroes and ones.
5
  • I keep getting this error....[Error in ==> hex2decimal at 6 C = textscan(fid,repmat({'%s '},27)); %# Read the data as strings] Also is there command to rewrite a new file with Nx34 size as i have last column converted to 8 binary bits
    – AP.
    Commented Nov 30, 2010 at 4:47
  • @AP: I think I've fixed the bug. Try the new version.
    – gnovice
    Commented Nov 30, 2010 at 4:50
  • @AP: I'm not sure what specific format you want for your output file, but generally you can use the function FPRINTF. For each line in the output, you would print dates and <00000> as strings (%s format), then your 23 decimal values (%d format), then the 8 characters of your binary number (%c format).
    – gnovice
    Commented Nov 30, 2010 at 5:07
  • @gnoice.. Another interesting problem, looking at hex decimal FAEE ect i am sure its a negative value. But it has both negative and positive , how do i distinguish them? I can use [ >> typecast(uint16(sscanf('FAEE', '%4x')),'int16') ] to get the negative values , but what if that number was positive?
    – AP.
    Commented Nov 30, 2010 at 5:14
  • @AP: I've updated the code. In particular, I simplified the removal of the 0x's and added a line that will convert your unsigned 16-bit integer values to signed 16-bit integer values using two's complement.
    – gnovice
    Commented Nov 30, 2010 at 17:15

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.