6
$\begingroup$

This question appeared maybe an hour ago, but was removed by OP. I'm still interested in a solution. Suppose we have a text file with hex numbers separated by tabs (or spaces):

000F    6D30    5800    4680    3790    2910    1B50    0DB0
02E0    F6F0    ED20    E7A0    E6D0    1240    3490    4E50
6700    7670    7B60    6480    5140    4120    3100    23A0
14D0    0B60    FCA0    F2D0    E990    E670    E880    20B0
3F20    56E0    6C00    7C90    72B0    5C90    4A20    3B70
2C40    1E30    1140    0500    F960    EF10    E890    E620

And we want to get a 2D List of integers. Is there some sort of format specifier we can add to Import?

I tried a straightforward solution and it works, but looks over-complicated:

importTable[fileName_, separator_] :=
  Map[FromDigits[#, 16] &, 
     ToExpression /@ (# /. {"A" -> 10, "B" -> 11, "C" -> 12, 
           "D" -> 13, "E" -> 14, "F" -> 15}) & /@ 
      Characters /@ StringSplit[#, separator], 1] & /@ 
   Import[fileName, "Lines"];

Using importTable["test.dat", WhitespaceCharacter ..] will parse the above.

Is there a "native" way of doing it?

$\endgroup$
  • 1
    $\begingroup$ I think your solution can be written more simply like this: Map[FromDigits[#, 16] &]@*StringSplit /@ Import[file, "Lines"] $\endgroup$ – C. E. Jun 24 '15 at 19:37
  • $\begingroup$ @Pickett Indeed… somehow it didn't work for me when I tried something similar $\endgroup$ – BlacKow Jun 24 '15 at 19:41
  • 2
    $\begingroup$ I had suggested Map[FromDigits[#, 16] &, Import["test.dat", "Table", "Numeric" -> False], {2}], but the original thread got deleted. $\endgroup$ – J. M. will be back soon Jun 24 '15 at 19:51
  • $\begingroup$ @Guesswhoitis. "Numeric" -> False is it documented anywhere? $\endgroup$ – BlacKow Jun 24 '15 at 20:00
  • $\begingroup$ In the documentation for the "Table" format, IIRC. $\endgroup$ – J. M. will be back soon Jun 24 '15 at 20:01
2
$\begingroup$

Where possible I try to use ReadList as it is usually quite a bit faster than Import:

hex[s_String] := FromDigits[s, 16]

ReadList["test.dat", hex[Word], RecordLists -> True]
{{15, 27952, 22528, 18048, 14224, 10512, 6992, 3504},
 {736, 63216, 60704, 59296, 59088, 4672, 13456, 20048},
 {26368, 30320, 31584, 25728, 20800, 16672, 12544, 9120},
 {5328, 2912, 64672, 62160, 59792, 58992, 59520, 8368},
 {16160, 22240, 27648, 31888, 29360, 23696, 18976, 15216},
 {11328, 7728, 4416, 1280, 63840, 61200, 59536, 58912}}

For an explanation of the use of hex[Word] as a type see:

Also for whatever interest it may hold you could do something with string processing:

StringCases[
 "000F    6D30    5800    4680    3790    2910    1B50    0DB0", 
 h : HexadecimalCharacter .. :> FromDigits[h, 16]
]
{15, 27952, 22528, 18048, 14224, 10512, 6992, 3504}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.