forked from MathiasCiarlo/ROSBridgeLib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ROSBridgeUtils.cs
74 lines (70 loc) · 3 KB
/
ROSBridgeUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using SimpleJSON;
namespace ROSBridgeLib
{
namespace custom_utils
{
/// <summary>
/// A class for putting utility methods used across various components/classes in the ROSBridgeLib namespace.
/// </summary>
public class ROSBridgeUtils
{
/// <summary>
/// Convert a JSONNode containing only raw data in string form to a byte array.
/// </summary>
/// <param name="node">The node directly encapsulating the data.</param>
/// <param name="is_bigendian">Whether the data is big endian.</param>
/// <returns>The raw bytes as a byte array.</returns>
/// <remarks>Assumes the string is in base64.</remarks>
public static byte[] JSONDataToBytes(JSONNode node, bool is_bigendian = true)
{
byte[] data = System.Convert.FromBase64String(node.Value);
if (!is_bigendian)
{
System.Array.Reverse(data);
}
return data;
}
/// <summary>
/// Convert a JSONNode containing raw data as a JSON array to a byte array.
/// </summary>
/// <param name="node">The JSON array directly holding the data.</param>
/// <param name="is_bigendian">Whether the data is big endian.</param>
/// <returns>The raw bytes as a byte array.</returns>
public static byte[] JSONArrayToBytes(JSONArray array, bool is_bigendian = true)
{
byte[] data = new byte[array.Count];
if (is_bigendian)
{
for (int i = 0; i < data.Length; i++)
{
data[i] = (byte)array[i].AsInt;
}
}
else
{
for (int i = 0; i < data.Length; i++)
{
data[data.Length - i - 1] = (byte)array[i].AsInt;
}
}
return data;
}
/// <summary>
/// A convenience function to convert a JSONNode object containing raw data to a byte array.
/// Auto-detects whether it is in string format or JSON array format.
/// </summary>
/// <param name="node">The node directly contaiing the data.</param>
/// <param name="is_bigendian">Whether the data is big endian.</param>
/// <returns>The raw bytes as a byte array.</returns>
public static byte[] ParseJSONRawData(JSONNode node, bool is_bigendian = true)
{
if (node.GetType() == typeof(JSONArray)) {
return JSONArrayToBytes(node.AsArray, is_bigendian);
} else
{
return JSONDataToBytes(node, is_bigendian);
}
}
}
}
}