c# decimal to hex & hex to decimal / hex to ascii in LINQ

11,279

Solution 1

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:

// string in decimal form to int
Int32.Parse("12345");

// string in hexadecimal form to int
Int32.Parse("ABCDE", NumberStyles.HexNumber);

// int to string in decimal form
12345.ToString();

// int to string in hexadecimal form
12345.ToString("x");

Then to do something like converting between decimal form to hexadecimal form:

var inDecimal = "12345";
var asInt = Int32.Parse(inDecimal);
var asHex = asInt.ToString("x");

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:

var str = "FOOBAR!";
var asAsciiInt = String.Join(" ", str.Select(c => (int)c));
var asAsciiHex = String.Join(" ", str.Select(c => ((int)c).ToString("x2")));
// asAsciiInt == "70 79 79 66 65 82 33"
// asAsciiHex == "46 4f 4f 42 41 52 21"

var asciiInt = "70 79 79 66 65 82 33";
var charStrs = asciiInt.Split();
var asStr = String.Concat(charStrs.Select(cs => (char)Int32.Parse(cs)));
// asStr == "FOOBAR!"

var asciiHex = "46 4f 4f 42 41 52 21";
var charStrs = asciiHex.Split();
var asStr = String.Concat(charStrs.Select(cs => (char)Int32.Parse(cs, NumberStyles.HexNumber)));
// asStr == "FOOBAR!"

Solution 2

I don't really understand why you'd need to use LINQ here, but you can do the conversion between decimal and hex like this:

decimal dec1 = 182;

//convert decimal as a hex in a string variable
string hex = dec.ToString("X");

//convert the hex string back to the decimal
decimal dec2 = decimal.Parse(hex, System.Globalization.NumberStyles.HexNumber);

Solution 3

To convert numeric value to hex is dead easy.

Decimal d = 10M;
d.ToString("X")

I am however utterly confused by what you mean "decimal to ascii"

Share:
11,279
Clu
Author by

Clu

Updated on June 05, 2022

Comments

  • Clu
    Clu almost 2 years

    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.

  • Jeff Mercado
    Jeff Mercado over 12 years
    I don't think he meant the literal, decimal type, but instead, a decimal representation of a number.
  • Clu
    Clu over 12 years
    Jeff, thats exactly what I meant, thank you for the clarification.
  • Clu
    Clu over 12 years
    Thank you for the information on Linq and for the examples, much appreciated.
  • Jeff Mercado
    Jeff Mercado over 12 years
    I added some more info on your other conversions (just in case I misunderstood what you meant by "ASCII").
  • Jeff Mercado
    Jeff Mercado over 12 years
    I intentionally left that part out trying to keep this as plain as possible. You should be able to add the prefixing and whatnot wherever needed.
  • Clu
    Clu over 12 years
    Jeff, thanks, most of that works with the exception of not having the prefix and delimiter in the string.concat. since I'm using this on a keyup event, its throwing errors on the parse for int32, basically I'm making a "text" conversion utility thats backwards compatible, so text to hex, decimal, reverse string. then of course the other conversions, decimal to hex, decimal to text, hex to decimal, hex to text. heres a picture of the app to give you an idea of what i'm doing: link