0

I need to convert an integer value, for example 100, to a hex value of 0x0000064 with a variable type of uint. In C# I am manually assigning this hex value:

uint id = 0x0000064;

I'd like a function that can take the integer input and output the uint hex version with the prefix. I've tried building it up out of strings but then I can't assign that string to a uint type variable. Any C# methods I've tried will just convert the hex value back to the integer. Any ideas? Or am I missing something really obvious? Thank you!

  • 2
    the hex value is just a _string representation_ of the integer. internally, it's a binary number either way. also: what are you even trying to achieve? if you want the number to _show_ as a hex number, just use `id.ToString("X")` – Franz Gleichmann Nov 02 '21 at 17:59
  • Try `"0x" + id.ToString("X8")` – Charlieface Nov 02 '21 at 21:08
  • *Or am I missing something really obvious?* - probably something similar to a complaint like "no matter how many times I try some variation of assigning integer 1 to my `double d` it prints out as 1.0".. 0x64 is 100; if you want to see the hex version when debugging though you can right click on a debugger tooltip and choose hex values.. I'll see if I can find a screenshot.. – Caius Jard Nov 02 '21 at 21:16
  • Are you asking for this: https://stackoverflow.com/questions/3354453/visual-studio-debugger-displaying-integer-values-in-hex ? – Caius Jard Nov 02 '21 at 21:27
  • the hex values identify nodes on a can bus. at first it seemed like the api im using would only accept a uint in that form but it turned out the integer equivalent of the hex value worked as well. thanks for the comments! – bradburydoom Nov 02 '21 at 21:43

0 Answers0