Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i dont know how to convert the code in unicode in asp.net c# please help me asap. is c# code is unicode

What I have tried:

nothing to be tried yet im not aware of unicode so ill be confuse how to do that google would not be giving the full detail
Posted
Updated 29-Jan-20 23:13pm

You can convert string to UTF16 or Unicode

public static string GetSingleUnicodeHex(string strTextMsg)
{
     byte[] s1 = UTF8Encoding.Unicode.GetBytes(strTextMsg);
     string strUnicode = "";
     string strTmp1 = "";
     string strTmp2 = "";
     
     for (int i = 0; i < s1.Length; i += 2)
     {
        strTmp1 = int.Parse(s1[i + 1].ToString()).ToString("X");
        if (strTmp1.Length == 1)
              strTmp1 = "0" + strTmp1;

        strTmp2 = int.Parse(s1[i].ToString()).ToString("X");
        if (strTmp2.Length == 1)
              strTmp2 = "0" + strTmp2;

        strUnicode += strTmp1 + strTmp2;
     }
     return strUnicode;
}
 
Share this answer
 
.Net uses the UTF-16 encoding. You have to convert only when having data or passing data using other encodings.

See also Character Encoding in .NET | Microsoft Docs[^].

With text files, the used encoding can be indicated by a Byte order mark - Wikipedia[^] (mandatory for UTF-16, optional - but recommended - for UTF-8) and by headers when the file is for a specific protocol like HTML or XML.

For web servers and applications there are also often configuration options for the default encoding (e.g. the <globalization> Element | Microsoft Docs[^]).

[EDIT]
The real question has been found.
Quote:
i want to display the string data in unicode format like im having my name is "Shahbaz" is in string format and the convert it to the unicode is
U+0053 U+0068 U+0061 U+0068 U+0062 U+0061 U+007A
You just have to create a string using that format by iterating over the characters of the input string:
C#
// Using a StringBuilder is more efficient than appending to a string
StringBuilder sb = new StringBuilder(input.Length * 7);
// Iterate over the characters of the input string
foreach (char c in input)
{
    // Append the character code
    sb.AppendFormat("U+{0:X4} ", c);
}
// Create final output string
string output = sb.ToString();
[/EDIT]
 
Share this answer
 
v2
Comments
Eric Lynch 19-Jun-18 13:11pm    
To (hopefully) add a bit to Jochen's response, a C# string is always stored as UTF-16 within memory. There two common cases where you need to worry about the encoding: 1) you are reading or writing a file or 2) you are dealing with some non-.NET entity (via interop). For those cases, check out the suggested links.

Another concern can be with a user interface. There, you need to make certain you select a font that contains glyphs for the characters you expect to display. Though, this is a presentation issue, not an encoding issue. Usually, the default MS fonts are fairly decent for letters in most languages...less so for symbols.
Shahbaz435 20-Jun-18 1:27am    
but my question is how to convert the string into utf 16 can u provide me the code in c#
Jochen Arndt 20-Jun-18 2:45am    
If you have a C# string, it is already UTF-16 encoded because that is the encoding used internally by .Net.
Shahbaz435 20-Jun-18 2:48am    
but i need the string should be u0012 like this format
Jochen Arndt 20-Jun-18 3:15am    
A computer stores information binary. How data are interpreted is defined by data formats.

With a C# string, characters are stored as 16 bit ushort values according to the UTF-16 standard and the common output format is printing the corresponding characters.

If you want a different output or input format, you have to use appropriate functions or methods.

The 'uXXXX' notation is nothing else than the letter 'u' followed by a 16-bit hex value representing a single character. Other notations are the corresponding numerical values 0x0012 (hex) and 18 (decimal).

If you need to assing a character like 'u0012' to a string, you have to use a method supported by the compiler like
string test = "\x12";

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