1

Details about the application:

  • Developed under Visual Studio 2019 (Windows 10)
  • Designed on UWP platform with C# & XAML language

My application receive a frame from a remote server. After receiving the frame, I decode all received information, it's here that I have a problem.

Data received (hexa) : CA0000000100000030000000010000003137322E31362E3233392E343100000000000000000000000000000000000000

Code:

int _IndexLecture = 0;
bool _PRIORITE;
string _ABONNE;

_TraitementString = _Decode.Substring(_IndexLecture + 8, 2);
_VOIE = Convert.ToSByte(_TraitementString, 16);

_TraitementString = _Decode.Substring(_IndexLecture + 24, 2);
_PRIORITE = Convert.ToBoolean(_TraitementString);

_TraitementString = _Decode.Substring(_IndexLecture + 32, 64);
_ABONNE = Convert.ToString(_TraitementString);

Obtained result :

_VOIE = 1
_PRIORITE = 
_ABONNE = 

Exepected result :

_VOIE = 1
_PRIORITE = TRUE
_ABONNE = "172.16.239.41"

How can I pass my hex string to bool and ASCII string to find the right values?

ValentinDP
  • 323
  • 5
  • 14
  • 1
    It looks almost correct. You should be able to debug most of it, e.g. Convert.ToBoolean("01") throws exception and could be written as _PRIORITE = _IndexLecture == "01" – Thomas Koelle May 28 '19 at 12:39

1 Answers1

1

String Hex to Bool and String Hex to String ASCII conversion

The reason why _PRIORITE is not correct, because _IndexLecture is string type. you could not pass such "01" string parameter to ToBoolean(sting value) method, you can only pass "true" or "false" string parameter. Please use the following method to replace.

_TraitementString = _Decode.Substring(_IndexLecture + 24, 2);   
_PRIORITE = Convert.ToBoolean(int.Parse(_TraitementString, System.Globalization.NumberStyles.AllowHexSpecifier));

How to pass hex string to IPAddress.

You could refer this case reply. And please note Internet Protocol version 4 (IPv4) defines an IP address as a 32-bit number. So, it can expressed in 8-bit hexadecimal numbers. Please check if _TraitementStringis correct.

var ip = new IPAddress(long.Parse("4a0e94cb", NumberStyles.AllowHexSpecifier));
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36