I’m trying to do some conversion and would like to use Linq to achieve the following,
Decimal to Hex,
Decimal to Ascii,
Hex to Decimal,
Hex to Ascii
Can someone please show me how to do this efficently in Linq?
I’ll be displaying the output into textboxes.
Also, I have a prefix and delimiter field that will also need to be included,
Example:
string input = txtAscii.Text;
string delim = txtDelimiter.Text;
string prefix = txtPrefix.Text;
if (checkBox1.Checked == true && string.IsNullOrEmpty(delim)) delim = " ";
//Linq, Ascii to Decimal.
txtDecimal.Text = string.Join(delim, input.Select(c => prefix + ((int)c).ToString()));
Thanks all.
LINQ is for querying collections, not converting values. It is wrong to say you want to use LINQ to convert X to Y.
That said, here’s the building blocks you need:
Then to do something like converting between decimal form to hexadecimal form:
Your “ASCII to (hexa)decimal” conversions could be done with a little bit of LINQ using the above building blocks. Assuming you mean the (hexa)decimal representation of each character’s ASCII code: