My program in a nutshell is reading a text file, outputting it to the console screen one character at a time, and then writing the characters as its read as hex pairs.
I'm calling this method in a loop: WriteByte((char)ch, ref writer); (passing in the character, and a reference to the output file, which is a StreamWriter)

Here is the method:

static void WriteByte(char w, ref StreamWriter writer)
    {
        
        writer.Write(w);
    }

I need to write the character to that output file as 2-digit hex, with a space in between each pair. How to I format the char to be a hex digit?

Recommended Answers

All 5 Replies

writer.Write(w.ToString("X2") + " ");

This works when writing to the console:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            char ch1 = 'A';
            Console.Write("{0:X} ",Convert.ToUInt32(ch1));
            char ch2 = 'Z';
            Console.Write("{0:X} ", Convert.ToUInt32(ch2));
            Console.ReadKey();
        }
    }
}

I need to create a method that counts words and lines using specific guidelines and I can't figure it out:

Update the count of words & lines in the file as each character is read
create a void method, CountIt(), to count the lines and words
Parameters: pass in the character by value
pass in a reference to the word count
pass in a reference to the line count
pass in a reference to the bool that tracks if you are "in a word"
Will need a bool variable to keep track WHEN you are IN A WORD or NOT

This is what I have:

bool inWord = true;
.
.
.
// calling the CountIt() method
CountIt(x, ref linecount, ref wordcount, ref inWord);
.
.
.
// the CountIt() method
private static void CountIt(string x, ref int linecount, ref int wordcount, ref bool inWord)
    {
    
    //count words and lines in here.    

    }

I have no clue how to go about this (what the bool should say, what should be in my method). I've been trying to teach myself through reading and other means but no luck.

OK, fine. But what has your assignment (which it obviously is) to do with a char to hex conversion?
If your problem about that is solved, mark it as such and start a new thread with your new questions.

OK, fine. But what has your assignment (which it obviously is) to do with a char to hex conversion?
If your problem about that is solved, mark it as such and start a new thread with your new questions.

Sorry about that, I started a new thread.

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.