how to convert decimal to hex inside a text file ?

조회 수: 9 (최근 30일)
deep wilson
deep wilson 2015년 3월 25일
편집: Stephen23 2015년 3월 25일
i have a text file which contains the pixel values of an image in decimal , now i would like to convert these values into hexadecimal and create a new text file ... is it possible??
this is the code i used;
img = imread('index.jpg');
J=rgb2gray(img);
fid = fopen('index.txt', 'w');
if fid == -1, error('Cannot open file');
end
fprintf(fid, '%d %d %d ', size(J));
fprintf(fid, '%g ', J(:));
fprintf(fid,'%x ',dec2hex(img),'\n');
fclose(fid);
  댓글 수: 1
Stephen23
Stephen23 2015년 3월 25일
Yes, it is possible, and it would likely be fairly easy.
But if you don't give us any information about the file format, delimiters, value ranges and so forth there is not much more that we can do to help you. If you want more specific advice, then you need to give us more specific information. Start by reading this:

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

답변 (2개)

Stephen23
Stephen23 2015년 3월 25일
편집: Stephen23 2015년 3월 25일
It seems that mostly you are confused about how to define the fprintf format specifier string: please read the documentation to know how to specify the string format. The documentation is not there just to fill up the internet with pretty pages: it contains lots of information and examples for you to try out.
It seems that you want to do something a bit like this:
img = imread('index.jpg');
%J = rgb2gray(img);
fid = fopen('index.txt', 'wt');
fprintf(fid, '%d %d %d\n', size(img));
fprintf(fid, '%g ', img(:));
fprintf(fid,'\n%x',img);
fclose(fid);
(I don't have rgb2gray). Note that you can also add newlines, tabs and other literal characters (e.g. commas) inside the format specifier string too! Please read the documentation to know how!

Jan
Jan 2015년 3월 25일
Grey images have 2 dimensions only:
fprintf(fid, '%d %d ', size(J));
You do not need '%x' and dec2hex at the same time. Better:
fprintf(fid, '%.2x ', J);

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by