Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

Converting an hex string to an ascii string from http request

I've been building a little tool that retrieves data from a website through an http request, and then using this data to compare different football players. The website I'm using is understat.com, and when I get the page, then filter it to the specific data I need, the result I get is something like this:

x22datex22x3Ax222020x2D11x2D08x2014x3A00x3A00x22,x22winsx22x3A0,x22drawsx22x3A0,x22losesx22x3A1,x22ptsx22x3A0,x22npxGDx22x3Ax2D2.844799999999999773336867292528040707111358642578125x7D,x7Bx22h_ax22x3Ax22ax22,x22xGx22x3A0.73111599999999998811262003073352389037609100341796875,x22xGAx22x3A0.413067000000000017490009440734866075217723846435546875

It is far longer (after all, it's almost all players in a league), but it's just repeated patterns for all the players. I can't figure out how to turn this into a readable JSON, if possible, (which I'm pretty sure the website actually uses) to then use myself. I've tried an handful of "converters", the latter of which is as follows:

        List<string> findhex = data.Split(new string[] { "&#", ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
        StringBuilder sb = new StringBuilder();
        foreach(string st in findhex)
        {
            if(st.Substring(0, 1) == "x")
            {
                string hs = st.Substring(1, 2);
                sb.Append(Convert.ToChar(Convert.ToUInt32(hs, 16)));
            }
            else
            {
                sb.Append(st);
            }
        }
        String ascii = sb.ToString();
        return ascii;

but they all result in very confused mess, or straight up more unconverted hex-looking stuff (not too expert on this). An example of "very confused mess" is as follows:

{u0022attu0022:322,u0022defu0022:23},u0022deepu0022:2,u0022deep_allowedu0022:1,u0022scoredu0022:0,u0022missedu0022:1,u0022xptsu0022:0.89449999999999996180832795289461500942707061767578125,u0022resultu0022:u0022lu0022,u0022dateu0022:u00222021-01-31 11:30:00u0022,u0022winsu0022:0,u0022drawsu0022:0,u0022losesu0022:1,u0022ptsu0022:0,u0022npxGDu0022:0.3743179999999999285620333466795273125171661376953125}]}}"

Lastly, the code I've been using to do the request, filter the data I care for and then convert it is as follows:

        public async Task Update() {
        try{
            HttpResponseMessage response = await client.GetAsync("https://understat.com/league/Serie_A");
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            string sub1 = responseBody.Split("var teamsData = JSON.parse('")[1];
            string sub2 = sub1.Split("')")[0];
            string teamData = HexConvert.DecodeHex(sub2);
            Console.WriteLine(teamData);
        } catch(HttpRequestException e) {
            Console.WriteLine("
Exception Caught!");   
            Console.WriteLine("Message :{0} ",e.Message);
        }
    }

With HexConvert.DecodeHex being the converter I'm using. Does anyone know how I can convert this into a readable format? Thank you in advance!

PS: I hope I included all the information needed, rarely ask questions and I am not too expert yet, being a student, so I'm never 100% sure

question from:https://stackoverflow.com/questions/66056958/converting-an-hex-string-to-an-ascii-string-from-http-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...