-1

I have a body of text that is innertext inside of an XML. Here are 3 lines of that string for example

0x2007A3C8,0xAE8900B8,
0x2007A3CC,0x000E5320,
0x2007A3D0,0x03E00008

So the innertext property is a string. I am trying to convert this entire string back into an uint array. So that for every comma adds a new array element.

x , x
x , x

Would be a total of 4 array elements.

I want to keep the hex syntax, everything. I just need this back into an uint array. Any ideas?

001
  • 13,291
  • 5
  • 35
  • 66
  • 1
    You will need to [split the string](https://www.c-sharpcorner.com/UploadFile/mahesh/split-string-in-C-Sharp/), then [parse it to long](https://stackoverflow.com/questions/10715534/long-parse-c-sharp), [convert the long value to string](https://learn.microsoft.com/en-us/dotnet/api/system.object.tostring?view=net-6.0), and finally [join the strings back again](https://learn.microsoft.com/en-us/dotnet/api/system.string.join?view=net-6.0). – D.Kastier Feb 15 '22 at 14:39
  • 2
    How do you go from 6 input numbers to 4 output numbers? – 001 Feb 15 '22 at 14:44
  • 1
    Welcome to Stack Overflow! Your question seems to be [homework](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions). – Mat Feb 15 '22 at 14:45
  • I’m voting to close this question because it seems to be homework and shows no own commitment so far. – Markus Safar Feb 15 '22 at 14:48
  • Your provided data seems inconsistent to me `3 lines` `6 values` `4 array elements`. How do you go from `3 lines` or `6 values` to `4 array elements`? To me it is also unclear if you want to have one array with all the values or seperate arrays for each line. – Rand Random Feb 15 '22 at 14:52
  • 1
    Does this answer your question? [How to parse hex values into a uint?](https://stackoverflow.com/questions/98559/how-to-parse-hex-values-into-a-uint) – Mat Feb 15 '22 at 15:01

2 Answers2

1

It should be as simple as this:

string input = "0x2007A3C8,0xAE8900B8, 0x2007A3CC,0x000E5320, 0x2007A3D0,0x03E00008";
string[] strNumbers = input.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
uint[] numbers =  Array.ConvertAll(strNumbers, z => Convert.ToUInt32(z.Trim(), 16));
Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • this works. but its giving me the actual int value of the hex values. ex "537371648" how can i get those back to the 0x20000 hex values that are an array? – bismofunyuns Feb 16 '22 at 01:50
  • @bismofunyuns - If you want the hex values, then just omit the last line and you'll have a `string[]` array of hexadecimal values. Since a hexadecimal number contains letters and numbers, the data type must be a *string* and not a *uint* type. – Icemanind Feb 16 '22 at 03:59
  • Are you sure? When I declare an uint array, as long as I have the 0x before the 8 digit hex, it doesn’t throw any errors and works normally. I need to pass these array values through a function that I am writing to memory with – bismofunyuns Feb 16 '22 at 07:05
  • m = new MemorySharp(Process.GetProcessesByName(PCSX2PROCESS).FirstOrDefault()); uint address = 0; uint data = 1; uint Length = (uint)ASM.Length; while (address < Length) { m.Write((IntPtr)ASM[address], ASM[data], false); address += 2; data += 2; } – bismofunyuns Feb 16 '22 at 08:50
  • the params of m.write are uint and intptr. i'm trying to remove the asm code completely from my program into an xml file that needs to be decrypted , i've got that part done but now i need to get this encrypted string back into uint format while keeping the hex format. – bismofunyuns Feb 16 '22 at 08:51
  • actually i was wrong. apparently those hex format gets put into int when i set a breakpoint on the m.write .. your method is perf. thx a lot m8! – bismofunyuns Feb 16 '22 at 16:06
1

I'd definitely propose using LINQ for that:

var initialString = "0x2007A3C8,0xAE8900B8,\n0x2007A3CC,0x000E5320,\n0x2007A3D0,0x03E00008";
var unsignedValues = initialString
                            .Split(',')
                            .Select(n => Convert.ToUInt32(n.Trim(), 16));

Don't really understand about "keeping the hex syntax", but you can sustain it like this, I guess:

var initialString = "0x2007A3C8,0xAE8900B8,\n0x2007A3CC,0x000E5320,\n0x2007A3D0,0x03E00008";
var unsignedValues = initialString
                            .Split(',')
                            .Select(n => "0x" + Convert.ToUInt32(n.Trim(),16));
PavelPerov
  • 65
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 15 '22 at 15:24