hi there i need to parse the byte data of a response from api the data is
[0, 2, 0, 44, 0, 6, 58, 1, 0, 1, 109, 85, 0, 0, 0, 1, 0, 1, 111, 97, 0, 115, 224, 4, 0, 0, 0, 0, 0, 0, 1, 114, 0, 1, 115, 169, 0, 1, 116, 18, 0, 1, 108, 121, 0, 1, 113, 241, 0, 44, 0, 13, 128, 1, 0, 0, 55, 200, 0, 0, 0, 10, 0, 0, 55, 227, 5, 172, 149, 3, 0, 0, 84, 154, 0, 0, 0, 0, 0, 0, 56, 79, 0, 0, 57, 28, 0, 0, 54, 226, 0, 0, 56, 89]
there are certain rules to parse the data according to api provider as below
A The first two bytes ([0 - 2] -- SHORT or int16) represent the number of packets in the message.
B The next two bytes ([2 - 4] -- SHORT or int16) represent the length (number of bytes) of the first packet.
C The next series of bytes ([4 - 4+B]) is the quote packet.
D The next two bytes ([4+B - 4+B+2] -- SHORT or int16) represent the length (number of bytes) of the second packet.
C The next series of bytes ([4+B+2 - 4+B+2+D]) is the next quote packet.
please someone help me how to parse this data according to these rules in dart. i am stuck thanks for help
First, you need to convert the list of integers into a list of bytes:
final byteList = Uint8List.fromList(responseList);
Then you need to create a ByteData from that byte list:
final byteData = ByteData.view(byteList.buffer);
Then you can do your parsing of various bytes, shorts, ints, or longs that you want at various byte offsets. For example:
final packetNum = byteData.getUint16(0);
final firstPacketLength = byteData.getUint16(2);
final firstPacketView = ByteData.sublistView(byteData, 4, 4 + firstPacketLength);
// Do whatever you need for that packet
final secondPacketPos = 4 + firstPacketLength;
final secondPacketLength = byteData.getUint16(secondPacketPos),
final secondPacketView = ByteData.sublistView(byteData, secondPacketPos + 2, 4 + secondPacketLength);
// Do whatever you need for the saecond packet
Related
Hey all i imagine this could have been answered however I can not find what i'm looking for exactly. Here is the code bellow:
positiveData = np.array([])
negativeData = []
with AedatFile('someFile') as f:
# loop through the "frames" stream
for e in f['events'].numpy():
for event in e:
time, x, y, polarity, _, _ = event
if polarity == 1:
data = np.array([time, x, y, polarity])
print(data)
positiveData = np.append(positiveData,data)
print(positiveData)
else:
data = [time, x, y, polarity]
negativeData.append(data)
I am expecting the code to look like this:
[[1,2,3,4],
[1,2,3,4],
....]
I plan on using this to make a 3d plot so i want an array so i can easily plot3d(array[0][:],array[1][:],array[2][:])
cheers all.
Here is the a sample set of data that was asked for below. I cant paste more as it says my comment is mostly code and wont allow me to post more without adding more text which incredibly stupid.
[(1612584805989190, 254, 304, 1, 0, 0)
(1612584805989190, 254, 283, 1, 0, 0)
(1612584805989190, 254, 286, 1, 0, 0) ...
(1612584805999148, 596, 20, 1, 0, 0)
(1612584805999162, 549, 60, 1, 0, 0)
(1612584805999189, 461, 225, 0, 0, 0)]
[(1612584806009235, 512, 31, 1, 0, 0)
(1612584806009263, 419, 274, 1, 0, 0)
(1612584806009287, 338, 188, 0, 0, 0) ...
(1612584806019188, 214, 241, 0, 0, 0)
(1612584806019188, 214, 237, 0, 0, 0)
(1612584806019189, 211, 234, 0, 0, 0)]
Try modifying to
positiveData = np.append( positiveData, [data] )
or
data = np.array( [[ time, x, y, polarity ]] )
The append function will append (embed) the array given to inside the array target
https://numpy.org/doc/stable/reference/generated/numpy.append.html
I'm trying to use reqwest library to download a PNG file, but when I download it I see a strange behaviour respect other programming languages like: Python.
For instance:
let content = reqwest::get("https://www.google.es/images/searchbox/desktop_searchbox_sprites302_hr.png").await?;
If I print the result as a bytes array (println!("{:?}", content.text().await?.as_bytes());
[ 191, 189, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 40, 0, 0, 0, 82, 8, 3, 0, 0, 0, 17, 191, 189, 102, 191, 189, 0, 0, 0, 108, 80, 76, 84, 69, 0, 0, 0, 191, 189, 191, 189, 191, 189,...]
However, the result using Python requests is:
[137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 40, 0, 0, 0, 82, 8, 3, 0, 0, 0, 17, 153, 102, 248, ...]
In the Rust version, I see a lot of 191, 189. This sequence repeats a lot throughout the array, but in Python doesn't appear at all.
What am I doing wrong in Rust?
I see a lot of 191, 189
It's better seen as EF, BF, BD, which is the Unicode replacement character encoded as UTF-8. Binary data is not text data. You should not use text for binary data, instead use bytes.
const URL: &str = "https://www.google.es/images/searchbox/desktop_searchbox_sprites302_hr.png";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let content = reqwest::get(URL).await?;
let bytes = content.bytes().await?;
println!("{:x?}", &bytes[..]);
Ok(())
}
[89, 50, 4e, 47, d, a, 1a, a, 0, 0, 0, d, 49, 48, 44, 52, 0, 0, 0, 28, 0, 0, 0, 52, 8, 3, 0, 0, 0, 11, 99, 66, f8, 0, 0, 0, 6c, 50, 4c, 54, 45, 0, 0, 0, 9f, ...
I have a cube that contains the probability density based on 3 variables of the shape [t1,t2,gamma] with 80 values in each direction. I need to plot the distribution of T, gamma. Where T is t2-t1.
Is there some clever way to collapse this cube into the desired result? I've been breaking my head on it and I can't find one.
You can pad with zeros and then shear the array by incrementing or decrementing a dimension by one:
# make small diagnostic example
nt1, nt2, ngm = 4, 5, 2
data = sum(np.ogrid[1:nt1+1,-1:-nt2-1:-1,100:100*ngm+100:100])
# by construction values are equal if coordinates (T,gamma) are equal, no matter how T = t2-t1 decomposes.
# Fixing gamma, for example at 1, we can see that T is constant along the diagonals
data[..., 1]
# array([[200, 199, 198, 197, 196],
# [201, 200, 199, 198, 197],
# [202, 201, 200, 199, 198],
# [203, 202, 201, 200, 199]])
# now let's transform the example, first recover dimensions
nt1, nt2, ngm = data.shape
# next, zero pad
aux = np.zeros((nt1+2, nt1+nt2-2, ngm), data.dtype)
aux[1:-1, :nt2] = data
# and shear, in this case by incrementing dimension 1
sheared = aux.reshape(-1, ngm)[nt2-1:3-nt1-nt2].reshape(nt1, nt1+nt2-1, ngm)
# check result, for example at gamma = 1
sheared[..., 1]
# array([[ 0, 0, 0, 200, 199, 198, 197, 196],
# [ 0, 0, 201, 200, 199, 198, 197, 0],
# [ 0, 202, 201, 200, 199, 198, 0, 0],
# [203, 202, 201, 200, 199, 0, 0, 0]])
# corresponding values of T are now aligned and ready for further processing.
I'm trying to convert a string from a file into a byte array to send over pyserial in python 3
I have a string like:
[66, 1, 32, 1, 3, 0, 0, 11, 0, 1, 4, 102, 198]
which I need to send over the line as:
\x42\x01\x20\x01\x03\x00\x00\x0b\x00\x01\x04\x66\xc6
I've tried many things without much success.
Any help is appreciated
You might do
data = [66, 1, 32, 1, 3, 0, 0, 11, 0, 1, 4, 102, 198]
a = bytearray()
for item in data:
a.append(item)
byte_str = bytes(a)
Output:
b'B\x01 \x01\x03\x00\x00\x0b\x00\x01\x04f\xc6'
I have the following problem. I need to change the shape of one Numpy array to match the shape of another Numpy array by adding rows and columns.
Let's say this is the array that needs to be changed:
change_array = np.random.rand(150, 120)
And this is the reference array:
reference_array = np.random.rand(200, 170)
To match the shapes I'm adding rows and columns containing zeros, using the following function:
def match_arrays(change_array, reference_array):
cols = np.zeros((change_array.shape[0], (reference_array.shape[1] - change_array.shape[1])), dtype=np.int8)
change_array = np.append(change_array, cols, axis=1)
rows = np.zeros(((reference_array.shape[0] - change_array.shape[0]), reference_array.shape[1]), dtype=np.int8)
change_array = np.append(change_array, rows, axis=0)
return change_array
Which perfectly works and changes the shape of change_array to the shape of reference_array. However, using this method, the array needs to be copied twice in memory. I understand how Numpy needs to make a copy of the array in memory in order to have space to append the rows and columns.
As my arrays can get very large I am looking for another, more memory efficient method, that can achieve the same result. Thanks!
Here are a couple ways. In the code examples, I'll use the following arrays:
In [190]: a
Out[190]:
array([[12, 11, 15],
[16, 15, 10],
[16, 12, 13],
[11, 19, 10],
[12, 12, 11]])
In [191]: b
Out[191]:
array([[70, 82, 83, 93, 97, 55],
[50, 86, 53, 75, 75, 69],
[60, 50, 76, 52, 72, 88],
[72, 79, 66, 93, 58, 58],
[57, 92, 71, 97, 91, 50],
[60, 77, 67, 91, 91, 63],
[60, 90, 91, 50, 86, 71]])
Use numpy.pad:
In [192]: np.pad(a, [(0, b.shape[0] - a.shape[0]), (0, b.shape[1] - a.shape[1])], 'constant')
Out[192]:
array([[12, 11, 15, 0, 0, 0],
[16, 15, 10, 0, 0, 0],
[16, 12, 13, 0, 0, 0],
[11, 19, 10, 0, 0, 0],
[12, 12, 11, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0]])
Or, use a more efficient version of your function, in which the result is preallocated as an array of zeros with the same shape as reference_array, and then the values in change_array are copied into the result:
In [193]: def match_arrays(change_array, reference_array):
...: result = np.zeros(reference_array.shape, dtype=change_array.dtype)
...: nrows, ncols = change_array.shape
...: result[:nrows, :ncols] = change_array
...: return result
...:
In [194]: match_arrays(a, b)
Out[194]:
array([[12, 11, 15, 0, 0, 0],
[16, 15, 10, 0, 0, 0],
[16, 12, 13, 0, 0, 0],
[11, 19, 10, 0, 0, 0],
[12, 12, 11, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0]])