Returning Byte array of given substring

조회 수: 4 (최근 30일)
Jimmy cho
Jimmy cho 2020년 8월 18일
댓글: Jimmy cho 2020년 8월 18일
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

채택된 답변

David Hill
David Hill 2020년 8월 18일
function ByteConvertRxBuffer=ByteConvertRxBuffer(rx)
rx=num2str(rx);
rx=rx(rx~=' ');
rx=bin2dec(reshape(rx,8,[])')';
ByteConvertRxBuffer=[rx,zeros(1,35-length(rx))];
end
  댓글 수: 3
David Hill
David Hill 2020년 8월 18일
function ByteConvertRxBuffer=ByteConvertRxBuffer(rx)
rx=num2str(rx);
rx=rx(rx~=' ');
rx=bin2dec(reshape(rx,8,[])')';
ByteConvertRxBuffer=uint8([rx,zeros(1,35-length(rx))]);
end
Jimmy cho
Jimmy cho 2020년 8월 18일
I understand you , 100% right!
but I want to ask you what's wrong with my implementation, couldn't I call another function while Im inside other function? weird.
I mean the calling function of EvaluateBinary -EvaluateBinary(substring)- is returning total, so I want total to store it in a specific index in array ByteConvertRxBuffer as what I did above .. can't I do that in matlab? I mean the function EvaluateBinary returns total, so total -returned value of EvaluateBinary(substring)- will be stored as a value in specific index in my array ByteConvertRxBuffer that I called the function from there ..
Can't I do that in matlab? mean call other function that returns another value and the another value I want to use it in the caller function ..?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bruno Luong
Bruno Luong 2020년 8월 18일
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
Bruno Luong
Bruno Luong 2020년 8월 18일
편집: Bruno Luong 2020년 8월 18일
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.
Jimmy cho
Jimmy cho 2020년 8월 18일
thanks !!!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by