how to convert decimal to hex inside a text file ?

9 visualizzazioni (ultimi 30 giorni)
deep wilson
deep wilson il 25 Mar 2015
Modificato: Stephen23 il 25 Mar 2015
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 Commento
Stephen23
Stephen23 il 25 Mar 2015
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:

Accedi per commentare.

Risposte (2)

Stephen23
Stephen23 il 25 Mar 2015
Modificato: Stephen23 il 25 Mar 2015
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 il 25 Mar 2015
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);

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by