I have this string that was UTF8 encoded from PHP.
Encoded value : tr\u008fs
How do I get the original value back using C#?
The original value should be très.
Try this:
Encoding encoding = new UTF8Encoding();
string s = "tr\u008fs";
string value = encoding.GetString(encoding.GetBytes(s));
Related
I need to convert some arabic text to utf-8 and convert it to Hexa I made some codes but it turns the output to like what in next image.
Codes I trid :
string myName = _name.Text;
string myNameLength = _name.TextLength.ToString("X2");
byte[] nameByte = Encoding.Default.GetBytes(myName);
var hexStringName = BitConverter.ToString(nameByte);
hexStringCo = hexStringCo.Replace("-", "");
Picture
Getting the utf8 bytes is:
string name = "عبود";
byte[] utf8 = Encoding.UTF8.GetBytes(name);
var hex = BitConverter.ToString(utf8);
hex = hex.Replace("-", "");
Console.WriteLine(hex); // D8B9D8A8D988D8AF
What you do with those is up to you; there's zero chance that a hex string was rendered with the replacement character (aka �), so: you're doing something else that we can't see. Maybe show us what you're doing with the value once you have it.
One of my DB query from a legacy app is returning string in this format "0.5°,0.6°, 0.7°,0.8°, 0.9° and 1.0°" which is not correct. I am expecting the strings to be in format.
"0.5°, 0.75°, 1.0°, 1.5°, 1.75°, and 2.0".
I tried to convert the windows 1252 to utf-8 by doing the following:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding w1252 = Encoding.GetEncoding(1252);
byte[] w125bytes = w1252.GetBytes(htmlNormalized);
byte[] utfbytes = Encoding.Convert(w1252, utf8Encoding, w125bytes);
htmlNormalized = Encoding.UTF8.GetString(utfbytes);
but I am still getting the same string with special characters. what am i doing wrong? and how can I fix it?
An ASP.NET page (ashx) receives a GET request with a UTF8 string. It reads a SqlServer database with Windows-1255 data.
I can't seem to get them to work together. I've used information gathered on SO (mainly Convert a string's character encoding from windows-1252 to utf-8) as well as msdn on the subject.
When I run anything through the functions below - it always ends up the same as it started - not converted at all.
Is something done wrong?
EDIT
What I'm specifically trying to do (getData returns a Dictionary<int, string>):
getData().Where(a => a.Value.Contains(context.Request.QueryString["q"]))
Result is empty, unless I send a "neutral" character such as "'" or ",".
CODE
string windows1255FromUTF8(string p)
{
Encoding win = Encoding.GetEncoding(1255);
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(p);
byte[] winBytes = Encoding.Convert(utf8, win, utfBytes);
return win.GetString(winBytes);
}
string UTF8FromWindows1255(string p)
{
Encoding win = Encoding.GetEncoding(1255);
Encoding utf8 = Encoding.UTF8;
byte[] winBytes = win.GetBytes(p);
byte[] utfBytes = Encoding.Convert(win, utf8, winBytes);
return utf8.GetString(utfBytes);
}
There is nothing wrong with the functions, they are simply useless.
What the functions do is to encode the strings into bytes, convert the data from one encoding to another, then decode the bytes back to a string. Unless the string contains a character that is not possible to encode using the windows-1255 encoding, the returned value should be identical to the input.
Strings in .NET doesn't have an encoding. If you get a string from a source where the text was encoded using for example UTF-8, once it's decoded into a string it doesn't have that encoding any more. You don't have to do anyting to a string to use it when the destination has a specific encoding, whatever library you are using that takes the string will take care of the encoding.
For some reason this worked:
byte[] fromBytes = (fromEncoding.UTF8).GetBytes(myString);
string finalString = (Encoding.GetEncoding(1255)).GetString(fromBytes);
Switching encoding without the conversion...
I have a string (C# code) that looks as follows:
string s = "Indsætning";
It is encoded in some way that I am not sure of.
I'd like to decode it so that I get the following string:
Indsætning
I have tried with
string s1 = HttpUtility.UrlDecode(s);
string s2 = HttpUtility.HtmlDecode(s);
However, I don't get the string that I am looking for.
Any help is appreciated.
Convert the string to bytes and then using the Encoding class to get the UTF-8 string representation.
string s = "Indsætning";
byte[] sBytes = s.Select(x => (byte)x).ToArray();
string decoded = Encoding.UTF8.GetString(sBytes);
Edit - As mentioned in the comments, this assumes that the string being converted is of a particular encoding (Latin-1 in this case). Therefore, it won't necessarily work for all strings, unless you know that they've all been encoded into the same format.
I'm trying to encode some strings back and forth from base-64 string and I'm having truble to get the right result.
string text = base64string.... //Here I have a base-64 string.
byte[] encodedByte = System.Text.ASCIIEncoding.ASCII.GetBytes(text);
string base64Encoded = Convert.ToBase64String(encodedByte);
if (text == base64Encoded) //If the new encoded string is equal to its original value
return base64Encoded;
I have tried my ways to do this and I don't seem to get the right result. I have tried both with System.Text.Encoding.Unicode and System.Text.Encoding.UTF8
What could be the problem? Does anyone have a proper solution?
string text = base64string.... //Here I have a base-64 string.
byte[] encodedByte = System.Text.ASCIIEncoding.ASCII.GetBytes(text);
string base64Encoded = Convert.ToBase64String(encodedByte);
You are double encoding the string. You begin with a base64 string, get the bytes, and then encode it again. If you want to compare you will need to begin with the original string.
If text is a base-64 string, then you are doing it backwards:
byte[] raw = Convert.FromBase64String(text); // unpack the base-64 to a blob
string s = Encoding.UTF8.GetString(raw); // assume the blob is UTF-8, and
// decode to a string
which will get you it as a string. Note, though, that this scenario is only useful for representing unicode text in an ascii format. Normally you wouldn't base-64 encode it if the original contents are string.
Convert whatever it is that you need in Base64 into a Byte array then use the FromBase64String and ToBase64String to convert to and from Base64:
Byte[] buffer = Convert.FromBase64String(myBase64String1);
myBase64String2 = Convert.ToBase64String(buffer);
myBase64String1 will be equal to myBase64String2. You will need to use other methods to get your data type into a Byte array and the reverse to get your data type back. I have used this to convert the content of a class into a byte array and then to Base64 string and write the string to the filesystem. Later I read it back into a class instance by reversing the process.
You have the encoding code correctly laid out. To confirm whether the base64-encoded string is correct, you can try decoding it and comparing the decoded contents to the original:
var decodedBytes = Convert.FromBase64String(base64encoded);
var compareText = System.Text.Encoding.ASCII.GetString(decodedText);
if (text == compareText)
{
// carry on...
return base64encoded;
}