Here is a string-to-binary conversion program.
str='haseeb'
int16bStr = uint16(str);
typecast(int16bStr,'uint8');
a=dec2bin(int16bStr)
I'm using this code to convert the text "haseeb" into binary form:
str =
haseeb
a =
1101000
1100001
1110011
1100101
1100101
1100010
I want to convert this binary output of "a" back to text (i.e. "haseeb"). I am using Matlab r2009b.
You can just do this :
word = reshape(char(bin2dec(a)),1,[])
And you can skip your typecast just by doing
int8bStr = uint8(str);
Related
I am trying to come to the point where I create a graph on a given data that I am supposed to read from a text file.
So I use in my code fopen to open the text file, textscan to scan it, than make a string out of it and by using split I want to cut of the first part of every line and use the second part so that I can decode it into json and then use the information.
So my text file consists of two lines of information:
123456.99 :: working completed: result=0 , data ="{"day":"monday", "breakfast":"sandwich"}"
123456.99 :: working completed: result=0 , data ="{"day":"tuesday", "breakfast":"bread"}"
The first part of my code:
fileID = fopen('test1');
text = textscan(fileID, '%s', 'delimiter','\n','whitespace','');
strLog = string(text{1});
res = split(strLog, "data =");
json_str = res(:, 2)
And as a result I get a 2x1 string array. Output:
json_str =
2×1 string array
""{"day":"monday", "breakfast":"sandwich"}""
""{"day":"tuesday", "breakfast":"bread"}""
This is where I got stuck.
My first idea was to call cellfun and apply jsondecode.
But I got
Error using jsondecode JSON syntax error at line 1, column 4
(character 4): extra text.
But it makes no sence to me, since that should be the " from "day" which for json should be okay!?
In json_str you have quote marks " at the start and end. These need to be removed for jsondecode to work. For example J = jsondecode(json_str{1}(2:end-1)).
You can then use cellfun to process all elements. For example,
S = cellfun(#(x)jsondecode(x(2:end-1)),json_str)
I have to read a textfile which contains a list of companycodes. The format of the textfile is:
[1233A12; 1233B88; 2342Q85; 2266738]
Even if I have read the file? Is it possible to compare these numbers with regular numbers? Because I have the codes from two different data-bases and one of them has regular firmnumbers (no characters) and the other has characters inside the firmnumbers.
Btw the file is big (50+mb).
Edit: I have added an additional number in the example because not all the numbers have a character inside
If you want to compare part of a string with a number, you could do it as follows:
combiString = '1234AB56'
myNumber= 1234
str2num(combiString(1:4))==myNumber
str2num(combiString(7:8))==myNumber
You can achieve this result by using regular expressions. For example, if str = '1233A12' you can write
nums = regexp(str, '(\d+)[A-Z]*(\d+)', 'tokens');
str1 = nums{1}(1);
num1 = str2num(str1{1});
str2 = nums{1}(2);
num2 = str2num(str2{1});
I'm trying to calculate the RIPEMD160 hash in matlab for some data represented by a hex string. I found the following java class and compiled it for jvm 1.6
http://developer.nokia.com/Community/Wiki/RIPEMD160_encryption_in_JavaME
the following code works perfectly in matlab for hashing strings:
clear all
% add folder with class file to java path
functions_folder = strcat(pwd,filesep,'functions');
javaaddpath(functions_folder)
% string to hash
string_to_hash = 'test12345';
% convert to java String
str_to_hash_java = javaObject('java.lang.String',uint8(string_to_hash));
% pass in string and convert output to char array
mystr = char(RIPEMD160.RIPEMD160String(str_to_hash_java))
Now my problem comes about when I try to hash some binary data represented by a hex string. The hash output is correct for hex values of 7f or smaller, but once I have 8 bits (>= 80) it no longer gives the correct answer. I can't seem to find the problem. Here is my code:
clear all
% add folder with class file to java path
functions_folder = strcat(pwd,filesep,'functions');
javaaddpath(functions_folder)
% data to hash in hex format
hex_string_in = '80';
hex_string_in_length = length(hex_string_in);
% split every to characters and calculate the data in each byte
for i=1:hex_string_in_length/2
data_uint8_array(1,i) = uint8(hex2dec(hex_string_in(2*i-1:2*i)));
end
% constructor
x = RIPEMD160;
% pass in binary data
x.update(data_uint8_array)
% get hash in binary format
hash_out_bin = x.digestBin();
% typecast binary data into unit8 primitive
hash_out_unit8=typecast(hash_out_bin,'uint8');
% convert to hex
hash_out_hex = dec2hex(hash_out_unit8)';
% pad with zeros if bytes all smaller than hex(80)
if(size(hash_out_hex,1))==1
hash_out_hex=[repmat('0',[1 size(hash_out_hex,2)]);hash_out_hex];
end
% final formatting, convert to lowercase
hash_out_hex = lower(hash_out_hex(:)')
for an input of '7f' it produces the correct hash of c8297aad716979548921b2e8e26ca8f20061dbef
but for '80' is gives e633ca40d977e24a1ffd56b7a992e99b48d13359 instead of the correct result b436441e6bb882fe0a0fa0320cb2d97d96b4d1bc
Thanks.
You are passing strings to your Java code instead of regular byte arrays. Not all bytes are representations of valid character encodings. Therefore you are likely to loose information. Only hash bytes, without any conversion. If strings are required use base 64 encoding/decoding.
str_to_hash_java should not be needed.
I have a matrix, say:
M = [1000 1350;2000 2040;3000 1400];
I wish to write this matrix onto a text file in the hex format, like this:
0x000003e8 0x00000bb8
0x000007d0 0x000007f8
0x00000bb8 0x00000578
I considered using the function dec2hex but it's very slow and inefficient. It also gives me the output as a string, which I don't know how to restructure for my above required format.
MATlab directly converts hex numbers to decimal when reading from a text file, ex. when using the function fscanf(fid,'%x').
Can we do the exact same thing while writing a matrix?
You can use the %x format string. For the sake of demonstration, see an example with sprintf below. If you want to write to a file, you will have to use fprintf.
M = [1000 1350;2000 2040;3000 1400];
str = sprintf('0x%08x\t0x%08x\n', M')
this results in
str =
0x000003e8 0x00000546
0x000007d0 0x000007f8
0x00000bb8 0x00000578
You may use num2str with a format string:
str = num2str(M, '0x%08x ');
which returns
str =
0x000003e8 0x00000546
0x000007d0 0x000007f8
0x00000bb8 0x00000578
Using this instead of sprintf you do not need to repeat the format string for each column.
I have my sparse features stored in a text file in the following format (ArrayIndex: Value). Currently I am parsing the text using reg-expressions and converting this to a matlab array. What I wanted to know was, is there a faster/better more MATLAB-ish approach to convert this format of data into a matlab array.
2402:0.099061 2404:0.136546 2406:0.447161 2407:0.126333 2408:0.213803 2411:0.068189 2416:0.223526 2417:0.090420
EDIT:
You can read and parse the file using TEXTSCAN, then the build sparse matrix from those values:
fid = fopen('input.txt');
C = textscan(fid, '%f:%f');
fclose(fid);
C = sparse(1,C{1},C{2});
result:
>> C
C =
(1,2402) 0.099061
(1,2404) 0.13655
(1,2406) 0.44716
(1,2407) 0.12633
(1,2408) 0.2138
(1,2411) 0.068189
(1,2416) 0.22353
(1,2417) 0.09042