hi
hi,
i have a .hex file and i have to read it and convert and sasve it in decimal format.
any ideas plzzz

Wath do you mean by a hex file?
A binary file or a text file with hexadecimal characters?

its basically a .hex file which cant be read with a simple notepad. It will show some garbage values. I read the file with a software Hex Editor. But i cant understand how i will read it from the file and know its hexa value.

My reply from this other thread is below.

Do you understand different number bases?

You could use an atoi(...) type function to convert it to an integer, then you can output it in whatever number base you want to.

Or, if you want to do it manually, divide out and sum the multiples of powers of 16.

i.e. 0x539 = 5*(16^2) + (3*16^1) + 9*(16^0) = 5*256 + 3*16 + 9*1 = 1280 + 48 + 9 = 1337.

The rightmost (least significant bit, LSB) represents 16^0, or 1, times the value 0-F, and so on, with each position to the left increasing the power of 16. So, from right to left, it's some number 0-15 * n where n is increasing from 0 at the right most.

thanx 4 the reply but i know that.
Here the problem is that the person who created the file has saved in .hex format which cant be read in notepad,will show garbage values. How should i read that file.

All of my .hex files open in notepad just fine. Same for WordPad.

If you just want to manually inspect you can use something like WinHex, but I suspect that notepad should read them OK. Perhaps it's a text encoding issue (ANSI/ASCII/UniCode) :?:

Perhaps it is more something like this you want:

using System;
using System.IO;
using System.Windows.Forms;

namespace HexDump
{
    class Program //simulate hexdump utility
    {
        [STAThread]
        static void Main(string[] args)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                DumpHexFile(dlg.FileName);
            }
            Console.ReadKey();
        }

        static void DumpHexFile(string fileName)
        {
            FileStream fSTR;

            try
            {
                fSTR = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
            }
            catch (Exception ex)
            {
                Console.WriteLine(">>> ERROR HEXDUMP: {0}", ex.Message);
                return;
            }
            Console.WriteLine(" ------------ HEXDUMP " + fileName + " -------------");
            Console.WriteLine();
            DumpStream(fSTR);
            fSTR.Close();
        }

        static void DumpStream(Stream stream)
        {
            byte[] someBytes = new byte[16];
            long fAddress = 0;
            int cnt;

            while ((cnt = stream.Read(someBytes, 0, 16)) > 0)
            {
                Console.WriteLine(ContructLine(fAddress, someBytes, cnt));
                fAddress += 16;
            }
        }

        static string ContructLine(long Adr, byte[] buf, int count)
        {
            string str = String.Format("{0:X4} {1:X4}  ", Adr / 65536, Adr);
            for (int i = 0; i < 16; i++)
            {
                str += (i < count) ? String.Format("{0:X2}", buf[i]) : "  ";
                str += " ";
            }
            str += " ";
            // convert file to readable chars if possible
            for (int i = 0; i < 16; i++)
            {
                char ch = (i < count) ? Convert.ToChar(buf[i]) : ' ';
                str += Char.IsControl(ch) ? "." : ch.ToString();
            }
            return str;
        }
    }
}

thanx a lot it worked
but my file is too long its abt 2 mb
and it takes lot of time to decode it
is there any way of buffering or anything else

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.