Effacer les filtres
Effacer les filtres

Convert an array of type char to hex values

81 vues (au cours des 30 derniers jours)
Robert Scott
Robert Scott le 30 Juil 2021
Modifié(e) : dpb le 31 Juil 2021
I have a character array read in from a csv
I want to convert the entire array to hex values before processing the data
This saves me computation time by not having to call hex2dec or hex2binaryvector 50000 times
Anyone have any ideas?

Réponses (2)

Walter Roberson
Walter Roberson le 31 Juil 2021
The edit I just made, to use a lookup table, is the fastest method yet, more than 50 times faster than dec2hex()

dpb le 31 Juil 2021
I went back and found your first posing of roughly a week ago -- and made a comment there.
In short, take Walter's advice from back then and use either textscan for fscanf or readmatrix and do the conversion on input inside the C i/o library instead.
Not being willing to share anything; even a dummy-data file of typical structure pretty-much limits how much more specific can get.
  4 commentaires
Robert Scott
Robert Scott le 31 Juil 2021
its a .csv file
It is hex with the exception of the first and last col
Not sure what you mean by encoded
dpb le 31 Juil 2021
Modifié(e) : dpb le 31 Juil 2021
"It is hex with the exception of the first and last col"
So what are they? Format has to match column by column to read proper types; otherwise one gets what MATLAB thinks should be -- and default as you've discovered is that hex comes in as char()
"what you mean by encoded"
Is there any leading 0X prefix or the like in the file or just string of hex digits.
The format string encoding textscan uses for hex inputs is as follows: <textscan formatSpec>
Conversion Output
Specifier Class
Hexadecimal number, unsigned integer %xu int64
%xu8u int8
%xu16u int16
%xu32u int32
%xu64u int64
Hexadecimal number, signed integer %xs8 int8
%xs16 int16
%xs32 int32
%xs64 int64
So, which of the above are your values?
Again, it would be SO much easier if you would just attach a small sample file, even if the data are random -- and there's absolutely nothing anybody could do with a set of hex numbers in isolation, even if they were the real thing.
If there's something proprietary in the first/last column, that could be obfuscated as long as it was same data type.
But, I did discover one thing I was not aware of that had presumed was so since was in Walter's earlier response -- readtable and friends have not yet been extended to accept the hex type as input -- why, I've no idea; seems a glaring oversight.
So, we are down to lower-level routines, but if speed is the intent, they're better, anyways, just a little more up front work--although Walter also showed that by lookup table the conversion from hex can be pretty quick.
One last Q? on the file structure -- is the size known a priori or can it be inferred from some other piece of information or is it unknown in length but fixed number of columns or both? It's most important to know how many columns although that can be found out by reading the first record and counting delimiters, if is known a priori can save the step as well as dynamic building of a format string to match in favor of fixed one.

Connectez-vous pour commenter.




Community Treasure Hunt

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

Start Hunting!

Translated by