-2

I tried this code to convert Decimal to hex, but it doesn't work.

Dim i As Integer
    Dim x As String = sASC
    i = Convert.ToInt32(x, 16)
    TextBox8.Text = i

This is what i want to conver. 912,697,583,1065,261,627,53,703,544,697,924,1003,697

Goma
  • 53
  • 2
  • 10

1 Answers1

1

You can reach your goal with a single line of code in Linq

Dim input = "912,697,583,1065,261,627,53,703,544,697,924,1003,697"

Dim result = String.Join(",", input.Split(","c).
                              Select(Function(x) _
                              Convert.ToInt32(x).ToString("X")))
Console.WriteLine(result)

' 390,2B9,247,429,105,273,35,2BF,220,2B9,39C,3EB,2B9

This will convert each value in the input string in its equivalent hex value and rebuild the string with the comma separator

Back to base 10 values is (result is from the previous code)

Dim result2 = String.Join(",", result.Split(","c).
                          Select(Function(x) _
                          Convert.ToInt32(x, 16).ToString()))
Console.WriteLine(result2)
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thanks, now how to convert from Hex To Decimal? To reach same format Above. – Goma Mar 26 '16 at 00:00
  • _Convert.ToInt32(value, frombase)_ Meaning that you apply the same logic to the hexdecimal string but apply the Convert.ToInt32 overload that specify a second parameter equals to 16 – Steve Mar 26 '16 at 08:41
  • I understand @Steve, but i don't know how to do that in code, could you give me code. plz – Goma Mar 26 '16 at 14:14
  • Thanks, but is this code work if does not have a comma?? What should i do for that? – Goma Mar 26 '16 at 17:05
  • No this code works splitting the individual numbers at the comma, Without a comma what would be the rule to divide the string? – Steve Mar 26 '16 at 17:26
  • I don't know, can we replace "comma" and put another "char" or Zero "0"? – Goma Mar 26 '16 at 17:37
  • Yes, but I don't understand these changes. Perhaps you could post a new question explaining in detail what are your changed requirements. In this way more people will see your problem and could help better than just me in this now 'old' question. – Steve Mar 26 '16 at 17:44