Translate string to emoji in c# - c#

How to turn such a string into an emoji? 1F600 => πŸ˜€ or 1F600 => \U0001F600 or 1F600 => 0x1F600
I spent a few days but I still didn't understand how to translate a string like 1F600 into emoji

You simply need to convert the value to the code point then get the character at that code point:
var emoji = Char.ConvertFromUtf32(Convert.ToInt32("1F600", 16));
Demo on dotnetfiddle

The string "1F600" is the hexadecimal representation of a Unicode code point. As it is not in the BMP, you either need UTF32 or a UTF16 surrogate pair to represent it.
Here is some code to perform the requested conversion using UTF32 representation:
Parse as 32-bit integer:
var utf32Char = uint.Parse("1F600", NumberStyles.AllowHexSpecifier);
Convert this to a 4-element byte array in litte-endian byte order:
var utf32Bytes = BitConverter.GetBytes(utf32Char);
if (!BitConverter.IsLittleEndian)
Array.Reverse(utf32Bytes);
Finally, use Encoding.UTF32 to make a string from it.
var str = Encoding.UTF32.GetString(utf32Bytes);
Console.WriteLine(str);

Related

Passing a hex string in a textbox.text property to BitConverter.GetBytes as hex in C#

I am reading values from sensors and receiving two 16 bit Hex values back in string format "417D" and "8380" for example.
I cannot seem to find a way in C# to parse the strings to split them up into a byte array ( 41,7D,83,80 ) preserving the hex number.
I then use the following to convert the IEEE754 number to a decimal to get the correct sensor reading value.
txtFloatValue.Text = BitConverter.ToSingle(hex, 0).ToString();
The code below works, but I need to pass the values as a hex array, not 0x417D8380.
byte[] hex = BitConverter.GetBytes(0x417D8380);
txtFloatValue.Text = BitConverter.ToSingle(hex, 0).ToString();
Any advice would be much appreciated. I may be approaching this the wrong way, but the IEEE754 conversion works well.
You can parse your hex strings to ints and then use BitConverter. For example:
var i = int.Parse("417D8380", NumberStyles.AllowHexSpecifier);
var bytes = BitConverter.GetBytes(i);
var single = BitConverter.ToSingle(bytes, 0); // 15.8446

I have a string of octal escapes that I need to convert to Korean text - not sure how

I found a similar question:
Converting integers to UTF-8 (Korean)
But I can't figure out how you would do this in .net c#
Problem: I have a string from a database - "\354\202\254\354\232\251\354\236\220\354\203\201\354\204\270\354\240\225\353\263\264\354\236\205\353\240\245"
That should translate to - μ‚¬μš©μžμƒμ„Έμ •λ³΄μž…λ ₯
Any help would be greatly appreciated!
There are a number of steps involved in the conversion:
Extract the individual octal numbers (such as 354) from the source string.
Convert each octal string representation to its decimal equivalent as a byte.
Decode the byte sequence as UTF-8.
Here's a sample implementation:
string source = #"\354\202\254\354\232\251\354\236\220\354\203\201\354\204" +
#"\270\354\240\225\353\263\264\354\236\205\353\240\245";
byte[] bytes = source.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => (byte)Convert.ToInt32(s, 8))
.ToArray();
string result = Encoding.UTF8.GetString(bytes); // "μ‚¬μš©μžμƒμ„Έμ •λ³΄μž…λ ₯"

C# Convert Char to Byte (Hex representation)

This seems to be an easy problem but i can't figure out.
I need to convert this character < in byte(hex representation), but if i use
byte b = Convert.ToByte('<');
i get 60 (decimal representation) instead of 3c.
60 == 0x3C.
You already have your correct answer but you're looking at it in the wrong way.
0x is the hexadecimal prefix
3C is 3 x 16 + 12
You could use the BitConverter.ToString method to convert a byte array to hexadecimal string:
string hex = BitConverter.ToString(new byte[] { Convert.ToByte('<') });
or simply:
string hex = Convert.ToByte('<').ToString("x2");
char ch2 = 'Z';
Console.Write("{0:X} ", Convert.ToUInt32(ch2));
get 60 (decimal representation) instead of 3c.
No, you don't get any representation. You get a byte containing the value 60/3c in some internal representation. When you look at it, i.e., when you convert it to a string (explicitly with ToString() or implicitly), you get the decimal representation 60.
Thus, you have to make sure that you explicitly convert the byte to string, specifying the base you want. ToString("x"), for example will convert a number into a hexadecimal representation:
byte b = Convert.ToByte('<');
String hex = b.ToString("x");
You want to convert the numeric value to hex using ToString("x"):
string asHex = b.ToString("x");
However, be aware that you code to convert the "<" character to a byte will work for that particular character, but it won't work for non-ANSI characters (that won't fit in a byte).

How can i convert a string of characters into binary string and back again?

I need to convert a string into it's binary equivilent and keep it in a string. Then return it back into it's ASCII equivalent.
You can encode a string into a byte-wise representation by using an Encoding, e.g. UTF-8:
var str = "Out of cheese error";
var bytes = Encoding.UTF8.GetBytes(str);
To get back a .NET string object:
var strAgain = Encoding.UTF8.GetString(bytes);
// str == strAgain
You seem to want the representation as a series of '1' and '0' characters; I'm not sure why you do, but that's possible too:
var binStr = string.Join("", bytes.Select(b => Convert.ToString(b, 2)));
Encodings take an abstract string (in the sense that they're an opaque representation of a series of Unicode code points), and map them into a concrete series of bytes. The bytes are meaningless (again, because they're opaque) without the encoding. But, with the encoding, they can be turned back into a string.
You seem to be mixing up "ASCII" with strings; ASCII is simply an encoding that deals only with code-points up to 128. If you have a string containing an 'Γ©', for example, it has no ASCII representation, and so most definitely cannot be represented using a series of ASCII bytes, even though it can exist peacefully in a .NET string object.
See this article by Joel Spolsky for further reading.
You can use these functions for converting to binary and restore it back :
public static string BinaryToString(string data)
{
List<Byte> byteList = new List<Byte>();
for (int i = 0; i < data.Length; i += 8)
{
byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
}
return Encoding.ASCII.GetString(byteList.ToArray());
}
and for converting string to binary :
public static string StringToBinary(string data)
{
StringBuilder sb = new StringBuilder();
foreach (char c in data.ToCharArray())
{
sb.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
}
return sb.ToString();
}
Hope Helps You.
First convert the string into bytes, as described in my comment and in Cameron's answer; then iterate, convert each byte into an 8-digit binary number (possibly with Convert.ToString, padding appropriately), then concatenate. For the reverse direction, split by 8 characters, run through Convert.ToInt16, build up a byte array, then convert back to a string with GetString.

how to convert string of unicodes to the char?

i have a text file in which sets of Unicodes are written as
"'\u0641'","'\u064A','\u0649','\u0642','\u0625','\u0644','\u0627','\u0647','\u0631','\u062A','\u0643','\u0645','\u0639','\u0648','\u0623','\u0646','\u0636','\u0635','\u0633','\u0641','\u062D','\u0628','\u0650','\u064E','\u062C','\u0626"
"'\u0622'","'\u062E','\u0644','\u064A','\u0645".
I opened the file and started reading of file by using readline method. I got the above line shown as a line now i want to convert all Unicode to char so that i could get a readable string. i tried some logic but that doesn't worked i stuck with converting string "'\u00641'" to char.
You can extract strings containing individual numbers (using Regex for example), apply Int16.Parse to each and then convert it to a char.
string num = "0641"; // replace it with extracting logic of your preference
char c = (char)Int16.Parse(num, System.Globalization.NumberStyles.HexNumber);
You could parse the line to get each unicode char. To convert unicode to readable character you could do
char MyChar = '\u0058';
Hope this help
What if you do something like this:
string codePoints = "\u0641 \u064A \u0649 \u0642 \u0625";
UnicodeEncoding uEnc = new UnicodeEncoding();
byte[] bytesToWrite = uEnc.GetBytes(codePoints);
System.IO.File.WriteAllBytes(#"yadda.txt", bytesToWrite);
byte[] readBytes = System.IO.File.ReadAllBytes(#"yadda.txt");
string val = uEnc.GetString(readBytes);
//daniel

Categories

Resources