Click here to Skip to main content
14,636,910 members
Home » Discussions » C#
   

C#

 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn1-Jul-20 9:36
sitebuilderLuc Pattyn1-Jul-20 9:36 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox1-Jul-20 10:40
professionalpkfox1-Jul-20 10:40 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn5-Jul-20 3:27
sitebuilderLuc Pattyn5-Jul-20 3:27 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox5-Jul-20 5:45
professionalpkfox5-Jul-20 5:45 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn5-Jul-20 6:19
sitebuilderLuc Pattyn5-Jul-20 6:19 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox5-Jul-20 22:15
professionalpkfox5-Jul-20 22:15 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn6-Jul-20 2:12
sitebuilderLuc Pattyn6-Jul-20 2:12 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox6-Jul-20 3:08
professionalpkfox6-Jul-20 3:08 
Hi Luc, I was just going to post what I've done and I saw your reply - anyway here it is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LMS
{
    public class ParseLMSResponse
    {
        private const string NAME = "ENAME";
        private const string PORT = "JSON";
        private const string UUID = "UUID";
        private const string VERS = "VERS";

        private byte[] ResponseBytes { get; set;}
        private Dictionary<string, string> KeyValuePairs { get; set; }

        // Pass the byte array to the constructor
        public ParseLMSResponse(byte[] ResponseBytes)
        {
            this.ResponseBytes = ResponseBytes;
            this.GetKeyValuePairs();
        }

        private void GetKeyValuePairs()
        {
            // Local working variables
            string keyname = "";
            string keyvalue = "";
            int i = 0;
            byte b;
            int DataLen = 0;

            KeyValuePairs = new Dictionary<string, string>();

            while (i < ResponseBytes.Length) // Process the entire array
            {
                keyname = "";
                keyvalue = "";
                b = ResponseBytes[i];

                while (b >= 65 && b <= 90) // Read while byte is an upper case value
                {
                    // This builds up the keyname byte by byte
                    keyname += Convert.ToChar(b);
                    ++i;
                    b = ResponseBytes[i];
                }

                // At the end of the keyname is the data length
                DataLen = Convert.ToInt32(b);

                ++i; // move i to the start of the value

                // Get string value from bytes
                keyvalue = Encoding.UTF8.GetString(ResponseBytes, i, DataLen);

                // Add to dictionary
                KeyValuePairs.Add(keyname, keyvalue);
                i += DataLen;
            }
        }

        public string GetHostName
        {
            get
            {
                return GetValue(NAME);
            }
        }

        public string GetUUID
        {
            get
            {
                return GetValue(UUID);
            }
        }
        public string GetVersion
        {
            get
            {
                return GetValue(VERS);
            }
        }

        public int GetPortNumber
        {
            get
            {
                return Convert.ToInt32(GetValue(PORT));
            }
        }

        private string GetValue(string Name)
        {
            string RetVal = "";
            KeyValuePairs.TryGetValue(Name, out RetVal);
            return RetVal;
        }
    }
}

A much better way methinks - thanks for your help
"We can't stop here - this is bat country" - Hunter S Thompson - RIP

GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn6-Jul-20 3:23
sitebuilderLuc Pattyn6-Jul-20 3:23 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn6-Jul-20 6:14
sitebuilderLuc Pattyn6-Jul-20 6:14 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox6-Jul-20 6:56
professionalpkfox6-Jul-20 6:56 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn6-Jul-20 7:16
sitebuilderLuc Pattyn6-Jul-20 7:16 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox6-Jul-20 7:27
professionalpkfox6-Jul-20 7:27 
GeneralRe: Strange characters in JSON RPC server response Pin
Luc Pattyn6-Jul-20 7:43
sitebuilderLuc Pattyn6-Jul-20 7:43 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox6-Jul-20 8:05
professionalpkfox6-Jul-20 8:05 
QuestionIntegers to Float IEEE 754 Pin
Member 1487212830-Jun-20 16:39
MemberMember 1487212830-Jun-20 16:39 
AnswerRe: Integers to Float IEEE 754 Pin
Luc Pattyn30-Jun-20 17:29
sitebuilderLuc Pattyn30-Jun-20 17:29 
GeneralRe: Integers to Float IEEE 754 Pin
Member 148721281-Jul-20 5:44
MemberMember 148721281-Jul-20 5:44 
GeneralRe: Integers to Float IEEE 754 Pin
Luc Pattyn1-Jul-20 5:50
sitebuilderLuc Pattyn1-Jul-20 5:50 
GeneralRe: Integers to Float IEEE 754 Pin
Member 148721281-Jul-20 6:08
MemberMember 148721281-Jul-20 6:08 
GeneralRe: Integers to Float IEEE 754 Pin
Luc Pattyn1-Jul-20 6:12
sitebuilderLuc Pattyn1-Jul-20 6:12 
GeneralRe: Integers to Float IEEE 754 Pin
Member 148721281-Jul-20 6:18
MemberMember 148721281-Jul-20 6:18 
GeneralRe: Integers to Float IEEE 754 Pin
Member 148721281-Jul-20 8:09
MemberMember 148721281-Jul-20 8:09 
GeneralRe: Integers to Float IEEE 754 Pin
Luc Pattyn1-Jul-20 9:29
sitebuilderLuc Pattyn1-Jul-20 9:29 
QuestionHow can I find out what "USB Serial Device" is called in all other languages? Pin
arnold_w30-Jun-20 11:14
Memberarnold_w30-Jun-20 11:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.