Encoding.ASCII.GetBytes it convert "∞" to "?" - c#

I want infinity Symbol i my string. i used following Code to get infinity symbol
char.ConvertFromUtf32(8734)
and it convert to json when json is encoded ie.
Encoding.ASCII.GetBytes(json)
then it convert "∞" to "?" symbol
so how i can resolve this problem. please Help me.
thanks.

The infinity sign ∞ is not part of the ASCII character set. So by using Encoding.ASCII.GetBytes() you explicitly exclude it from the string, effectivly replacing it with a placeholder, in this case ?
Since you use the resulting byte array for a JSON reply, you might want to consider using UTF8 inxtead of ASCII

Related

How to remove char 8236

I'm trying to parse some phone numbers, and I have a function to check if the parsed string is made up of only numbers or the + sign.
In some of them there is an hiden character of value 8236.
Comparing it against '\0' and '\u8236' doesnt work...
What is this character and how do I remove it?
Thanks to #Maximilian Gerhardt who sent this link in a comment https://www.fileformat.info/info/unicode/char/202c/index.htm
I was able to know that 8236 corresponds to character '\u202c'
So I did str.Trim('\u202c')
And it did work
edit:
The simple way to get the corresponding code is to convert from decimal to hex.
8236(decimal) -> 202C(hexadecimal)
I had the same issue, but with character 8237, which led me to this post.
This corresponds with character \u202d.

Convert UTF-8 hexadecimal to regular character

Does anyone know how to convert a string in C# that has UTF-8 hexadecimal characters in it to a regular characters.?
For example
hell%c3%b3 to hello
Chart
UTF-8 ASCII TECKEN Flattened
%c3%b3 %f3 ò o
There are many UTF-8 hexadecimals I need to convert is there a way to do this with a built in method in .NET?
This is called URL encoding and can be undone with
using System.Web;
HttpUtility.UrlDecode("hell%c3%b3");
This gives helló, but probably that's what you wanted.
The second part, removing the diacritics, is not so simple, see How do I remove diacritics here on SO.

How to decode a string like "string\x27s" in c#?

I have a c# app that sometimes has to work with strings like:
"example\x27s string"
How do I decode that? I know 27 is the ascii code for a single quote ', but UrlDecode() wont work on that string.
Should I replace the \x value with % and then use System.Web.HttpUtility.UrlDecode() or is there another way to do it?
\x27 is not an HTML encoded value. This is a string Escape character. The truth behind it though is that in the actual string is probably a physical \ character so what you're dealing with is:
"\\x27"
Or
#"\x27"
I am unsure if .NET has a way to re-evaluate a string, but the \codes for strings are handled on a compiler level if i remember correctly.
You could use regular expressions to do a replacement if you want, since you know what it represents.

How do I escape strings which contain byte data in C#?

How do I escape strings in C#? I have strings which are the bytes from a PNG and I need to escape them correctly in code to avoid compile errors...Any ideas?
So here is the type of code, I have which doesn't compile
public const string s =
"wewegliwewejwqejsadaskjda" +
"wewegliwewejqejsadaskjda" +
"wewegliwewejejsadaskjda" ;
The code you've given will compile, except that you've used constant instead of const. (Admittedly that's not valid Base64, as it contains the padding = character in the middle rather than at the end.) If this doesn't help, please post an example with this problem corrected but that still doesn't compile.
As Neil said, none of the characters in Base64 need escaping in C#.
Do you mean a Base 64 string? Usually if you see a PNG in string form, its base 64.
If not base 64, what type of encoding? Can you give us an example of what these strings look like?
EDIT:
To convert a Base64 string to a byte array (from which you can either save it as a PNG file or open it as an Image object) do this:
byte[] filebytes = Convert.FromBase64String(yourBase64String);
\ is the escape character for C#.

Base64 String throwing invalid character error

I keep getting a Base64 invalid character error even though I shouldn't.
The program takes an XML file and exports it to a document. If the user wants, it will compress the file as well. The compression works fine and returns a Base64 String which is encoded into UTF-8 and written to a file.
When its time to reload the document into the program I have to check whether its compressed or not, the code is simply:
byte[] gzBuffer = System.Convert.FromBase64String(text);
return "1F-8B-08" == BitConverter.ToString(new List<Byte>(gzBuffer).GetRange(4, 3).ToArray());
It checks the beginning of the string to see if it has GZips code in it.
Now the thing is, all my tests work. I take a string, compress it, decompress it, and compare it to the original. The problem is when I get the string returned from an ADO Recordset. The string is exactly what was written to the file (with the addition of a "\0" at the end, but I don't think that even does anything, even trimmed off it still throws). I even copy and pasted the entire string into a test method and compress/decompress that. Works fine.
The tests will pass but the code will fail using the exact same string? The only difference is instead of just declaring a regular string and passing it in I'm getting one returned from a recordset.
Any ideas on what am I doing wrong?
You say
The string is exactly what was written
to the file (with the addition of a
"\0" at the end, but I don't think
that even does anything).
In fact, it does do something (it causes your code to throw a FormatException:"Invalid character in a Base-64 string") because the Convert.FromBase64String does not consider "\0" to be a valid Base64 character.
byte[] data1 = Convert.FromBase64String("AAAA\0"); // Throws exception
byte[] data2 = Convert.FromBase64String("AAAA"); // Works
Solution: Get rid of the zero termination. (Maybe call .Trim("\0"))
Notes:
The MSDN docs for Convert.FromBase64String say it will throw a FormatException when
The length of s, ignoring white space
characters, is not zero or a multiple
of 4.
-or-
The format of s is invalid. s contains a non-base 64 character, more
than two padding characters, or a
non-white space character among the
padding characters.
and that
The base 64 digits in ascending order
from zero are the uppercase characters
'A' to 'Z', lowercase characters 'a'
to 'z', numerals '0' to '9', and the
symbols '+' and '/'.
Whether null char is allowed or not really depends on base64 codec in question.
Given vagueness of Base64 standard (there is no authoritative exact specification), many implementations would just ignore it as white space. And then others can flag it as a problem. And buggiest ones wouldn't notice and would happily try decoding it... :-/
But it sounds c# implementation does not like it (which is one valid approach) so if removing it helps, that should be done.
One minor additional comment: UTF-8 is not a requirement, ISO-8859-x aka Latin-x, and 7-bit Ascii would work as well. This because Base64 was specifically designed to only use 7-bit subset which works with all 7-bit ascii compatible encodings.
string stringToDecrypt = HttpContext.Current.Request.QueryString.ToString()
//change to
string stringToDecrypt = HttpUtility.UrlDecode(HttpContext.Current.Request.QueryString.ToString())
If removing \0 from the end of string is impossible, you can add your own character for each string you encode, and remove it on decode.
One gotcha to do with converting Base64 from a string is that some conversion functions use the preceding "data:image/jpg;base64," and others only accept the actual data.

Categories

Resources