Binary to Hex to a File in Matlab - string

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.

Related

Bytes Object is Comma Separated Decimal Values (Python 3)

A machine I interface with at my work returns its frequency as a bytes object like this:
b'192,232,206,0'
This little-endian (I think that's right, I'm not great at remembering which is which) bytes object is supposed to translate to a hex bytes object \x00\xCE\xE8\xC0 which translates into decimal as 13560000. I have found that Python has int.from_bytes() which takes the hex bytes object and turns it to a nice integer, but when I apply that to my comma-separated bytes object where each bytes is a decimal value, I get an astronomically large number (3816634650710199623094969186609 to be exact). Can anyone help me out here?
It's a bit convoluted but you can convert the decimal numbers to hex strings and combine their byte values to assemble a number:
freq = b'192,232,206,0'
freq_bytes = b''
for decimal in freq.split(b','):
hex_str = hex(int(decimal))
if hex_str == '0x0':
hex_str += '0' # otherwise it won't convert
freq_bytes += bytes.fromhex(hex_str[2:]) # remove the 0x part
freq_int = int.from_bytes(freq_bytes, 'little')
gives 13560000

convert binary string to hex value

I'm working on my Web Server using Node JS and Express.js. I need to insert hex value in a Buffer, starting from a binary string. My code is something like this:
var buffer = fs.readFileSync("myBINfile.dat");
setValue(buffer, 4);
function setValue(buffer, i) {
var value1 = parseInt(buffer[i].toString(16), 16).toString(2).toString()
value1 = "0" + value1.substring(1, value1.length);
var hex = parseInt(value1, 2).toString(16);
console.log(hex); // print a8 (correct)
buffer[i] = hex;
console.log(buffer[i]); // print 0 (why?)
}
The buffer contains a hex file. value1 is read correctly. How can fix this this problem?
Thanks
You're writing a string value into your Buffer object rather than a numerical value that it expects. Replace the line:
var hex = parseInt(value1, 2).toString(16);
with:
var hex = parseInt(value1, 2);
"a8" is really just the integer value 168 (you'll find if you console.log(value1) before your var hex = parseInt(value1, 2).toString(16); line, you'll get 10101000 (168 in binary)). When you write this value to the buffer, you really just want to write the integer value, not the string "a8".
The "hex value" as you put it, is actually just a number, hex is simply a presentation format. You don't store "hex numbers" or "binary numbers", you just store the numbers themselves.
As a result doing this you'll find console.log(hex); outputs 168 instead and think "That's wrong though!", but it's not, because 168 is a8.
The reason why you'll find it works with some values but not others is because any values which result in a purely numerical hex value (e.g. "22" or "67") will be automatically converted the their numerical equivalent (22 or 67). In your case however "a8" the value cannot be converted to the number type required by the buffer and so is discarded and 0 is written.

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.

text to binary conversion matlab

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')'

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