convert Binary code into TEXT in MATLAB - matlab

Is there any way to convert binary code in to text/string in MATLAB? I have converted the binary code in decimal value, but couldn't find any way to convert that decimal value into a character using MATLAB according to the ASCII table. Can anyone help please?

Are you looking for char ?
>> char(65:90)
ans = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
and
>> char(bin2dec('1010101'))
ans = 'U'

Here you are a few approaches you can use to achieve your goal:
1) Using the native2unicode function (this supports different encodings, which can be explicitly defined in the second input argument):
native2unicode([77 65 84 76 65 66]) % Output: char 'MATLAB'
2) Using the char function (it supports both Unicode and ASCII encodings, but the conversion is automatically performed):
char([77 65 84 76 65 66]) % Output: char 'MATLAB'
3) Using the underlying Java framework:
java.lang.String(uint8([77 65 84 76 65 66])) % java.lang.String "MATLAB"

Related

matlab replace space character on fgets

I'm using fgets to read lines from file to select specific lines to print into another file.
But i'm having some problem on deleting the space character that exists on the line gotten by the fgets and replacing it for a tab character.
How can I solve this?
First you need to determine exactly which character is the issue. The most common "space" character is ASCII char code 32, but there are others. You can use double to determine the code for each of the characters in a string read in by fgets:
test_str = 'hello world!';
double(test_str)
which returns
104 101 108 108 111 32 119 111 114 108 100 33
Next you can use strrep to replace this with a tab (ASCII char code 9):
test_str = 'hello world!';
new_str = strrep(test_str,' ',char(9))
or using a non-standard whitespace:
test_str = sprintf('hello\fworld!');
new_str = strrep(test_str,char(12),char(9))
It is also possible to do this with regexprep, but strrep is considerably faster even if you need to call it multiple times to replace different characters.
You may also find the isspace, isstrprop, and deblank functions useful.

How to know the string is decoded with which format ? can any one decode this string?

My generated string is "%2fxYdLLMoeO1s34oHAUBjSQ%3d%3d".
Now How can i know that this is decoded/Hashed and in which format ?
can any one decode this string?
I found it seems look like "ei" tag in google parameter.
When i try to decode /xYdLLMoeO1s34oHAUBjSQ== with base64 i get value like
(1) ,(xl#cI
(2) ,(xlߊ#cI
(3) ے,³(xيlكٹ#cI
First encoding is clearly url %-encoding of /xYdLLMoeO1s34oHAUBjSQ==.
The next looks standard base64 of some data (based on == padding).
What actual data represent - your call: FF 16 1D 2C B3 28 78 ED 6C DF 8A 07 01 40 63 49

Convert text to binary and store in a single array in matlab

I need to convert the given text (not in file format) into binary values and store in a single array that is to be given as input to other function in Matlab .
Example:
Hi how are you ?
It is to be converted into binary and stored in an array.I have used dec2bin() function but i did not suceed in getting the output required.
Sounds a bit like a trick question. In MATLAB, a character array (string) is just a different representation of 16-bit unsigned character codes.
>> str = 'Hi, how are you?'
str =
Hi, how are you?
>> whos str
Name Size Bytes Class Attributes
str 1x16 32 char
Note that the 16 characters occupy 32 bytes, or 2 bytes (16-bits) per character. From the documentation for char:
Valid codes range from 0 to 65535, where codes 0 through 127 correspond to 7-bit ASCII characters. The characters that MATLAB® can process (other than 7-bit ASCII characters) depend upon your current locale setting. To convert characters into a numeric array,use the double function.
Now, you could use double as it recommends to get the character codes into double arrays, but a minimal representation would simply involve uint16:
int16bStr = uint16(str)
To split this into bytes, typecast into 8-bit integers:
typecast(int16bStr,'uint8')
which yields 32 uint8 values (bytes), which are suitable for conversion to binary representation with dec2bin, if you want to see the binary (but these arrays are already binary data).
If you don't expect anything other than ASCII characters, just throw out the extra bits from the start:
>> int8bStr =
72 105 44 32 104 111 119 32 97 114 101 32 121 111 117 63
>> binStr = reshape(dec2bin(binStr8b.'),1,[])
ans =
110011101110111001111111111111110000001001001011111011000000 <...snip...>

How does sprintf('%d',A) - '0' work?

I was looking for a way to separate the digits of an array in Matlab i.e.
if A = 1024 then I would like it to be A = [1, 0, 2, 4].
I searched on the net and found this code (also posted on the title):
sprintf('%d',A) - '0'
which converted [1024] -> [1, 0, 2, 4].
It did solve my problem but I did not understand it, especially the - '0' part.
can someone please explain how this works?
Also if I write sprintf('%d',A) + '0' (for A = [1024]) in MATLAB command window then it showed the following:
97 96 98 100
this puzzled me even more can anyone explain this?
It takes advantage of the automatic casting from a char array to a double array when the - operator is used. Remember that each character has an ascii value so if you type
double('0') in the command line and you'll see you get 48 as an answer. While double('1024') gives you
ans =
49 48 50 52
sprintf('%d', A) just convert the integer to a string (i.e. a char array). The minus casts both sides to double so you end up with
double('1024') - double('0')
which is
[49, 48, 50, 52] - [48]
which ends up as [1,0,2,4]
From here it should be clear why adding '0' resulted in [97, 96, 98, 100]
The command sprintf('%d',A) converts integer A=1024 into a string representation of the number, '1024'.
In addition, a string in matlab is really a character array, so if A = '1024' then A(1) = '1'.
The rest of the explanation follows from the answer #Dan posted. When numeric operations (+ - * / mod ^ ...) are applied to character arrays they are converted to the equivalent numeric representation according to the ASCII code, retaining the array format as type double.

Matlab - how to read 2 bytes at a time

I have a number like this - 778310098 - and I want to read 2 bytes at a time. So, I am expecting my output to be 77; 83; 10; 09; 8. I tried using the below:
uint16(fread(fileID,inf, 'ubit8')) and the output I get is the ASCII value of the individual numbers:
55
55
56
51
49
48
48
57
56
What do I need to do to get the desired output?
To read pairs of ASCII digits from a text file (we tend not to describe text files in byets, but in characters), use:
[10 1] * (fread(fileID,[2 inf], 'char') - 48)
To read bytes pairwise from a binary file, try
fread(fileID,inf, '*uint16')
One method is to convert it to a string, then process the string, then convert it back to an integer. While this may not be particularly elegant or perfect, will this do the trick?
a = 778310098;
b = num2str(a);
for i = 1:2:length(b)
if i == length(b) % to handle the case for odd input
split = str2num(b(i))
else
split = str2num(b(i:i+1)) % handle all others
end
end