0

Okay so lets say I input this in to a rich textbox:

private void noRecoilOn()
{
    this.Jtag.WriteUInt32(2200480928u, 1024u);
}
private void noRecoilOff()
{
    this.Jtag.WriteUInt32(2200480928u, 0u);
}

I want to be able to parse out the decimals and convert it to Hexadecimal like so:

private void noRecoilOn()
{
    this.Jtag.WriteUInt32(0x8328ACA0, 0x400);
}
private void noRecoilOff()
{
    this.Jtag.WriteUInt32(0x8328ACA0, 0x0);
}

This is just a quick example but the real things I will be converting will be whole blocks of code with many different decimals that need to be converted, not just "2200480928u".

How would I go about doing this. I have no idea

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user2692560
  • 71
  • 2
  • 9

1 Answers1

0

I would suggest looking at How to: Convert Between Hexadecimal Strings and Numeric Types (C# Programming Guide)

In addition the ToString() has a format to write to Hex and UInt32.Parse can be provided an IFormat

uint num = 2200480928u;

// Converts the uint to a hex string
string hex = num.ToString("X");


// Convert the hex string back to the number
uint number = UInt32.Parse(hex, System.Globalization.NumberStyles.HexNumber);
Harrison
  • 3,843
  • 7
  • 22
  • 49
  • I will try this when i get home. The only other question I have would be how can I parse the uint out of the block of code I an passing through the input? – user2692560 Dec 04 '13 at 13:10
  • @user2692560 You would need to find an appropriate pattern to search for and replace. Possibly a Regex expression like *"[0-9]+u"*. But that makes an assumption that all numbers followed by a u should be changed. – Harrison Dec 04 '13 at 14:52
  • This pattern works well for what I need but is there a way to edit it so it will only replace uints that are a certain length? For example only replace uints that are 7 chars long – user2692560 Dec 04 '13 at 19:50
  • *"[0-9]{7}u"* would look for exactly 7 numbers followed by a u and "*[0-9]{7,}u"* would look for at least 7 numbers followed by a u. [see this](http://stackoverflow.com/questions/1649435/regular-expression-to-limit-number-of-characters-to-10) – Harrison Dec 04 '13 at 21:33