I have a gui which gives a result as follows. Result=a253ff09c5a8678e1fd1962b2c329245e139e45f9cc6ced4e5d7ad42c4108fc0. Is there a way to truncate the above result to a given bit length? (Say bit lengths of 19,13,10 etc.)
Please help me with the above question.
Thanks a lot in advance.

 채택된 답변

Walter Roberson
Walter Roberson 2015년 12월 30일

0 개 추천

Assuming Result is a string and assuming that the values represent hex,
NeededBitLength = 19; %for example
bitvec = reshape(dec2bin(sscanf(Result,'%1x'),4).',1,[]);
bitvec = bitvec(end-NeededBitLength+1 : end);
truncatedResult = bin2dec(bitvec);
Caution: if you need more than 53 bits this version will not work.

댓글 수: 4

Buddhini Angelika
Buddhini Angelika 2015년 12월 30일
Yes it is the same question. Thanks a lot. If it is more than 53 bits is there another method? Thanks.
bitvec = reshape(dec2bin(sscanf(Result,'%1x'),4).',1,[]);
bitvec = bitvec(end-NeededBitLength+1 : end);
extrabits = mod(NeededBitLength,8);
if extrabits ~= 0
truncatedResult = [repmat('0', 1, 8-extrabits), bitvec];
end
bytevec = uint8(bin2dec(reshape(truncatedResult,8,[]).'));
extrabytes = mod(length(bytevec),8);
if extrabytes ~= 0
bytevec = [repmat(uint8(0), 1, 8-extrabytes), bytevec];
end
truncatedResult = typecast(extrabytes, 'uint64');
This will be a vector of uint64 integers, as many as needed to hold all of the bits. This is needed between 53 and 64 bits to get an integer result. Beyond 64 bits it is not possible to get a single integer as the result (not unless you go for a symbolic number.)
Buddhini Angelika
Buddhini Angelika 2016년 1월 6일
What are symbolic numbers? And how to do it if I go for symbolic numbers?
My program generate results like above and also various integers (n). Then I need to truncate the result as above to obtain a single integer value.
Actually the Result obtained above is a result when I apply the SHA-256 to a given message word.
Please help me to solve this problem. If there is a better hash function to be used please mention it too.
Thanks a lot in advance
Walter Roberson
Walter Roberson 2016년 1월 6일
I am certain that SHA-256 does not expect you to work with word lengths of more than 64 bits. Typical implementations work with arrays of 32 bit words.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by