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

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());

Related

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);

c# remove trailing 0 for INR currency conversion?

I use following code to convert input to comma separated string in INR:
decimal input = 1111111111.59m;
string result = input.ToString("C", new CultureInfo("EN-in"));
I want to remove the trailing 0s now, how do i do this?
for example:
decimal input = 1111111111.00m;
Output should be 1111111111
string result = input.ToString("c0", new CultureInfo("EN-in"));
Update:
So you want output "123.45" for input 123.45 and output "123" for input 123.00.
You can't achieve these 2 different formats without conditional operator, String.Format() will produce only one output format for you.
The code is simple though:
string format = Decimal.Round(input) == input ? "c0" : "c";
string output = input.ToString(format);
string output = input.ToString("0");
Following code should work :
string results = input.ToString("0.##");
The simplest thing to convert is convert into int.
int d = convert.toInt32(1111.00);
or use any math function as suggested.
How to remove decimal part from a number in C#
How do I format a C# decimal to remove extra following 0's?
Edit
As I understand just try
Console.WriteLine(d.ToString("0.#####"));
Seet this url :- Best way to display decimal without trailing zeroes

Converting hex to decimal

I have an array of strings that are in hex format, for example {"3d", "20", "5a"}.
What would be a good way of converting each element of this string to decimal format?
I've tried using GetBytes(), but that doesn't seem to work since it sees "3d" as two different characters because it doesn't know that it is in hex format.
GetBytes() works fine in a situation like below but not if characters are in hex.
What am I missing here?
string a = "T";
byte[] b = {10};
b = System.Text.UTF8Encoding.Default.GetBytes(a);
Use int.Parse with NumberStyles.HexNumber.
int decValue = int.Parse(hexValue,System.Globalization.NumberStyles.HexNumber);
Try that.
HexValue is the number in hex format and decValue is in decimal...

how to convert string to integer in RDLC

I want to convert string number like "000023" to integer 000023 in RDLC report but when I am trying to convert it into integer it only displays 23 instead of 000023.
does anyone know how to do this ?
thanks
000023 is not an valid integer. When 000023 is converted to an integer, it will automatically be converted to the valid integer 23.
Jaq316's answer is correct.
How could you have an int as 000023? You could format it as a string with leading zeros which you have already it as a string.
But if you want to display 23 as a 000023 you can use String.PadLeft like;
public string Represent(string s)
{
int i = 0;
if (int.TryParse(s, out i))
{
return(i.ToString().PadLeft(6,'0'));
}
}
This method seems pointless of course since you have already 000023 in your database.
Integral types and Floating point types doesn't owns any formatting.
Formatting is done only when converting them to string. so you've to format it when you display it. There is no way to store an integer as 000023

Converting from arabic string to hex html string in 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.

Categories