Write Decimals to 12 Bit Signed Binary
24 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Karl Haebler
am 28 Nov. 2017
Kommentiert: Walter Roberson
am 29 Nov. 2017
I am trying to take a set of integer decimal inputs (ranging from -100 to 100) and map them to 12 bit signed binary. The trick is that the binary must consists of 12 bits, even if there are leading zeros. So far I have generated the integers, converted them to 16 (not 12) bit signed binary, but am really struggling with making all of the binary numbers 12 bits long.
For example, if the integer is 23, the binary should be 000000010111. If the integer is -23, the binary should be 111111101001. I then need to write these signed binary numbers to a text file with each binary number on a new line. So far I have what is below, but the output is 16 bit and there are no leading zeros. I have struggled for an answer but haven't found it. Any help is greatly appreciated!
integers=round(200*(rand(1,8000)-0.5));
for i=1:8000
binary{i}=dec2bin(typecast(int16(integers(i)),'uint16'));
end
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 28 Nov. 2017
temp = integers;
mask = temp < 0;
temp(mask) = 2^12 + temp(mask) ;
binary = dec2bin(temp, 12);
3 Kommentare
Walter Roberson
am 29 Nov. 2017
temp = cellstr(binary);
fprintf(fileID, '%s\n', temp{:});
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!