Returning Byte array of given substring
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jimmy cho
el 18 de Ag. de 2020
Comentada: Jimmy cho
el 18 de Ag. de 2020
Hi guys , I've implemented in matlab a function that gets as its input an array of binary values (0 or 1) and it returns the Byte value of every 8 bit in my input array.
this means if I input to it [1 1 1 1 1 1 1 1] , the returned value is [255 0 0 0 0 0 0 0 ...%34 zeros%], another example if I input to it 16 bit array of 1
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1] , so my function returns an array [255 255 0 0 0 0 0 0 0 ...%32 zeros..%] -I just wrote 32 zeros in my array which means 0 0 0 0 .. 32 times. it should return twice 255 because first 8bit from left to right of my array is 255 in unsigned int, and the other following 8bit of 1 also 255 in unsigned int .. so my function return twice 255 , first 255 for first 8bit of 1, and the second 255 value at index 2 is the second 8bit of 1 in my input array.
I have implemented this in matlab but it doesn't give me correct answers, any help please? the input is always its length divisible by 8.(the input length is multiplication of 8)
this what Im implementing called ByteConvertRxBuffer and return an array of Byte values(unsgined integers array values)
function ByteConvertRxBuffer=ByteConvertRxBuffer(rx)
ByteConvertRxBuffer = zeros(1,35,'uint8'); %starting array with size 35 which all its %values are zero
ireal = 0;
% int byteSize=8;
%warning: some of the below code becomes invalid if byteSize exceeds 32767
byteSize = 8 ;
i = 0; %#ok<NASGU> %size_t i inside block
for i = 0: byteSize: (numel(rx))
nibble = zeros(1,byteSize); %for taking every 8bit of my input seperately
for j = 1 : 1 : byteSize %taking every 8bit of my input seperately
nibble(j) = rx(j+i);
end %after it finishes 8 iterations then out from the current loop
clear j %int j was local to block
ByteConvertRxBuffer(ireal) = EvaluateBinary(nibble); % here I want to put every value of this returned function to ByteConverRxBuffer array
ireal=ireal+1;
end
Im calling this function:
function total=EvaluateBinary(substring)
byteSize=8;
total = uint8(0); %this varibal is unsigned integer of 8bit
counter = uint8(1);
for i=byteSize:-1:1
if (substring(i) == 1)
total=total + counter; end
counter = counter*2;
end
0 comentarios
Respuesta aceptada
David Hill
el 18 de Ag. de 2020
function ByteConvertRxBuffer=ByteConvertRxBuffer(rx)
rx=num2str(rx);
rx=rx(rx~=' ');
rx=bin2dec(reshape(rx,8,[])')';
ByteConvertRxBuffer=[rx,zeros(1,35-length(rx))];
end
3 comentarios
David Hill
el 18 de Ag. de 2020
function ByteConvertRxBuffer=ByteConvertRxBuffer(rx)
rx=num2str(rx);
rx=rx(rx~=' ');
rx=bin2dec(reshape(rx,8,[])')';
ByteConvertRxBuffer=uint8([rx,zeros(1,35-length(rx))]);
end
Más respuestas (1)
Bruno Luong
el 18 de Ag. de 2020
This should work
function ByteConvertRxBuffer=ByteConvertRxBuffer(rx)
ByteConvertRxBuffer = zeros(1,35,'uint8'); %starting array with size 35 which all its %values are zero
ireal = 0;
% int byteSize=8;
%warning: some of the below code becomes invalid if byteSize exceeds 32767
byteSize = 8 ;
for i = 1: byteSize: (numel(rx))
nibble = zeros(1,byteSize); %for taking every 8bit of my input seperately
for j = 1 : 1 : byteSize %taking every 8bit of my input seperately
nibble(j) = rx(j+i-1);
end %after it finishes 8 iterations then out from the current loop
ireal=ireal+1;
ByteConvertRxBuffer(ireal) = EvaluateBinary(nibble); % here I want to put every value of this returned function to ByteConverRxBuffer array
end
end
function total=EvaluateBinary(substring)
byteSize=8;
total = uint8(0); %this varibal is unsigned integer of 8bit
counter = uint8(1);
for i=byteSize:-1:1
if (substring(i) == 1)
total=total + counter;
end
counter = counter*2;
end
end
5 comentarios
Bruno Luong
el 18 de Ag. de 2020
Editada: Bruno Luong
el 18 de Ag. de 2020
for i=1:bytesize:numel(rx) give you
i=1
i=8
which means two iterations , not?! numel(rx) in my example is 8.
NO!!!! i=1 only, one iteration think again.or even better run it.
Ver también
Categorías
Más información sobre String en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!