2017-04-11 144 views
-1

我需要可視化的二進制文件(例如.EXE)等的圖像
此代碼收拾字節到的圖像中的C#語言:
如何用matlab可視化二進制文件像圖像?

var width = (int)Math.Sqrt(fi.Length * 8); 
width = width + 8 - (width % 8); 
var length = (int)(fi.Length * 8/width); 

Func<byte, int, Color> getcolor = 
     (b, m) => (b & m) == m ? Color.Black : Color.White; 

using (var bitmap = new Bitmap(width, length + 1)) 
{ 
    var buffer = File.ReadAllBytes(exefile); 

    int x = 0, y = 0; 
    foreach (var @byte in buffer) 
    { 
     bitmap.SetPixel(x + 0, y, getcolor(@byte, 0x80)); 
     bitmap.SetPixel(x + 1, y, getcolor(@byte, 0x40)); 
     bitmap.SetPixel(x + 2, y, getcolor(@byte, 0x20)); 
     bitmap.SetPixel(x + 3, y, getcolor(@byte, 0x10)); 

     bitmap.SetPixel(x + 4, y, getcolor(@byte, 0x8)); 
     bitmap.SetPixel(x + 5, y, getcolor(@byte, 0x4)); 
     bitmap.SetPixel(x + 6, y, getcolor(@byte, 0x2)); 
     bitmap.SetPixel(x + 7, y, getcolor(@byte, 0x1)); 

     x += 8; 
     if (x >= width) 
     { 
      x = 0; 
      y++; 
     } 
    } 

    bitmap.Save(Path.ChangeExtension(exefile, ".tif"), ImageFormat.Tiff); 
} 

此代碼二進制文件轉換爲圖像這樣:
enter image description here

任何人都可以給我這個代碼的Matlab實現?

+0

你可以上傳二進制文件嗎? –

+0

@JeruLuke https://www.file-upload.com/eztinwfv1700 – Tavakkoli

回答

1

How can I convert a binary file to another binary representation, like an image

%Matlab具有功能bitget,你想要做什麼。然後你需要把所有的比特放在一個方形矩陣中。

[f,d]=uigetfile('*.*'); 
fid=fopen([d,filesep,f],'r'); 
d = fread(fid,inf,'*uint8'); %load all data as bytes. 
fclose(fid); 
width = sqrt(length(d)*8); 
width = width+8-mod(width,8); %make sure width is a multiple of 8 
IM = false(width); %binary matrix 
x=1;y=1; 
for ct = 1:length(d) 
    v=bitget(d(ct),[1:8]); 
    IM(x:x+7,y)=v; 
    x=x+8; 
    if x>width 
     x=1;y=y+1; 
    end 
end 
imagesc(IM) %display image 
+0

它的工作,但爲什麼生成的圖像是不同的C#和MATLAB中? – Tavakkoli

+0

它可以旋轉90度嗎? 試試這個: imagesc(IM') – Gelliant

+0

如何改變背景和像素顏色?圖像背景可能是黑色的,像素是白色的。 – Tavakkoli