I would like to know how to reshape a 2-D linearized Array in Scala without loops, i.e. by using functional programming.
Assume a 2-D Array of 6x12 = 72 elements. What I need is to resize this Array so that it has 8x16 = 128 elements, assuming that the new elements are initialized to zero.
This is how I do it with loops:
val a = (1 to 72).toArray
a: Array[Int] = Array(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)
scala> val h = 6
h: Int = 6
scala> val w = 12
w: Int = 12
scala> val H = 8
H: Int = 8
scala> val W = 16
W: Int = 16
scala> val b = Array.ofDim[Int](H * W)
b: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
for (i <- 0 until h)
for (j <- 0 until w)
b(i * W + j) = a(i * w + j)
b: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
The question is: how to do this by using functional programming?
Thank you very much!
a.grouped(w).map(_.padTo(W, 0)).padTo(H, Array.fill(W)(0)).flatten.toArray
Result (and string copied from your code, for comparison):
result : Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
b: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
Related
I'm getting a different result trying to replace numbers in an array with the number's cosine depending what I do.
I have a 10 x 10 Numpy array and I'm trying to apply a cosine to the even columns.
This is my array
tens
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18],
[ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27],
[ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36],
[ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45],
[ 0, 6, 12, 18, 24, 30, 36, 42, 48, 54],
[ 0, 7, 14, 21, 28, 35, 42, 49, 56, 63],
[ 0, 8, 16, 24, 32, 40, 48, 56, 64, 72],
[ 0, 9, 18, 27, 36, 45, 54, 63, 72, 81]])
I rotate and slice the array to apply the cosine to the even columns and use the same slicing and rotating to store the cosine in the right position.
tens[:, 0::2] = np.cos(tens[:, 0::2])
Here is the array after that
tens
array([[ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[ 1, 1, 0, 3, 0, 5, 0, 7, 0, 9],
[ 1, 2, 0, 6, 0, 10, 0, 14, 0, 18],
[ 1, 3, 0, 9, 0, 15, 0, 21, 0, 27],
[ 1, 4, 0, 12, 0, 20, 0, 28, 0, 36],
[ 1, 5, 0, 15, 0, 25, 0, 35, 0, 45],
[ 1, 6, 0, 18, 0, 30, 0, 42, 0, 54],
[ 1, 7, 0, 21, 0, 35, 0, 49, 0, 63],
[ 1, 8, 0, 24, 0, 40, 0, 56, 0, 72],
[ 1, 9, 0, 27, 0, 45, 0, 63, 0, 81]])
I'm not sure why I get integers here instead of floats. I don't get this if I apply a cosine to the original array. I get this.
np.cos(tens[:, 1::2])
array([[ 1. , 1. , 1. , 1. , 1. ],
[ 0.54030231, -0.9899925 , 0.28366219, 0.75390225, -0.91113026],
[-0.41614684, 0.96017029, -0.83907153, 0.13673722, 0.66031671],
[-0.9899925 , -0.91113026, -0.75968791, -0.54772926, -0.29213881],
[-0.65364362, 0.84385396, 0.40808206, -0.96260587, -0.12796369],
[ 0.28366219, -0.75968791, 0.99120281, -0.90369221, 0.52532199],
[ 0.96017029, 0.66031671, 0.15425145, -0.39998531, -0.82930983],
[ 0.75390225, -0.54772926, -0.90369221, 0.30059254, 0.98589658],
[-0.14550003, 0.42417901, -0.66693806, 0.85322011, -0.96725059],
[-0.91113026, -0.29213881, 0.52532199, 0.98589658, 0.77668598]])
Is there something happening with floats and integers when I try to store these numbers in their correct positions in the original array?
I receive data from an external device in decimal values, that I need to convert in ascii and then push it in a list
data example:
#onData: 1,3,200,
78,69,84,71,69,65,82,45,71,117,101,115,116,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
70,114,105,103,111,109,97,116,45,49,50,51,52,53,54,55,56,45,87,73,70,73,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,45,71,85,69,83,84,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,123
The first 3 elements of #onData need to be ignored and the last 2 elements as well.
In ascii it would be:
NETGEAR-Guest
Frigomat-12345678-WIFI
MICRO-SYSTEMS
MICRO-SYSTEMS-GUEST
MICRO-SYSTEMS
I have a method to convert:
arrayBuffer2str(buf) {
var str = "";
var ui8 = new Uint8Array(buf);
for (var i = 0; i < ui8.length; i++) {
str = str + String.fromCharCode(ui8[i]);
}
return str;
}
I would need to get rid of the zeros and then push it in an array of strings:
this.wifiNetworks.push("Network-1");
Thank you
If you want to extract the buffer between two zeros and isolate the significant response, try this
let prevVal;
let phrase= [];
for (let i=0; i<array.length; i++) {
let val = array[i];
if (val == 0 && prevVal != 0) {
// end of response
process(phrase);
}
if (prevVal == 0 && val != 0) {
// start new response
phrase= [];
}
phrase.push(val);
prevVal = val;
}
function process(phrase) {
// TODO Process your phrase here and remove the inner zeros
}
Also, consider removing your zeros using the .filter function
Try this,
Assuming that we are getting the input as an array.
const inputArray = [1, 3, 200,
78, 69, 84, 71, 69, 65, 82, 45, 71, 117, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70, 114, 105, 103, 111, 109, 97, 116, 45, 49, 50, 51, 52, 53, 54, 55, 56, 45, 87, 73, 70, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 45, 71, 85, 69, 83, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 123];
const resultArray = [];
let stringIndex = 0;
for (i = 3; i <= inputArray.length - 3; i++) {
if (inputArray[i] !== 0) {
resultArray[stringIndex] = resultArray[stringIndex]
? resultArray[stringIndex].concat(String.fromCharCode(inputArray[i]))
: ''.concat(String.fromCharCode(inputArray[i]));
} else if (inputArray[i - 1] !== 0) {
stringIndex = stringIndex + 1
}
}
console.log(resultArray);
// Add code to push the result to server,
The output would be
[ 'NETGEAR-Guest', 'Frigomat-12345678-WIFI', 'MICRO-SYSTEMS',
'MICRO-SYSTEMS-GUEST', 'MICRO-SYSTEMS' ]
I have a numpy array called new_input_processed. The code below transforms it into a one hot array of type float32 (cf byte_list). But when I type byte_list to see the values of this array, I get an empty tensor. I would like to have a non-empty tensor instead. Is it possible ?
In [30]: new_input_processed
Out[30]:
array([[ 83, 111, 109, 101, 32, 83, 101, 113, 117, 101, 110, 99, 101,
32, 111, 102, 32, 99, 104, 97, 114, 97, 99, 116, 101, 114,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
In [31]: byte_list = tf.cast(tf.one_hot(new_input_processed, 256, 1, 0), dtype=tf.float32)
In [32]: byte_list
Out[32]: <tf.Tensor 'Cast_2:0' shape=(1, 100, 256) dtype=float32>
You are not getting an empty tensor. The Tensor object info is returned properly with:
<tf.Tensor 'Cast_2:0' shape=(1, 100, 256) dtype=float32>
Look at the shape, it is just as expected.
Nevertheless, if you want to see the content (i.e. the actual value of the byte_list Tensor object), one way is to call eval().
Something like this should do:
import numpy as np
import tensorflow as tf
new_input_processed = np.array([[ 83, 111, 109, 101, 32, 83, 101, 113, 117, 101, 110, 99, 101,
32, 111, 102, 32, 99, 104, 97, 114, 97, 99, 116, 101, 114,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8)
byte_list = tf.cast(tf.one_hot(new_input_processed, 256, 1, 0), dtype=tf.float32)
with tf.Session() as sess: print(byte_list.eval()) # here
Output:
[[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
...
[1. 0. 0. ... 0. 0. 0.]
[1. 0. 0. ... 0. 0. 0.]
[1. 0. 0. ... 0. 0. 0.]]]
I'm trying to set up a 320x240 "room" in a game. I want to use a list of 16x16 tiles. Each tile is a predefined list, consisting of the each pixel's color.
extern char blue27tile[] ={
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55};
extern char grey10tile[] ={
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17};
So above would be my tile definitions. Ignore that they're all just blocks of one color, that would change in the future.
My question is how to set up the same array/list for the room? I was thinking a room like:
extern int testroom1[] ={
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
where "1" refers to the grey10tile and "0" refers to the blue27tile. I would correspond numbers to the variables with something like:
extern int *tilelist[2];
tilelist[0] = &blue27tile;
tilelist[1] = &grey10tile;
but this gives me a "typemismatch in redeclaration of tilelist" error.
I know this is a simple question, I don't know C and am trying to learn with this project. I can't describe the problem well enough to find a solution by googling.
If this was python, I would just do something like
tilelist[0] = [17,17,17,...] //all of my grey10tile 17s
tilelist[1] = [55,55,55,...] //all of my blue27tile 55s
tilelist[0][1] -> 17 //the first of my grey10tile 17s
How can I set this up to do something similar in C?
Thanks a ton.
You cannot place statements outside of functions in C. At global scope only declarations are allowed. tilelist[0] = &blue27tile; is a statement, for example.
Try:
char *tilelist[] = { blue27tile, grey10tile };
I am currently developing a display application for F1 2017 on my iPhone using swift. I am able to read the UDP packets from my PS4 to my phone by using SwiftSockets.
I received the following data:
[25, 79, 3, 64, 176, 153, 15, 61, 244, 133, 165, 69, 0, 20, 145, 192, 223, 145, 218, 194, 165, 106, 64, 64, 15, 88, 234, 67, 220, 222, 109, 59, 168, 219, 67, 187, 42, 245, 4, 59, 200, 163, 186, 57, 65, 126, 46, 63, 22, 207, 77, 59, 13, 81, 59, 191, 127, 79, 59, 191, 0, 73, 203, 187, 141, 126, 46, 191, 180, 81, 37, 190, 5, 254, 41, 189, 251, 167, 151, 62, 145, 185, 39, 62, 7, 210, 92, 63, 203, 186, 128, 192, 218, 25, 99, 64, 112, 21, 0, 191, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 24, 106, 72, 61, 239, 89, 49, 187, 0, 0, 192, 64, 191, 199, 133, 69, 0, 0, 0, 0, 0, 0, 144, 65, 0, 80, 195, 72, 0, 80, 195, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 75, 253, 209, 66, 0, 0, 210, 66, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 80, 182, 232, 65, 80, 182, 232, 65, 80, 182, 232, 65, 80, 182, 232, 65, 0, 0, 172, 65, 0, 0, 172, 65, 0, 0, 184, 65, 0, 0, 184, 65, 0, 0, 0, 64, 0, 0, 198, 66, 57, 170, 165, 69, 128, 93, 205, 66, 0, 128, 84, 70, 0, 96, 134, 69, 0, 0, 16, 65, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 252, 68, 240, 253, 176, 66, 131, 119, 113, 188, 93, 249, 95, 187, 28, 146, 43, 59, 116, 163, 23, 188, 87, 87, 88, 88, 0, 0, 0, 0, 5, 54, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 200, 88, 12, 67, 0, 0, 255, 20, 0, 146, 115, 234, 194, 88, 249, 59, 64, 95, 94, 231, 67, 128, 93, 205, 66, 176, 153, 15, 61, 24, 134, 204, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 122, 64, 100, 2, 18, 7, 5, 0, 0, 0, 0, 245, 73, 129, 193, 65, 222, 85, 64, 134, 169, 0, 68, 32, 234, 205, 66, 224, 188, 92, 67, 94, 87, 203, 66, 0, 0, 0, 0, 0, 0, 0, 0, 62, 112, 162, 69, 34, 2, 15, 3, 5, 1, 2, 0, 0, 134, 253, 181, 65, 69, 178, 83, 64, 5, 6, 10, 68, 136, 6, 200, 66, 70, 194, 141, 67, 180, 236, 199, 66, 0, 0, 0, 0, 0, 0, 0, 0, 17, 208, 160, 69, 16, 0, 5, 3, 5, 1, 2, 0, 0, 16, 137, 185, 195, 194, 6, 44, 64, 211, 20, 153, 66, 64, 15, 198, 66, 238, 232, 27, 61, 64, 15, 198, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 208, 253, 67, 0, 1, 1, 4, 5, 0, 0, 0, 0, 83, 13, 149, 65, 96, 78, 84, 64, 101, 6, 9, 68, 170, 16, 200, 66, 82, 86, 44, 68, 212, 184, 199, 66, 0, 0, 0, 0, 0, 0, 0, 0, 75, 252, 160, 69, 6, 1, 3, 3, 5, 1, 2, 0, 0, 247, 239, 27, 68, 227, 16, 124, 64, 33, 229, 220, 67, 171, 9, 202, 66, 0, 0, 0, 0, 171, 9, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 86, 90, 110, 69, 7, 11, 11, 2, 5, 0, 2, 0, 0, 91, 113, 167, 67, 92, 188, 60, 64, 223, 10, 42, 68, 58, 73, 206, 66, 0, 0, 0, 0, 58, 73, 206, 66, 0, 0, 0, 0, 0, 0, 0, 0, 214, 218, 146, 69, 18, 5, 19, 3, 5, 0, 2, 0, 0, 235, 33, 44, 68, 26, 50, 118, 64, 22, 232, 74, 68, 64, 189, 200, 66, 0, 0, 0, 0, 64, 189, 200, 66, 0, 0, 0, 0, 0, 0, 0, 0, 190, 18, 132, 69, 5, 6, 7, 3, 5, 0, 2, 0, 0, 63, 156, 38, 68, 27, 127, 136, 64, 55, 132, 5, 68, 48, 100, 203, 66, 0, 0, 0, 0, 48, 100, 203, 66, 0, 0, 0, 0, 0, 0, 0, 0, 12, 190, 116, 69, 35, 7, 16, 3, 5, 0, 2, 0, 0, 214, 118, 54, 196, 239, 49, 70, 64, 111, 224, 185, 195, 176, 46, 202, 66, 200, 232, 13, 61, 176, 46, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 128, 229, 135, 68, 3, 7, 13, 4, 5, 0, 0, 0, 0, 122, 220, 252, 67, 157, 82, 60, 64, 88, 205, 89, 68, 208, 106, 202, 66, 0, 0, 0, 0, 208, 106, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 213, 35, 138, 69, 14, 11, 14, 2, 5, 0, 2, 0, 0, 43, 225, 15, 68, 97, 160, 103, 64, 244, 178, 187, 67, 216, 10, 202, 66, 0, 0, 0, 0, 216, 10, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 64, 42, 105, 69, 1, 8, 12, 3, 5, 0, 2, 0, 0, 12, 73, 197, 67, 40, 119, 53, 64, 115, 141, 56, 68, 199, 157, 201, 66, 0, 0, 0, 0, 199, 157, 201, 66, 0, 0, 0, 0, 0, 0, 0, 0, 5, 231, 143, 69, 10, 3, 8, 2, 5, 0, 2, 0, 0, 66, 178, 177, 67, 10, 45, 66, 64, 88, 176, 42, 68, 168, 124, 203, 66, 0, 0, 0, 0, 168, 124, 203, 66, 0, 0, 0, 0, 0, 0, 0, 0, 230, 9, 146, 69, 20, 3, 17, 3, 5, 0, 2, 0, 0, 140, 235, 54, 68, 99, 244, 101, 64, 198, 155, 62, 68, 16, 171, 201, 66, 0, 0, 0, 0, 16, 171, 201, 66, 0, 0, 0, 0, 0, 0, 0, 0, 109, 212, 129, 69, 33, 6, 9, 2, 5, 0, 2, 0, 0, 21, 102, 86, 66, 112, 68, 96, 64, 60, 2, 135, 65, 128, 194, 199, 66, 164, 206, 103, 66, 128, 194, 199, 66, 96, 174, 4, 66, 0, 0, 0, 0, 236, 0, 63, 69, 22, 0, 4, 3, 5, 0, 1, 0, 0, 47, 20, 201, 67, 201, 159, 49, 64, 18, 130, 60, 68, 8, 44, 199, 66, 86, 200, 170, 66, 8, 44, 199, 66, 64, 16, 4, 66, 64, 253, 217, 65, 94, 90, 143, 69, 9, 4, 2, 3, 5, 0, 2, 0, 0, 32, 154, 227, 194, 27, 193, 93, 64, 243, 43, 77, 196, 251, 231, 201, 66, 0, 0, 0, 0, 251, 231, 201, 66, 0, 0, 0, 0, 0, 0, 0, 0, 112, 189, 255, 68, 23, 8, 10, 3, 5, 0, 1, 0, 0, 157, 12, 27, 66, 80, 210, 84, 64, 234, 220, 13, 68, 240, 20, 200, 66, 204, 189, 15, 67, 240, 20, 200, 66, 0, 0, 0, 0, 0, 0, 0, 0, 113, 36, 160, 69, 15, 4, 6, 2, 5, 1, 2, 0, 0, 70, 217, 139, 67, 81, 9, 51, 64, 70, 253, 57, 68, 81, 176, 206, 66, 204, 63, 149, 60, 81, 176, 206, 66, 0, 0, 0, 0, 0, 0, 0, 0, 71, 132, 149, 69, 31, 5, 20, 2, 5, 0, 2, 0, 0, 182, 135, 20, 192, 86, 227, 218, 187, 41, 207, 77, 187, 23, 39, 22, 59, 188, 55, 6, 59, 86, 40, 253, 58, 203, 2, 123, 66, 62, 192, 141, 194, 179, 242, 115, 66, 2, 233, 141, 194, 207, 30, 49, 191, 192, 105, 55, 62, 231, 125, 246, 190]
How would I be able to decode the data into a UDPPacket? In order to do this, I think I need to convert the data into four bytes and then somehow convert it to a float.
Here is my swift code:
import UIKit
import SwiftSocket
class ViewController: UIViewController {
var server: UDPServer!
var timer: Timer!
override func viewDidLoad() {
super.viewDidLoad()
server = UDPServer(address: "192.168.1.158", port: 20777)
timer = Timer.scheduledTimer(withTimeInterval: 0, repeats: true) { _ in
print("a")
let data = self.server.recv(1289).0
if let data = data {
print(data)
self.timer.invalidate()
}
}
timer.fire()
}
}
This is what the UDPPacket should look like:
struct UDPPacket
{
float m_time;
float m_lapTime;
float m_lapDistance;
float m_totalDistance;
float m_x; // World space position
float m_y; // World space position
float m_z; // World space position
float m_speed; // Speed of car in MPH
float m_xv; // Velocity in world space
float m_yv; // Velocity in world space
float m_zv; // Velocity in world space
float m_xr; // World space right direction
float m_yr; // World space right direction
float m_zr; // World space right direction
float m_xd; // World space forward direction
float m_yd; // World space forward direction
float m_zd; // World space forward direction
float m_susp_pos[4]; // Note: All wheel arrays have the order:
float m_susp_vel[4]; // RL, RR, FL, FR
float m_wheel_speed[4];
float m_throttle;
float m_steer;
float m_brake;
float m_clutch;
float m_gear;
float m_gforce_lat;
float m_gforce_lon;
float m_lap;
float m_engineRate;
float m_sli_pro_native_support; // SLI Pro support
float m_car_position; // car race position
float m_kers_level; // kers energy left
float m_kers_max_level; // kers maximum energy
float m_drs; // 0 = off, 1 = on
float m_traction_control; // 0 (off) - 2 (high)
float m_anti_lock_brakes; // 0 (off) - 1 (on)
float m_fuel_in_tank; // current fuel mass
float m_fuel_capacity; // fuel capacity
float m_in_pits; // 0 = none, 1 = pitting, 2 = in pit area
float m_sector; // 0 = sector1, 1 = sector2, 2 = sector3
float m_sector1_time; // time of sector1 (or 0)
float m_sector2_time; // time of sector2 (or 0)
float m_brakes_temp[4]; // brakes temperature (centigrade)
float m_tyres_pressure[4]; // tyres pressure PSI
float m_team_info; // team ID
float m_total_laps; // total number of laps in this race
float m_track_size; // track size meters
float m_last_lap_time; // last lap time
float m_max_rpm; // cars max RPM, at which point the rev limiter will kick in
float m_idle_rpm; // cars idle RPM
float m_max_gears; // maximum number of gears
float m_sessionType; // 0 = unknown, 1 = practice, 2 = qualifying, 3 = race
float m_drsAllowed; // 0 = not allowed, 1 = allowed, -1 = invalid / unknown
float m_track_number; // -1 for unknown, 0-21 for tracks
float m_vehicleFIAFlags; // -1 = invalid/unknown, 0 = none, 1 = green, 2 = blue, 3 = yellow, 4 = red
float m_era; // era, 2017 (modern) or 1980 (classic)
float m_engine_temperature; // engine temperature (centigrade)
float m_gforce_vert; // vertical g-force component
float m_ang_vel_x; // angular velocity x-component
float m_ang_vel_y; // angular velocity y-component
float m_ang_vel_z; // angular velocity z-component
byte m_tyres_temperature[4]; // tyres temperature (centigrade)
byte m_tyres_wear[4]; // tyre wear percentage
byte m_tyre_compound; // compound of tyre – 0 = ultra soft, 1 = super soft, 2 = soft, 3 = medium, 4 = hard, 5 = inter, 6 = wet
byte m_front_brake_bias; // front brake bias (percentage)
byte m_fuel_mix; // fuel mix - 0 = lean, 1 = standard, 2 = rich, 3 = max
byte m_currentLapInvalid; // current lap invalid - 0 = valid, 1 = invalid
byte m_tyres_damage[4]; // tyre damage (percentage)
byte m_front_left_wing_damage; // front left wing damage (percentage)
byte m_front_right_wing_damage; // front right wing damage (percentage)
byte m_rear_wing_damage; // rear wing damage (percentage)
byte m_engine_damage; // engine damage (percentage)
byte m_gear_box_damage; // gear box damage (percentage)
byte m_exhaust_damage; // exhaust damage (percentage)
byte m_pit_limiter_status; // pit limiter status – 0 = off, 1 = on
byte m_pit_speed_limit; // pit speed limit in mph
float m_session_time_left; // NEW: time left in session in seconds
byte m_rev_lights_percent; // NEW: rev lights indicator (percentage)
byte m_is_spectating; // NEW: whether the player is spectating
byte m_spectator_car_index; // NEW: index of the car being spectated
// Car data
byte m_num_cars; // number of cars in data
byte m_player_car_index; // index of player's car in the array
CarUDPData m_car_data[20]; // data for all cars on track
float m_yaw; // NEW (v1.8)
float m_pitch; // NEW (v1.8)
float m_roll; // NEW (v1.8)
float m_x_local_velocity; // NEW (v1.8) Velocity in local space
float m_y_local_velocity; // NEW (v1.8) Velocity in local space
float m_z_local_velocity; // NEW (v1.8) Velocity in local space
float m_susp_acceleration[4]; // NEW (v1.8) RL, RR, FL, FR
float m_ang_acc_x; // NEW (v1.8) angular acceleration x-component
float m_ang_acc_y; // NEW (v1.8) angular acceleration y-component
float m_ang_acc_z; // NEW (v1.8) angular acceleration z-component
};
CarUDPData:
struct CarUDPData
{
float m_worldPosition[3]; // world co-ordinates of vehicle
float m_lastLapTime;
float m_currentLapTime;
float m_bestLapTime;
float m_sector1Time;
float m_sector2Time;
float m_lapDistance;
byte m_driverId;
byte m_teamId;
byte m_carPosition; // UPDATED: track positions of vehicle
byte m_currentLapNum;
byte m_tyreCompound; // compound of tyre – 0 = ultra soft, 1 = super soft, 2 = soft, 3 = medium, 4 = hard, 5 = inter, 6 = wet
byte m_inPits; // 0 = none, 1 = pitting, 2 = in pit area
byte m_sector; // 0 = sector1, 1 = sector2, 2 = sector3
byte m_currentLapInvalid; // current lap invalid - 0 = valid, 1 = invalid
byte m_penalties; // NEW: accumulated time penalties in seconds to be added
};
If it helps, I am at the start of a Grand Prix (not career), racing as Lewis Hamilton at Australia and the starting lights have not gone off yet.
Here is a link to the UDP data specifications for F1 2017.