Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Roderick Mann via swift-users
> On Jun 26, 2017, at 14:05 , Philippe Hausler wrote: > > > >> On Jun 26, 2017, at 1:47 PM, Roderick Mann wrote: >> >>> >>> On Jun 26, 2017, at 10:20 , Charles Srstka wrote: >>> >>> Rats, I was hoping that one of the

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Charles Srstka via swift-users
> On Jun 26, 2017, at 3:47 PM, Roderick Mann wrote: > >> On Jun 26, 2017, at 10:20 , Charles Srstka wrote: >> >> Rats, I was hoping that one of the reasons about being so explicit what >> we’re going to access and where with bindMemory() and

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Philippe Hausler via swift-users
> On Jun 26, 2017, at 1:47 PM, Roderick Mann wrote: > >> >> On Jun 26, 2017, at 10:20 , Charles Srstka > > wrote: >> >> Rats, I was hoping that one of the reasons about being so explicit what >> we’re going

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Roderick Mann via swift-users
> On Jun 26, 2017, at 10:20 , Charles Srstka wrote: > > Rats, I was hoping that one of the reasons about being so explicit what we’re > going to access and where with bindMemory() and friends would be to take care > of these sorts of issues. > > In that case, the

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Charles Srstka via swift-users
> On Jun 26, 2017, at 3:41 PM, Roderick Mann wrote: > >> >> On Jun 26, 2017, at 10:20 , Charles Srstka > > wrote: >> >> Rats, I was hoping that one of the reasons about being so explicit what >> we’re going to

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Roderick Mann via swift-users
> On Jun 26, 2017, at 10:20 , Charles Srstka wrote: > > Rats, I was hoping that one of the reasons about being so explicit what we’re > going to access and where with bindMemory() and friends would be to take care > of these sorts of issues. > > In that case, the

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Joe Groff via swift-users
> On Jun 26, 2017, at 10:05 AM, Philippe Hausler wrote: > > Data.copyBytes will do that under the hood > > var crc: UInt16 = 0 > let amountCopied = withUnsafeMutablePointer(to: ) { data.copyBytes(to: > UnsafeMutableBufferPointer(start: $0, count: 1)) } > if amountCopied ==

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Charles Srstka via swift-users
> On Jun 26, 2017, at 12:58 PM, Joe Groff wrote: > >> >> On Jun 26, 2017, at 10:20 AM, Charles Srstka via swift-users >> > wrote: >> >> Rats, I was hoping that one of the reasons about being so explicit what >> we’re

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Joe Groff via swift-users
> On Jun 26, 2017, at 10:20 AM, Charles Srstka via swift-users > wrote: > > Rats, I was hoping that one of the reasons about being so explicit what we’re > going to access and where with bindMemory() and friends would be to take care > of these sorts of issues. There

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Philippe Hausler via swift-users
> On Jun 26, 2017, at 10:20 AM, Charles Srstka wrote: > > Rats, I was hoping that one of the reasons about being so explicit what we’re > going to access and where with bindMemory() and friends would be to take care > of these sorts of issues. > > In that case, the

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Charles Srstka via swift-users
Rats, I was hoping that one of the reasons about being so explicit what we’re going to access and where with bindMemory() and friends would be to take care of these sorts of issues. In that case, the simplest way to do it is probably just this: let crc = (UInt16(myData[myData.endIndex]) << 8)

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Philippe Hausler via swift-users
Data.copyBytes will do that under the hood var crc: UInt16 = 0 let amountCopied = withUnsafeMutablePointer(to: ) { data.copyBytes(to: UnsafeMutableBufferPointer(start: $0, count: 1)) } if amountCopied == MemoryLayout.size { // we have a full crc } That will probably do what you want; plus

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Joe Groff via swift-users
> On Jun 26, 2017, at 1:55 AM, Daniel Vollmer via swift-users > wrote: > > Hi Rick, > >> On 26. Jun 2017, at 02:37, Rick Mann via swift-users >> wrote: > > [snip] > >> I'd also like to avoid unnecessary copying of the data. All of it is >>

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Charles Srstka via swift-users
> On Jun 26, 2017, at 3:55 AM, Daniel Vollmer via swift-users > wrote: > > Hi Rick, > >> On 26. Jun 2017, at 02:37, Rick Mann via swift-users > > wrote: > > [snip] > >> I'd also like to avoid unnecessary copying of

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Roderick Mann via swift-users
Thanks, I'll give that a try. > On Jun 25, 2017, at 23:11 , Charles Srstka wrote: > >> On Jun 26, 2017, at 12:00 AM, Roderick Mann via swift-users >> wrote: >> >> I mean, it's as straightforward as my example. I have a Data of arbitrary >>

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Daniel Vollmer via swift-users
Hi Rick, > On 26. Jun 2017, at 02:37, Rick Mann via swift-users > wrote: [snip] > I'd also like to avoid unnecessary copying of the data. All of it is > immutable for the purposes of this problem. > > How can I get the UInt16 that starts at byte X in a Data? Same goes

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Charles Srstka via swift-users
> On Jun 26, 2017, at 12:00 AM, Roderick Mann via swift-users > wrote: > > I mean, it's as straightforward as my example. I have a Data of arbitrary > size (anywhere from 3 to 29 bytes, let's say). The last two bytes form a > UInt16 CRC. I need to get those last two out

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-26 Thread Charles Srstka via swift-users
> On Jun 26, 2017, at 1:11 AM, Charles Srstka via swift-users > wrote: > >> On Jun 26, 2017, at 12:00 AM, Roderick Mann via swift-users >> > wrote: >> >> I mean, it's as straightforward as my example. I have a Data of

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-25 Thread Roderick Mann via swift-users
I mean, it's as straightforward as my example. I have a Data of arbitrary size (anywhere from 3 to 29 bytes, let's say). The last two bytes form a UInt16 CRC. I need to get those last two out and compare them against the CRC I compute for the rest of the bytes. Having said that, I just used

Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-25 Thread Philippe Hausler via swift-users
There are probably a number of ways that would do what you need. I would need a bit more context or examples of what you are doing already to comment. But if I had those parameters to work with I would use copyBytes into the address of the target you are wanting to read. There are some cases

[swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-25 Thread Rick Mann via swift-users
I continue to struggle with the "proper" and most efficient way to do things with Data. In this case, I have a set of bytes received over a serial port in a Data. The last two bytes are a (big- or little-endian) UInt16 CRC. However, there maybe an odd or even number of bytes in the Data before