필터 지우기
필터 지우기

Converting "twos complement" - decimal to actual twos complement and back to decimal

조회 수: 124 (최근 30일)
Hello there
I've been working on a god damn binary-decimal conversion for at least three hours now so please help me out.
I imported a very large .csv file where the columns represent 32bit twos complement but are actual decimal values. So the very first element for example is 4.294.721.774 instead of an awful lot of ones and zeros.
In order to convert this number to the actual real value I tried the following:
% q object for 32bit precision
q = quantizier([32 31]);
% test with first element
num = 4294721774;
bin = de2bi(num, 'left-msb');
% convert binary array to string
str_bin = mat2str(bin);
% cut lead, tail and spaces
str_bin = str_bin(2:end-1);
str_bin = strrep(str_bin, ' ', '');
% convert back
output = bin2num(q, str_bin);
But not only is this highly inefficient, it also doesn't work... Any suggestions?

채택된 답변

Iain
Iain 2014년 8월 29일
Ah. Try:
typecast(uint32(4294721774),'int32')
I'm sure someone mentioned a faster version was in file exchange somewhere.

추가 답변 (2개)

Iain
Iain 2014년 8월 29일
Sounds like you just want to read the value as a signed 32 bit integer....
integer_with_32_bits = int32(4294721774)

Vinci
Vinci 2014년 8월 29일
Sadly no because int32 just gives me the saturated int32 value "2.147.483.647".
What I actually want is the answer -245.522 which (if I just calculated that correctly) is the answer of the conversion
4.294.721.774 -> as binary -> interpret as twos complement -> decimal

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by