Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want read from the file and write line by line it in another file.
of course first file consist of string code with length 20 and another file is hex code and I convert string to hex with this code:
C#
string hex = "";
                   foreach (char c in sline)
                   {
                       int tmp = c;
                       hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
                   }

sline is string code in first file.
how can do this?
Posted
Updated 10-Jul-12 3:46am
v3
Comments
[no name] 10-Jul-12 9:57am    
Open first file, read the line, do whatever you want to do with the input, write to second file. Do you have a specific question about something?

Here is sample code. It may be good example of what you need to do.
That code is not tested!
C#
void Foo(string inputFilePath, string outputFilePath)
        {

            try
            {
                if (File.Exists(inputFilePath))
                {
                    //open output stream
                    using (BinaryWriter bw = new BinaryWriter(File.Create(inputFilePath)))
                    {
                        //open input stream 
                        //!!! specify Encoding !!!
                        using (BinaryReader br = new BinaryReader(File.Open(inputFilePath, FileMode.Open, Encoding.ASCII)))
                        {

                            // Position and length variables.
                            int pos = 0;

                            // Use BaseStream.
                            int length = (int)br.BaseStream.Length;
                            while (pos < length)
                            {
                                var endOfLine = "\r\n"; // \n Or \r

                                // Read char and write char to output file
                                bw.Write(String.Format("0x{0:X}{1}", (int)br.ReadChar()), endOfLine);



                                // Advance our position variable.
                                pos += sizeof(int);
                            }
                        }
                    }
                }
                else
                {
                    throw new FileNotFoundException("Input file not found!",inputFilePath);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }

        }


;)
 
Share this answer
 
v3
C#
public void ConvertToHex(string inputFilePath, string outputFilePath)
        {
            using (StreamWriter writer = new StreamWriter(outputFilePath))
            {
                using (StreamReader reader = new StreamReader(inputFilePath))
                {
                    string text = reader.ReadToEnd();
                    foreach (char value in text)
                    {
                        writer.Write(string.Format("{0:x2}", (int)value));
                    }
                }
            }
        }
 
Share this answer
 
private string HexAsciiConvert(string hex)

{

StringBuilder sb = new StringBuilder();

for (int i = 0; i <= hex.Length - 2; i += 2)

{

sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hex.Substring(i, 2),

System.Globalization.NumberStyles.HexNumber))));

}

return sb.ToString();

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900