Click here to Skip to main content
16,022,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i am migrating a method of c++ to c#,I some guide line for it c++ is using WriteConsole
for printing the specific text "#xxxxx" .

what shuold i need to use for WriteConsole of c++ in c# any example code to use. >

i am not able figure out the basic difference between printf ,WriteConsole and Console.WriteLine?

What I have tried:

 static void Main(string[] args)
        {
            string str2 = "#T255001010";
        
            Console.WriteLine("Step 1");
            Console.Write(str2);
            Console.WriteLine();
//step 2
           Console.OutputEncoding = Encoding.Default;
            ASCIIEncoding ascii = new ASCIIEncoding();
            byte[] encodedBytes = ascii.GetBytes(str2);
            Console.OutputEncoding = Encoding.ASCII;
            foreach (byte b in encodedBytes)
            Console.Write(b);
            Console.WriteLine();


//step 3
 Console.OutputEncoding = Encoding.Default;
            ASCIIEncoding ascii1 = new ASCIIEncoding();
            Console.Write(str2);
Posted
Updated 8-Jun-17 21:23pm
Comments
[no name] 9-Jun-17 4:41am    
What exactly is the problem? Perhaps if you show the C++ code that you are trying to convert it would make more sense.

1 solution

printf uses a formatting string to output directly to the console: anything in the string prefaced with '%' indicates a "placeholder" which will be replaced with a value from the list of variables that follows the format string:
C++
printf("The value is %d!", i);
Will print "The value is 666!" or similar depending on the value in i when the statement is executed.
Console.Write and Console.WriteLine work much the same under normal circumstances: you supply a format string and a list of parameters and they are replaced:
C#
Console.WriteLine("The value is {0}!", i);
Will print the same as the printf example (there are other ways to print parameters with Write and WriteLine, but I'll ignore those here).
Unless there is just one parameter, in which case it it printed exactly as it is:
Console.WriteLine("The value is {0}!);
Will print "The value is {0}!"
When you specify an non printable value as the parameter to Write or WriteLine though, things are different. An int will print a string representation of the number: "666", as will a double: "666.66", and a byte is also not printable, so you get the numeric value or the byte: "#" will print as "35" because that is the ASCII representation of a '#' character, 'T' as a "84", '2' as "50", '5' as "53", and so on.
See here: ASCII Table[^] and the values should be clearer.
 
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