Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get the string data with special characters like below.

Á,õn O'kim José Nuñez    
JOSNUÑ44

but the special characters are missing while get back by using the below logic.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpecialCharacters
{
    class Program
    {
        static void Main(string[] args)
        {
           
            string specialcharactersdata = @"Á,õn O'kim José Nuñez JOSNUÑ44  ";
            Console.WriteLine(specialcharactersdata);


            using (MemoryStream ms = new MemoryStream(System.Text.ASCIIEncoding.UTF8.GetBytes(specialcharactersdata)))
            {
                StreamWriter sw = new StreamWriter(ms);
                sw.Flush();
                ms.Position = 0;
                StreamReader sr = new StreamReader(ms);
                string myStr = sr.ReadToEnd();
              
                Console.WriteLine(myStr);
                Console.Read();
            }


        }
    }
}



Could you please help me how to get exact text or string with special characters.
Á,õn O'kim José Nuñez JOSNUÑ44


What I have tried:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpecialCharacters
{
    class Program
    {
        static void Main(string[] args)
        {
           
            string specialcharactersdata = @"Á,õn O'kim José Nuñez JOSNUÑ44  ";
            Console.WriteLine(specialcharactersdata);


            using (MemoryStream ms = new MemoryStream(System.Text.ASCIIEncoding.UTF8.GetBytes(specialcharactersdata)))
            {
                StreamWriter sw = new StreamWriter(ms);
                sw.Flush();
                ms.Position = 0;
                StreamReader sr = new StreamReader(ms);
                string myStr = sr.ReadToEnd();
              
                Console.WriteLine(myStr);
                Console.Read();
            }


        }
    }
}

getting out put like below:
A,on O'kim José Nuñez JOSNUÑ44
A,on O'kim José Nuñez JOSNUÑ44



But i want out put like below:
Á,õn O'kim José Nuñez    
JOSNUÑ44
Posted
Updated 24-Jan-19 2:23am
v2

By default, the console doesn't support UTF8 / Unicode characters; it only supports characters in the current code-page.

Depending on your OS, you might be able to fix it by setting the Console.OutputEncoding property[^] to System.Text.Encoding.UTF8 before writing the values. For some characters, you might also need to install extra fonts, and change the console font.

c# - How to write Unicode characters to the console? - Stack Overflow[^]
 
Share this answer
 
Comments
CPallini 24-Jan-19 8:26am    
5.
DGKumar 24-Jan-19 9:46am    
Hi Richerd,
1. I have moved to this code to MVC4 application and try to add Header to the below code
TransferUtilityUploadRequest uploadRequest = new TransferUtilityUploadRequest();
uploadRequest.BucketName = sourceBucketName.ConfigValue.ToString();
uploadRequest.Key = fileFullName;

Stream tmpStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(downloadData));
tmpStream.Seek(0, SeekOrigin.Begin);
StreamReader sReaderData = new StreamReader(tmpStream);

Need to Add this header this uploadRequest.Headers.ContentEncoding = Encoding.Unicode;
but getting excecption
If i add this header then the data should display in my excel file.
That's because none of those character exist in the ASCII encoding.

Try using UTF8 instead!

-------------

Ok - still confused. I've tried your code, and the text gets preserved just fine. The special characters are not going to be an issue unless you go back to something like ASCII. I've rewritten it slightly:

string specialcharactersdata = @"Á,õn O'kim José Nuñez JOSNUÑ44  ";
Console.WriteLine(specialcharactersdata);
using (MemoryStream ms = new MemoryStream())
{
    StreamWriter sw = new StreamWriter(ms);
    sw.WriteLine(specialcharactersdata);
    sw.Flush();
    ms.Position = 0;
    StreamReader sr = new StreamReader(ms);
    string myStr = sr.ReadToEnd();
    Console.WriteLine(myStr);
    Console.Read();
}


Which produces the output I'd expect, which seems to align with what is required in your latest comment but not with the original question.
 
Share this answer
 
v2
Comments
DGKumar 23-Jan-19 8:04am    
I have used the below code
using (MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(specialcharactersdata)))
but this code text is not displaying special characters
Á,õn O'kim José Nuñez
Rob Philpott 23-Jan-19 8:25am    
Sorry, I think I misread your post. But I don't understand what you trying to do. You are trying to convert "Á,õn O'kim José Nuñez" to "JOSNUÑ44"? I don't see the relationship between the two.
DGKumar 23-Jan-19 8:44am    
Á,õn O'kim José Nuñez JOSNUÑ44 this is complete string
Rob Philpott 23-Jan-19 8:17am    
Give me a moment, I'll try it...
DGKumar 23-Jan-19 8:43am    
I have a string like below
Á,õn O'kim José Nuñez

I need to pass the data by using Memory stream as below
using (MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(specialcharactersdata)))

if i use this i am getting missed characters string
like below

A,on O'kim but actual string is Á,õn O'kim which is modified after using the code

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