Is it possible to store a array of single precision type into unsigned char?

9 views (last 30 days)
In my existing parsing file there is a assignment
plhs[0] = mxCreateNumericMatrix( nof_data_values, elem_count, mxSINGLE_CLASS ,mxREAL);
where the number of data values is 6 and element count depends upon the no. of samples reading in single precision and in later in that same code another assignment is there
unsigned char* output_mat = reinterpret_cast< unsigned char* >(mxGetData(plhs[0]));
where after reinterpret casting of plhs[0] is getting stored in a matrix.
How is it possible I tried the same but failed using matlab.

Accepted Answer

Walter Roberson
Walter Roberson on 1 Dec 2015
No, you cannot do that directly in MATLAB. MATLAB does not have unsigned char. MATLAB has char(), which is a non-numeric datatype (neither signed nor unsigned) which is 16 bits wide per element. char() are automatically promoted to double when used in arithmetic statement as if they were small positive integers.
If you want to convert an array of single into uint8 then use
typecast(YourArrayOfSingle, 'uint8')
If you want to convert an array of single into char then use either
char(typecast(YourArrayOfSingle, 'uint16'))
or
char(typecast(YourArrayOfSingle, 'uint8'))
Remember, though, that it is common for bytes inside a single precision number to map into unicode code points for which there is no standard glyph.
  5 Comments
Guillaume
Guillaume on 1 Dec 2015
Edited: Guillaume on 1 Dec 2015
Other than the inappropriate language used in the comment, I'll also note that the C++ code is also invalid. Converting a pointer of an unspecified type to a non const pointer of a different type is undefined behaviour in C++.
The conversion from a double pointer to char pointer is possibly safe on most systems (it's still UB), the reciprocal certainly is not due to the potential for mismatched alignment.
James Tursa
James Tursa on 2 Dec 2015
Edited: James Tursa on 2 Dec 2015
@Guillaume: I disagree. Since OP is converting to type (unsigned char *), this is in fact allowed under the rules. You can even see this mentioned under the "Type aliasing" section of your link:
"AliasedType is char or unsigned char: this permits examination of the object representation of any object as an array of unsigned char."
I.e., one is allowed to examine the original type at the byte level under the rules as long as some alignment etc restrictions are adhered to. There are no alignment issues with this specific example (allocated memory for single is going to be aligned properly to be examined at the byte level). This is not UB.
The result of the examination may of course vary from machine to machine (different floating point formats or byte ordering for singles).

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!