forked from Azure/iotedge-lorawan-starterkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sample code for decoding string and binary payloads (#66)
* Add sample for decoding binary payload - Add ByteArrayToString method - Add example decoders for string and binary payloads - Remove unnecessary using statements * Update LoraDecoders.cs - Move both examples to one sample decoder: DecoderValueSensor
- Loading branch information
1 parent
6232bea
commit 76baf91
Showing
5 changed files
with
49 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Text; | ||
|
||
namespace SensorDecoderModule.Classes | ||
{ | ||
public static class ConversionHelper | ||
{ | ||
/// <summary> | ||
/// Method enabling to convert a hex string to a byte array. | ||
/// </summary> | ||
/// <param name="hex">Input hex string</param> | ||
/// <returns></returns> | ||
public static byte[] StringToByteArray(string hex) | ||
{ | ||
int NumberChars = hex.Length; | ||
byte[] bytes = new byte[NumberChars / 2]; | ||
for (int i = 0; i < NumberChars; i += 2) | ||
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); | ||
return bytes; | ||
} | ||
|
||
public static string ByteArrayToString(byte[] bytes) | ||
{ | ||
StringBuilder Result = new StringBuilder(bytes.Length * 2); | ||
string HexAlphabet = "0123456789ABCDEF"; | ||
|
||
foreach (byte B in bytes) | ||
{ | ||
Result.Append(HexAlphabet[(int)(B >> 4)]); | ||
Result.Append(HexAlphabet[(int)(B & 0xF)]); | ||
} | ||
|
||
return Result.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters