-2

I'm receiving something from my front, which represents a string of a UintArray. Let's say I receive this :

"{"0":122,"1":127,"2":233,"3":149,"4":116,"5":228,"6":91,"7":80,"8":28,"9":111,"10":148,"11":62,"12":45,"13":116,"14":191,"15":157}"

I would like to transform this in an array like this, without the indexes (my UintArray):

[122, 127, 233, 149, 116, 228, 91, 80, 28, 111, 148, 62, 45, 116, 191, 157]

And then, decode it. Is that possible, and is there an easy way to do it? I'm kinda new with the use of C#, and using, for professional purposes, the .net framework version 4.5. Thanks.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
JimmyB
  • 99
  • 1
  • 8

5 Answers5

1

You could maybe do something like this:

First split the string from the commas

string str = "{"0":122,"1":127,"2":233,"3":149,"4":116,"5":228,"6":91,"7":80,"8":28,"9":111,"10":148,"11":62,"12":45,"13":116,"14":191,"15":157}";  
 
string[] strComma = str.Split(",");  

Then split each element of strComma from the colon

string[] strColon;
foreach(string s in strComma)
{
    strColon = s.Split(":");
    Console.WriteLine(strColon[1].ToString());
}

Beware I didnt actually test this code. Also you might need to format that string so it actually becomes a string.

If you want it as an array just add it to an array instead of Console.WriteLine()

Edit:

This is the full code that works

static void Main(string[] args)
        {
            List<int> list = new List<int>();

            string str = "{\"0\":122,\"1\":127,\"2\":233,\"3\":149,\"4\":116,\"5\":228,\"6\":91,\"7\":80,\"8\":28,\"9\":111,\"10\":148,\"11\":62,\"12\":45,\"13\":116,\"14\":191,\"15\":157}";

            str = str.Replace('{', '\0');
            str = str.Replace('}', '\0');
            string[] strComma = str.Split(',');
            

            string[] strColon;
            foreach (string s in strComma)
            {
                strColon = s.Split(':');
                Console.WriteLine(strColon[1].ToString());
                list.Add(Convert.ToInt32(strColon[1]));
            }

            Console.ReadLine();
        }

1

You can achieve this quite simple by using string.split

string tmp = "{\"0\":122,\"1\":127,\"2\":233,\"3\":149,\"4\":116,\"5\":228,\"6\":91,\"7\":80,\"8\":28,\"9\":111,\"10\":148,\"11\":62,\"12\":45,\"13\":116,\"14\":191,\"15\":157}";
var parts = tmp.Split(',');
var finalArray = parts.Select(p=>p.Split(':')[1]).ToArray(); 

Note this is completly without error handling, so it needs a little extra love befoire it is production ready :)

It gives a result like this:

122,127,233,149,116,228,91,80,28,111,148,62,45,116,191,157 
MikNiller
  • 1,242
  • 11
  • 17
1

If you use JSON.NET you could parse it as JSON and extract the property values:

string json = "{\"0\":122,\"1\":127,\"2\":233,\"3\":149,\"4\":116,\"5\":228,\"6\":91,\"7\":80,\"8\":28,\"9\":111,\"10\":148,\"11\":62,\"12\":45,\"13\":116,\"14\":191,\"15\":157}";

int[] parsedArray = JObject.Parse(json)
                            .Properties()
                            .Select(p => p.Value.Value<int>())
                            .ToArray();

Console.WriteLine(string.Join(", ", parsedArray));
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
1

Alternatively, you can apply Newtonsoft.Json to parse Json to array.

using Newtonsoft.Json;

string json = "{\"0\":122,\"1\":127,\"2\":233,\"3\":149,\"4\":116,\"5\":228,\"6\":91,\"7\":80,\"8\":28,\"9\":111,\"10\":148,\"11\":62,\"12\":45,\"13\":116,\"14\":191,\"15\":157}";
var numbers = JsonConvert.DeserializeObject<Dictionary<string, int>>(json)
    .Select(x => x.Value)
    .ToArray();
Yong Shun
  • 35,286
  • 4
  • 24
  • 46
0

Thank you all for your comments. I did it like this :

string json = sub.Keys["p256dh"];
                  int[] parsedArray = JObject.Parse(json)
                            .Properties()
                            .Select(p => p.Value.Value<int>())
                            .ToArray();
                  visitor.P256dh = string.Join(", ", parsedArray);

And i finally got my int array! Now, is it possible to turn this array into a base 64 so I can decode it? Thanks.

JimmyB
  • 99
  • 1
  • 8
  • Check this out https://stackoverflow.com/questions/11743160/how-do-i-encode-and-decode-a-base64-string – Özgür Güzeldereli Apr 09 '21 at 09:02
  • I don't really follow what the connection with base64 is here. The values don't seem to represent base64-encoded data (they don't represent ASCII character codes that can be interpreted as a string containing base64), and encoding the data you do have and then decoding it wouldn't make any sense since it would effectively be a no-op. – ProgrammingLlama Apr 09 '21 at 09:04