Convert text to binary and store in a single array in matlab - 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...>

Related

String bytes end in 255 for Swift

Is there any reason why when I run the following
var name = "A"
withUnsafeBytes(of: &name, { bytes in
print (bytes)
for byte in bytes.enumerated() {
print (byte.element, byte.offset)
}
})
The last byte is 255?
I expected the bytes to just contain 65 as that is the ASCII code!
That is, byte 0 is 65 (as expected) and byte 15 is 255 (all the rest are zeroed)
Why is byte 15 255?
struct String is a (opaque) structure, containing pointers to the actual character storage. Short strings are stored directly in those pointers, which is why in your particular case the byte 65 is printed first.
If you run your code with
var name = "A really looooong string"
then you'll notice that there is no direct connection between the output of the program and the characters of the string.
If the intention is to enumerate the bytes of the UTF-8 representation of the string then
for byte in name.utf8 { print(byte) }
is the correct way.

Input HEX as a string

How can I treat a string which is a hexadecimal number as a hexadecimal number? For example, I am loading in a file of hexadecimal numbers but Python is loading the file in as a string. Is it possible to get Powershell to treat the hexadecimal numbers in the file as hexadecimal numbers?
Background: I'm asking the user for a Mac Address and I need to add x numbers to the last block to get the next x Mac addresses. I can split on ":" and get the last block but this is a string containing for example "13", which is not 13 decimal but 13 hex.
Try converting it first to an int by casting the variable with the base 16 as the second argument
hex_str = "0xAD4"
hex_int = int(hex_str, 16)
then just add 1 to the result and use
hex(hex_int)
to get the result back to hex format
Just the whole MAC address in once by using the PhysicalAddress.Parse(String) Method:
$MAC = 'F0:E1:D2:C3:B4:A5' -Replace '[^0-9a-fA-F]' # remove all the non-Hex characters
[System.Net.NetworkInformation.PhysicalAddress]::Parse($MAC).GetAddressBytes()
240
225
210
195
180
165
Solved it by doing it like this:
$MacNr = ([int64]"0x$($MacAddress.Split(":")[5])")
$MacNr ++1
$NewMac = ('{0:X2}' -f $MacNr)

convert Binary code into TEXT in 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"

how to convert an arrary of 16-bit unsigned intergers into ascii string in matlab

I am looking for a way to convert an array of 16-bit unsigned integer into ASCII char array. I am using char to do the conversion
D=[65 65 65 65];
char(D)
which will show 4 'A'. However, since each number in D is 16-bit, I expect it to convert each number to 2 chars. For example, if I have
D=[16707]
char(D)
I expect it gives me two chars 'A' and 'C'. But char always return 1 character. Is that anyway to force char to convert like the way I stated? Thanks.
For this, you need to write your own function.
You can use char() to convert most significant byte and least significant byte separately.
k = 16707;
first = char(bitand(bitshift(k, -8), 255));
second = char(bitand(k, 255));
Have a look at
http://www.mathworks.com/help/matlab/ref/char.html
It cleatly states that the char function is valid only for 8 bit numbers. you can convert each part of cell of the array with this and contact the results for each two cells.
Use typecast to convert each uint16 to two uint8, and then apply char. Make sure that the input to typecastr is really of type uint16.
If you need to reverse char order, use swapbytes on the uint16 vector.
>> D = [16707 16708];
>> char(typecast(uint16(D),'uint8'))
ans =
CADA
>> char(typecast(swapbytes(uint16(D)),'uint8'))
ans =
ACAD

Converting Decimal array to Hex array in Matlab

Let's say i have a 1x32 double array Input in matlab workspace. This variable has all positive decimal values. I want to convert each value to Hex & store it in another array Output
I use dec2hex(Input) & it generates a character string with Hex values. Now, i want an array of Hex numbers & not a string.
How do i convert this Hex string into Hex array of 1x32 Output
If i use str2num or str2double, it gives empty & NaN respectively?
How to do it
to get neither empty nor Nan values use `hex2dec'. something like this works for me:
a=1:20;
b=dec2hex(a);
c=hex2dec(b)
ans =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Matlab does not manage hexadecimal numbers per se, only decimal notation. That's why matlab stores hexadecimal numbers in string format.
To do an addition of hex for example, you have to pass through the decimal notation:
a='ABC';
b='123';
c=dec2hex(hex2dec(a)+hex2dec(b))