converting number to string in matlab - matlab

is there a way to convert numbers to str in matlab
for example, 30120 turns into cat
where c is 03 a is 01 t is 20
here is my progress on RSA encryption/decryption I am trying to decrypt into plain text.
% variables
p=vpi('22953686867719691230002707821868552601124472329079')
q=vpi('30762542250301270692051460539586166927291732754961')
e=vpi('555799999999999');
n=(p.*q)
phi=((q-1).*(p-1))
% how to convert plaintext to integer mod 26
abc = 'abcdefghijklmnopqrstuvwxyz';
word = 'acryptographicallystrongrandomnumbergeneratorwhichhasbeenproperlyseededwithadequateentropymustbeusedtogeneratetheprimespandqananalysiscomparingmillionsofpublickeysgatheredfromtheinternetwasrecentlycarriedoutbylenstrahughesaugierboskleinjungandwachteracryptographicallystrongrandomnumbergeneratorwhichhasbeenproperlyseededwithadequateentropymustbeused';
[temp1, temp2, temp3, temp4, temp5, temp6, temp7,temp8,temp9] = split(word)
[int1,int2,int3,int4,int5,int6,int7,int8,int9] = intsplit(temp1,temp2,temp3,temp4,temp5,temp6,temp7,temp8,temp9)
[encrypt1, encrypt2, encrypt3, encrypt4, encrypt5, encrypt6, encrypt7, encrypt8, encrypt9] = encrypt_this(int1, int2, int3, int4, int5, int6, int7, int8, int9)
[decrypt1, decryt2, decrypt3, decrypt4, decryt5, decrypt6,decrypt7, decryt8, decrypt9] = decrypt_this(encrypt1, encrypt2, encrypt3, encrypt4, encrypt5, encrypt6, encrypt7, encrypt8, encrypt9)

From 30120 to 'cat':
num = 30120;
l = ceil(numel(num2str(num))/2); % number of characters
num2 = sprintf('%06i', num); % num in string form, with leading zero if needed
% '030120'
str = '';
for i = 1:l
str = [str char(96+str2num(num2(2*i-1:2*i)))];
end % 96 is the ASCII offset needed to get a=1, c=3 etc like in your example
Results in str = 'cat'. I think that's what you want.

Related

Proper string conversion with implicit data type in MATLAB

I have arbitrary strings that shall be converted to suited data types (i.e. scalar double, double array or string), depending on their content.
str2num() does its job when interpreting the status return value, but the function itself evaluates the content of the string which:
causes str2num('3-7') to be -4 (double), but I want to stick to '3-7' (char array)
is a severe security issue, since it can potentially execute any code
One workaround is to use str2double(), which does not end up with double arrays, but just scalars or strings. Unfortunately, isstrprop() is not really appropriate for this.
Example inputs (and outputs):
'123.4' -> 123.4 (double scalar) [covered by str2double() and str2num()]
abc' -> 'abc' (char array) [inherently covered by str2double() and str2num()]
'123,456' -> [123, 456] (double array) [covered by str2num() only]
'3-7' -> '3-7' (char array) [don't know how to cover]
use str2double and strsplit:
C = {'123.4','abc','123,456','3-7'};
for ii = 1:numel(C)
CC = strsplit(C{ii},',');
res = [str2double(CC)];
if isnan(res)
res = C{ii};
end
disp(res)
disp(class(res))
disp('****************')
end
shows:
123.4000
double
****************
abc
char
****************
123 456
double
****************
3-7
char
****************
Solution (thanks to #user2999345):
function res = str2impl(str, delimiter)
narginchk(1,2);
if isempty(str)
res = [];
return
end
if nargin < 2
delimiter = ',';
end
splits = strsplit(str, delimiter);
res = str2double(splits);
if any(isnan(res)) & ~strcmpi(str, 'NaN') % NaN not specifically requested
res = str;
end
ends up in
C = {'123.4','abc','123,456','3-7','NaN','',[]};
for ii = 1:numel(C)
r{ii,1} = str2impl(C{ii});
end
disp(r)
[ 123.4000]
'abc'
[1×2 double]
'3-7'
[ NaN]
[]
[]

Excel Range object throws error 0x800A03EC because of a string longer than 255 characters

Using an ActiveX server from MATLAB, I am trying to highlight many cells in an Excel sheet at once. These are not in specific columns or rows so I use Range('A1,B2,...') to access them. However the string accepted by the Range object has to be less than 255 characters or an error:
Error: Object returned error code: 0x800A03EC
is thrown. The following code reproduces this error with an empty Excel file.
hActX = actxserver('Excel.Application');
hWB = hActX.Workbooks.Open('C:\Book1.xlsx');
hSheet = hWB.Worksheets.Item('Sheet1');
col = repmat('A', 100, 1);
row = num2str((1:100)'); %'
cellInd = strcat(col, strtrim(cellstr(row)));
str1 = strjoin(cellInd(1:66), ','); %// 254 characters
str2 = strjoin(cellInd(1:67), ','); %// 258 characters
hSheet.Range(str1).Interior.Color = 255; %// Works
hSheet.Range(str2).Interior.Color = 255; %// Error 0x800A03EC
hWB.Save;
hWB.Close(false);
hActX.Quit;
How can I get around this? I found no other relevant method of calling Range, or of otherwise getting the cells I want to modify.
If you start with a String, you can test its length to determine if Range() can handle it. Here is an example of building a diagonal range:
Sub DiagonalRange()
Dim BigString As String, BigRange As Range
Dim i As Long, HowMany As Long, Ln As String
HowMany = 100
For i = 1 To HowMany
BigString = BigString & "," & Cells(i, i).Address(0, 0)
Next i
BigString = Mid(BigString, 2)
Ln = Len(BigString)
MsgBox Ln
If Ln < 250 Then
Set BigRange = Range(BigString)
Else
Set BigRange = Nothing
arr = Split(BigString, ",")
For Each a In arr
If BigRange Is Nothing Then
Set BigRange = Range(a)
Else
Set BigRange = Union(BigRange, Range(a))
End If
Next a
End If
BigRange.Select
End Sub
For i = 10, the code will the the direct method, but if the code were i=100, the array method would be used.
The solution, as Rory pointed out, is to use the Union method. To minimize the number of calls from MATLAB to the ActiveX server, this is what I did:
str = strjoin(cellInd, ',');
isep = find(str == ',');
isplit = diff(mod(isep, 250)) < 0;
isplit = [isep(isplit) (length(str) + 1)];
hRange = hSheet.Range(str(1:(isplit(1) - 1)));
for ii = 2:numel(isplit)
hRange = hActX.Union(hRange, ...
hSheet.Range(str((isplit(ii-1) + 1):(isplit(ii) - 1))));
end
I used 250 in the mod to account for the cell names being up to 6 characters long, which is sufficient for me.

Reading binary file into matlab

I have a data file that uses (char(1 byte), char[n](array of n chars), word(2 byte unsigned int), short(2 byte signed int), dword(4 byte unsigned int), long(4 byte signed int) and float(4 byte real)) and is supposedly in the following format. I am reading the data file into MATLAB with fopen, fread, etc. but the values I am getting are not what I expect.
Format:
char[8]
<-- outputs 8 ascii values that spell the correct string identifier
dword
<--version of the data files, msw-major version, lsw-minor version (have tried reading as 1 uint32 and 2 uint16's)
dword
dword
dword
dword
<--number of window displays in program
displayinfo[8]
<--contains display window params in the following structure: (not sure what data type this is)
dword
dword
dword
dword
dword
dword
dword
dword
dword
dword
dword
dword
dword
(end of display window params; some are specified as must be a number in [0,3] and they aren't coming out like that)
char[16]
word <-- supposed to be year data was collected (2013) but coming up as 0
Code:
fid = fopen('MIC1.001','rb');
fileIdentifier = fread(fid, 8,'char');
dataFileMajorVersion = fread(fid,1,'uint16');
dateFileMinorVersion = fread(fid,1,'uint16');
numModules = fread(fid,1,'uint32');
fread(fid,1,'uint32'); % value not used
numSwipesCollected = fread(fid,1,'uint32');
numWindowDisplays = fread(fid,1,'uint32');
% display info vars:
displayType = [];
moduleNumber = [];
channelNumber = [];
beginningBar = [];
endBar = [];
vertExpFactor = [];
voltageOffset =[];
isGridEnabled = [];
isEngineeringUnitEnabled = [];
colorOfDisplay = [];
multiChannelIndex = [];
numChannelsForMultiChannelDisp = [];
multiChannelDispStyle = [];
% or does it go through loop for all 8 whether or not there are 8 displays??
for i=1:numWindowDisplays
displayType = [fread(fid,1,'uint32'); displayType];
moduleNumber = [fread(fid,1,'uint32'); moduleNumber];
channelNumber = [fread(fid,1,'uint32'); channelNumber];
beginningBar = [fread(fid,1,'uint32'); beginningBar];
endBar = [fread(fid,1,'uint32'); endBar];
vertExpFactor = [fread(fid,1,'uint32'); vertExpFactor];
voltageOffset =[fread(fid,1,'uint32'); voltageOffset];
isGridEnabled = [fread(fid,1,'uint32'); isGridEnabled];
isEngineeringUnitEnabled = [fread(fid,1,'uint32'); isEngineeringUnitEnabled];
colorOfDisplay = [fread(fid,1,'uint32'); colorOfDisplay];
multiChannelIndex = [fread(fid,1,'uint32'); multiChannelIndex];
numChannelsForMultiChannelDisp = [fread(fid,1,'uint32'); numChannelsForMultiChannelDisp];
multiChannelDispStyle = [fread(fid,1,'uint32'); multiChannelDispStyle];
end
fread(fid,1,'uint32'); % value only used internally
fread(fid,16,'char'); % unused parameter for future use
yearOfDataCollection = fread(fid,1,'uint16');
I would recommend first, just read in all the data at once as a byte array. You'll be able to debug the problem much faster:
fid = fopen('MIC1.001','rb');
data = fread(fid);
fclose(fid);
% could look at it as all chars, just for debugging
char(A)'
The data is read in as a large array of bytes. Then you would go through and parse the bytes by casting them appropriately. You may want to try just testing your method first:
% create a binary file to follow the same format as the specified file
fid = fopen('test.dat','wb');
% Put in 8 character string for file ID
aa = 'myfile0';
fwrite(fid,aa);
% Null terminate it, (I guess)
fwrite(fid,0);
% write the 2 byte file major revision
aa = 1000;
fwrite(fid,aa,'uint16');
% write the 2 byte file minor revision
aa = 5000;
fwrite(fid,aa,'uint16');
% write the 4 byte number of modules
aa = 65536;
fwrite(fid,aa,'uint32');
fclose(fid);
% read the entire file in
fid = fopen('test.dat','rb');
A = fread(fid);
fclose(fid);
% Try to read the file id
disp(char(A(1:8))')
% Try to read the file major revision
majorByte1 = dec2hex(A(9));
majorByte2 = dec2hex(A(10));
% see if it needs byte swapped
tmp1 = hex2dec([majorByte1 majorByte2]);
tmp2 = hex2dec([majorByte2 majorByte1]);
fprintf(1,'File Major: %d ? = %d\nFile Major: %d ? = %d\n',1000,tmp1,1000,tmp2);
For output I get:
myfile0
File Major: 1000 ? = 3715
File Major: 1000 ? = 1000
So, for me, I'll need to byte swap the data, maybe you do too? :-)
EDIT
To do this using fread, from the Matlab docs:
A = fread(fileID, sizeA, precision, skip, machineformat) reads data
with the specified machineformat.
For your case:
A = fread(fid,2,'uint16',0,'b');
I'm assuming you're on a little endian machine, to swap it to little endian, just use a l instead of a b.
number of digit of dec2hex conversion should be given (0x0A0C != 0xAC):
majorByte1 = dec2hex(A(9), 2);
majorByte2 = dec2hex(A(10), 2);
% see if it needs byte swapped
tmp1 = hex2dec([majorByte1 majorByte2]);
tmp2 = hex2dec([majorByte2 majorByte1]);

How to write a unicode symbol in lua

How can I write a Unicode symbol in lua. For example I have to write symbol with 9658
when I write
string.char( 9658 );
I got an error. So how is it possible to write such a symbol.
Lua does not look inside strings. So, you can just write
mychar = "►"
(added in 2015)
Lua 5.3 introduced support for UTF-8 escape sequences:
The UTF-8 encoding of a Unicode character can be inserted in a literal string with the escape sequence \u{XXX} (note the mandatory enclosing brackets), where XXX is a sequence of one or more hexadecimal digits representing the character code point.
You can also use utf8.char(9658).
Here is an encoder for Lua that takes a Unicode code point and produces a UTF-8 string for the corresponding character:
do
local bytemarkers = { {0x7FF,192}, {0xFFFF,224}, {0x1FFFFF,240} }
function utf8(decimal)
if decimal<128 then return string.char(decimal) end
local charbytes = {}
for bytes,vals in ipairs(bytemarkers) do
if decimal<=vals[1] then
for b=bytes+1,2,-1 do
local mod = decimal%64
decimal = (decimal-mod)/64
charbytes[b] = string.char(128+mod)
end
charbytes[1] = string.char(vals[2]+decimal)
break
end
end
return table.concat(charbytes)
end
end
c=utf8(0x24) print(c.." is "..#c.." bytes.") --> $ is 1 bytes.
c=utf8(0xA2) print(c.." is "..#c.." bytes.") --> ¢ is 2 bytes.
c=utf8(0x20AC) print(c.." is "..#c.." bytes.") --> € is 3 bytes.
c=utf8(0x24B62) print(c.." is "..#c.." bytes.") --> 𤭢 is 4 bytes.
Maybe this can help you:
function FromUTF8(pos)
local mod = math.mod
local function charat(p)
local v = editor.CharAt[p]; if v < 0 then v = v + 256 end; return v
end
local v, c, n = 0, charat(pos), 1
if c < 128 then v = c
elseif c < 192 then
error("Byte values between 0x80 to 0xBF cannot start a multibyte sequence")
elseif c < 224 then v = mod(c, 32); n = 2
elseif c < 240 then v = mod(c, 16); n = 3
elseif c < 248 then v = mod(c, 8); n = 4
elseif c < 252 then v = mod(c, 4); n = 5
elseif c < 254 then v = mod(c, 2); n = 6
else
error("Byte values between 0xFE and OxFF cannot start a multibyte sequence")
end
for i = 2, n do
pos = pos + 1; c = charat(pos)
if c < 128 or c > 191 then
error("Following bytes must have values between 0x80 and 0xBF")
end
v = v * 64 + mod(c, 64)
end
return v, pos, n
end
To get broader support for Unicode string content, one approach is slnunicode which was developed as part of the Selene database library. It will give you a module that supports most of what the standard string library does, but with Unicode characters and UTF-8 encoding.

Prefix match in MATLAB

Hey guys, I have a very simple problem in MATLAB:
I have some strings which are like this:
Pic001
Pic002
Pic003
004
Not every string starts with the prefix "Pic". So how can I cut off the part "pic" that only the numbers at the end shall remain to have an equal format for all my strings?
Greets, poeschlorn
If 'Pic' only ever occurs as a prefix in your strings and nowhere else within the strings then you could use STRREP to remove it like this:
>> x = {'Pic001'; 'Pic002'; 'Pic003'; '004'}
x =
'Pic001'
'Pic002'
'Pic003'
'004'
>> x = strrep(x, 'Pic', '')
x =
'001'
'002'
'003'
'004'
If 'Pic' can occur elsewhere in your strings and you only want to remove it when it occurs as a prefix then use STRNCMP to compare the first three characters of your strings:
>> x = {'Pic001'; 'Pic002'; 'Pic003'; '004'}
x =
'Pic001'
'Pic002'
'Pic003'
'004'
>> for ii = find(strncmp(x, 'Pic', 3))'
x{ii}(1:3) = [];
end
>> x
x =
'001'
'002'
'003'
'004'
strings = {'Pic001'; 'Pic002'; 'Pic003'; '004'};
numbers = regexp(strings, '(PIC)?(\d*)','match');
for cc = 1:length(numbers);
fprintf('%s\n', char(numbers{cc}));
end;