Converting from arabic string to hex html string in C# - c#

I need to convert arabic characters to its hex code as in character map, for example
in windows 7 character map with font "Arabic Typesetting" and character set "DOS:Arabic" when choose char 'ب' it gives U+0628 (0xA0) I need to get this with C#(in more details mvc3 razor view)

To achieve this you need to take each character, get its integral value, then format it as a string using the hexadecimal format specifier.
For example:
string name = "أحمد";
foreach (char c in name)
{
int value = (int)c;
string hex = value.ToString("X4");
Console.WriteLine("{0} : {1}", hex, c);
}
You might also find this helpful: How to: Convert Between Hexadecimal Strings and Numeric Types.

Related

How to convert a number (larger than 0xFFFF) that represents a Unicode character to its equivalent string in C#

For example, a character '𠀀' in CJK Unified Ideographs Extension A; its unicode value is 0x20000, as a char in C# can't represent such character, so I wonder if I could convert it to string, my question is:
If I give you a number like 0x20000, how to convert it and let me get its equivalent string like "𠀀"
You can use char.ConvertFromUtf32 for that:
int utf32 = 0x20000;
string text = char.ConvertFromUtf32(utf32);
string itself is a sequence of UTF-16 code units, in this case U+D840 and U+DC00, which you can see by printing out the individual char values:
int utf32 = 0x20000;
string text = char.ConvertFromUtf32(utf32);
Console.WriteLine(((int) text[0]).ToString("x4")); // d840
Console.WriteLine(((int) text[1]).ToString("x4")); // dc00

Is there a different way to convert to hexadecimal?

I'm trying to write some information to a special device that requires me to encode the string and I quote " an even number of bytes to write (1-32, base 10) "
The example string provided "DE AD BE EF CA FE" (works).
I have converted my string to decimal and from decimal to hexadecimal.
string TextToConvert = "Test Andrei";
TextToConvert=ConvertStringToHex(TextToConvert, Encoding.UTF8);
List<char> Chars = TextToConvert.ToCharArray().ToList();
string CharValue = "";
string secondHexConvert = "";
foreach(char c in Chars)
{
CharValue+=Convert.ToInt32(c);
secondHexConvert+=Convert.ToString(c, 16)+" ";
}
string hexValue = String.Format("{0:X}", CharValue)+" ";
I have found on internet a tool that converts to hexadecimal that works. The problem is that I can't figure what type of encoding is that. The site is this: https://codebeautify.org/decimal-hex-converter
from decimal "841011151163265110100114101105" to hex = "a9d741e82c990000000000000"
To convert such a big integer to a hexadecimal string, use the aptly named BigInteger type:
var num = BigInteger.Parse("841011151163265110100114101105");
string hex = num.ToString("X");
Console.WriteLine(hex);
will output:
0A9D741E82C98FC6A137B75371
but here's a snag, the output you showed in your question is somewhat different, let me show it together with what the code above produces:
0A9D741E82C98FC6A137B75371
a9d741e82c990000000000000
As you can see, the numbers start the same but your example then ends up with lots of zeroes.
The only way I understand this could happen is that they're in fact not using a type that can hold that many significant digits, so you get a rounding error.
Many of the dynamic programming languages allows you to use floating point numbers and integers interchangeably, I guess this is what happened, a floating point type that can only hold 17-18 significant digits or some such was used, and you lost precision. .NET, however, doesn't have built-in support for converting floating point types to hexadecimal.
You can see that .NET produces the exact value by converting back:
Console.WriteLine(BigInteger.Parse(hex, System.Globalization.NumberStyles.HexNumber));
outputs:
841011151163265110100114101105
In other words, I'm not sure you can get the exact same results in .NET.
Corollary: Don't use that site for this kind of conversion!
You can use the following code to convert a string to hexadecimal:
public static string ConvertStringToHex(String input, System.Text.Encoding encoding)
{
Byte[] stringBytes = encoding.GetBytes(input);
StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);
foreach (byte b in stringBytes)
{
sbBytes.AppendFormat("{0:X2}", b);
}
return sbBytes.ToString();
}
And you just call it using:
string testString = "11111111";
string hex = ConvertStringToHex(testString, System.Text.Encoding.Unicode);

ASP - TextBox accept only characters [UniCode] - C#

I have a TextBox where introduce Text(Three names). I want this textbox accept only characters in UNICODE format!
This is my (wrong)code for ASCII format:
if (!Regex.IsMatch(txtThreeNames.Text, "[a-zA-Z]^"))
{
return "Three names, must be a string !";
}
Try this:
public static bool IsASCII(this string value)
{
// ASCII encoding replaces non-ascii with question marks, so we use UTF8 to see if multi-byte sequences are there
return Encoding.UTF8.GetByteCount(value) == value.Length;
}
source: http://snipplr.com/view/35806/

How to convert Decimal to ASCII in c#.net?

Can anyone help me on how to convert decimal to ASCII using C#.net?
When I input a decimal into the textbox1, after clicking the CONVERT button then the result will display in textbox2. My problem is the code on how to convert decimal to ASCII. How to do this?
Here is a simple solution I found on the net. See if it works for you. 65 being the ASCII character.
char c = Convert.ToChar(65);
string d = c.ToString();
Source: http://forums.asp.net/t/1827433.aspx?Converting+decimal+value+to+equivalent+ASCII+character+in+c+
Another source: Decimal to ASCII Convertion
Is ASCII a requirement? Normally UTF-8 should be used when sending a string to another app.
var utf8 = Encoding.Utf8.GetBytes(myDecimal.ToString());
However, if you just want to convert a decimal to a string do
var s = myDecimal.ToString();
For example you have a Textbox and a Button
Textbox name is txtInput.
//Converting Decimal to ASCII
MessageBox.Show(Convert.ToChar(Convert.ToByte( txtInput.Text )).ToString());
//Convertşng ASCII to Decimal
MessageBox.Show(Convert.ToByte(Convert.ToChar( txtInput.Text )).ToString());

Trying to convert first symbol of string to int, getting weird value

static void Main(string[] args)
{
string str_val = "8584348,894";
//int prefix = Convert.ToInt32(str_val[0]); //prefix = 56 O_o
//int prefix = (int)str_val[0]; //what, again 56? i need 8!
int prefix = Convert.ToInt32("8"); //at least this works -_-
}
Any idea how to convert first symbol to right numeric value?
If you use:
Convert.ToInt32(str_val[0]);
then you are actually calling the overload:
Convert.ToInt32(char val);
which gives the Unicode/Ascii number of character being passed as a parameter.
If you want to convert first character, you need to force it to be a string type:
Convert.ToInt32(str_val.Substring(0, 1));
This way you call the overload:
Convert.ToInt32(string val);
which actually do what you want (convert string value to int value that this string represents).
Your trying to parse a string but passing in a char. Convert the character to a string first.
int prefix = Convert.ToInt32(str_val[0].ToString());
So the character value of 8 is the ASCII value 56, what you want to do is inteprete the value as a string rather than an ASCII Value. By using the .ToString() method you are converting the character into a null terminated string, which can be read by the ToInt32 method.
By doing this way Convert.ToInt32(str_val[0]) you are reading the character at a index of the string that converts the char to int. the int is the ascii for it
Like Bridge mentioned, it's getting the ASCII value. When you use the indexer on the string class to get just a character, it returns it as a char. If you read the char documentation you'll see that it is internally stored as a numeric UTF-16 value. It also has implicit conversions to and from most numeric types, which extract the UTF-16 value, or convert the numeric form into the character form. That's what you're doing.
What you mean to do is parse it as an int, not get the numeric representation of the UTF-16 value. That's where the Convert answers all come in.
Int32.Parse(str_val[0])
Will give you number in string.

Categories