text to binary conversion matlab - text

I need to import a text file into matlab and convert it into binary so that the final array that contains the binary value is in some integer format and not character format. How can I do it?

fid = fopen('myFile.txt', 'r');
F = fread(fid, 'char=>uint32')'

Related

Convert unicode code points to alphabetical strings in Python

I have a list of unicode code points like
<U+041D><U+041C><U+0418><U+0426> <U+041D><U+0435><U+0439><U+0440><U+043E><U+0445><U+0438><U+0440><U+0443><U+0440><U+0433><U+0438><U+0438> <U+0438><U+043C>. <U+041D>,<U+041D>.<U+0411><U+0443><U+0440><U+0434><U+0435><U+043D><U+043A><U+043E>
How do I convert these to a regular string - they represent a particular string like 'hello'
Use regular expressions to identify unicode points. Extract the hexadecimal number from each point, convert it to decimal, and map to the character with that code:
import re
def decoder(match):
code = match.group(1) # The code in hex
return chr(int(code, 16))
uni = # Your string
re.sub("<U\+([0-9a-fA-F]+)>", decoder, uni)
#'НМИЦ Нейрохирургии им. Н,Н.Бурденко'

Converting 16-digit hexadecimal string into double value in Python 3

I want to convert 16-digit hexadecimal numbers into doubles. I actually did the reverse of this before which worked fine:
import struct
import wrap
def double_to_hex(doublein):
return hex(struct.unpack('<Q', struct.pack('<d', doublein))[0])
for i in modified_list:
encoded_list.append(double_to_hex(i))
modified_list.clear()
encoded_msg = ''.join(encoded_list).replace('0x', '')
encoded_list.clear()
print_command('encode', encoded_message)
And now I want to sort of do the reverse. I tried this without success:
from textwrap import wrap
import struct
import binascii
MESSAGE = 'c030a85d52ae57eac0129263c4fffc34'
#Splitting up message into n 16-bit strings
MSGLIST = wrap(MESSAGE, 16)
doubles = []
print(MSGLIST)
for msg in MSGLIST:
doubles.append(struct.unpack('d', binascii.unhexlify(msg)))
print(doubles)
However, when I run this, I get crazy values, which are of course not what I put in:
[(-1.8561629252326087e+204,), (1.8922789420412524e-53,)]
Were your original numbers -16.657673995556173 and -4.642958715557189 ?
If so, then the problem is that your hex strings contain the big-endian (most-significant byte first) representation of the double, but the 'd' format string in your unpack call specifies conversion using your system's native format, which happens to be little-endian (least-significant byte first). The result is that unpack reads and processes the bytes of the unhexlify'ed string from the wrong end. Unsurprisingly, that will produce the wrong value.
To fix, do one of:
convert the hex string into little-endian format (reverse the bytes, so c030a85d52ae57ea becomes ea57ae525da830c0) before passing it to binascii.unhexlify, or
reverse the bytes produced by unhexlify (change binascii.unhexlify(msg) to binascii.unhexlify(msg)[::-1]) before you pass them to unpack, or
tell unpack to do the conversion using big-endian order (replace the format string 'd' with '>d')
I'd go with the last one, replacing the format string.

Binary to Hex to a File in Matlab

I have a binary vector which I converted to hex. I want this hex data to go to a .bin(or any other fornat) file. Since this is a vector,I tried to first convert it to a string so that the hex data can be formatted and then output to a file. Please see below the code I was trying to use. Also shown is the snapshot of the problem. As you can see, all the converted hex data is present in 1 cell. I want it to be byte wise for each cell.
my_new_vector = binaryVectorToHex(M); %M is my input binary matrix
%cellfun(FormatHexStr, mat2cell(my_new_vector), 'UniformOutput', false)
%new_vector = mat2cell(my_new_vector);
vect2str(my_new_vector); %[matlab file exchange function][2] for converting vector to string
FormatHexStr(my_new_vector,2); %FormatHexStr is a function for formatting hex values which requires a hex string as an input [function is here][3]
[n_rows,n_cols] = size(my_new_vector);
fileID = fopen('my_flipped_data.bin','wt');
for row = 1:n_cols
fprintf(fileID,'%d\n',my_new_vector(:,row));
end
fclose(fileID);
Convert the binary values into blocks of 8 in a uint8 (byte) variable, then write it to file.
nbytes = floor(length(M)/8);
bytevec = zeros(1,nbytes, 'uint8');
for i = 1:8
bytevec = bytevec + uint8((2^(8-i))*M(i:8:end));
end
fileID = fopen('my_flipped_data.bin','wb');
fwrite(fileID, bytevec);
fclose(fileID);
This writes the first bit as the MSB of the first byte. Use (2^(i-1)) for first bit as LSB.

How do I convert a numeric string to its corresponding Unicode character?

Given a string such as "2764" how can i programatically convert it to "\u2764"? Is there a built in function that will let me convert a standard string to its unicode-escaped equivalent?
http://docs.python.org/3/library/functions.html#chr
>>> chr( int('2764', 16) )
❤
First convert your string to the number that is intended. Then convert it to the corresponding character.

Lua: Read hex values from binary

I'm trying to read hex values from a binary file. I don't have a problem with extracting a string and converting letters to hex values, but how can I do that with control characters and other non-printable characters? Is there a way of reading the string directly in hex values without the need of converting it?
Have a look over here:
As a last example, the following program makes a dump of a binary file. Again, the first program argument is the input file name; the output goes to the standard output. The program reads the file in chunks of 10 bytes. For each chunk, it writes the hexadecimal representation of each byte, and then it writes the chunk as text, changing control characters to dots.
local f = assert(io.open(arg[1], "rb"))
local block = 10
while true do
local bytes = f:read(block)
if not bytes then break end
for b in string.gfind(bytes, ".") do
io.write(string.format("%02X ", string.byte(b)))
end
io.write(string.rep(" ", block - string.len(bytes) + 1))
io.write(string.gsub(bytes, "%c", "."), "\n")
end
From your question it's not clear what exactly you aim to do, so I'll give 2 approaches.
Either you have a file full with hex values, and read it like this:
s='ABCDEF1234567890'
t={}
for val in s:lower():gmatch'(%x%x)' do
-- do whatever you want with the data
t[#t+1]=s:char(val)
end
Or you have a binary file, and you convert it to hex values:
s='kl978331asdfjhvkasdf'
t={s:byte(1,-1)}

Resources