Re: byte orders question
On Nov 26, 2011, at 4:23 PM, Kyle Sluder wrote: On Sat, Nov 26, 2011 at 4:17 PM, Greg Guerin glgue...@amug.org wrote: Since you're just doing a memcpy(), you can simply cast the bits and avoid the copying. Try this: float f = *((float*) res); Or try defining a C union: union foo { float f; u_int32_t u; }; union foo bar; bar.u = CFSwapInt32HostToBig(value); float f = bar.f; Neither of these is legal. Not true. The union solution is perfectly legal, and doesn't involve pointer aliasing. You are not allowed to alias a pointer to two different types (except pointer-to-char). See Section 6.5, paragraph 7 of the C99 standard: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf See footnote 82: If the member used to access the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called type punning). This might be a trap representation. -- Clark S. Cox III clarkc...@gmail.com smime.p7s Description: S/MIME cryptographic signature ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Sat, 26 Nov 2011 16:23:47 -0800, Kyle Sluder said: Since you're just doing a memcpy(), you can simply cast the bits and avoid the copying. Try this: float f = *((float*) res); Or try defining a C union: union foo { float f; u_int32_t u; }; union foo bar; bar.u = CFSwapInt32HostToBig(value); float f = bar.f; Neither of these is legal. You are not allowed to alias a pointer to two different types (except pointer-to-char). See Section 6.5, paragraph 7 of the C99 standard: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf (My reply is late, but...) Strangely, there is no CF version, but there is NSSwapBigDoubleToHost() and variants, which are more expressive that the trickery above. As Kyle says, don't cast float* to int* (or variations), you see everyone doing it, but it's illegal. The union strategy is dubious too, but in practice safer. Looking in NSByteOrder.h you see it is the technique used by NSSwapBigDoubleToHost() and variants. Cheers, -- Sean McBride, B. Eng s...@rogue-research.com Rogue Researchwww.rogue-research.com Mac Software Developer Montréal, Québec, Canada ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Dec 8, 2011, at 8:45 AM, Sean McBride wrote: The union strategy is dubious too, but in practice safer. It's neither illegal nor dubious. It's explicitly allowed. -- Scott Ribe scott_r...@elevated-dev.com http://www.elevated-dev.com/ (303) 722-0567 voice ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 27, 2011, at 12:55 AM, Charles Srstka wrote: On Nov 26, 2011, at 5:44 PM, Koen van der Drift wrote: Another thing that might be a good idea for general safety reasons is to have f be a Float32 instead of a regular float type. This will only work if we can safely assume the sender is using 4 byte floats. This cannot be guaranteed, since the size of floats is platform dependend. So, *if possible* I would recommend to send these floats as decimal strings and then convert these number strings to whatever math type appropriate on the receiver site. There is no base64 encoding/decoding required, too. Since the posted code is obviously not designed to be efficient, the extra cost for string to double/float conversions can probably be accepted or can even be compensated by a more efficient math type on the receiver site when performing computations. Although I don’t know how likely it is, it’s probably theoretically possible that the float data type could eventually become something other than 32 bits on some platform, and if that happened, doing a memcpy of sizeof(float) bytes from a uint32_t would cause garbage data to be written to the float. Yes, this. Andreas Charles ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 28, 2011, at 2:41 AM, Andreas Grosam wrote: On Nov 27, 2011, at 12:55 AM, Charles Srstka wrote: On Nov 26, 2011, at 5:44 PM, Koen van der Drift wrote: Another thing that might be a good idea for general safety reasons is to have f be a Float32 instead of a regular float type. This will only work if we can safely assume the sender is using 4 byte floats. This cannot be guaranteed, since the size of floats is platform dependend. So, *if possible* I would recommend to send these floats as decimal strings and then convert these number strings to whatever math type appropriate on the receiver site. There is no base64 encoding/decoding required, too. Since the posted code is obviously not designed to be efficient, the extra cost for string to double/float conversions can probably be accepted or can even be compensated by a more efficient math type on the receiver site when performing computations. He said that the sender isn’t his own code and isn’t something he can change, and that it is apparently sending raw binary data (in Base64 encoding). If you’re sending raw binary data, you’d *better* define the widths of your fields as part of the specification. Charles___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 28, 2011, at 1:41 AM, Andreas Grosam wrote: This will only work if we can safely assume the sender is using 4 byte floats. This cannot be guaranteed, since the size of floats is platform dependend. So, *if possible* I would recommend to send these floats as decimal strings and then convert these number strings to whatever math type appropriate on the receiver site. While possible, it's highly unlikely these days to get data from a platform that doesn't adhere to float is IEE 32-bit and double is IEE 64-bit. Plus conversion to/from strings is likely to be subject to roundoff errors unless you're really careful. -- Scott Ribe scott_r...@elevated-dev.com http://www.elevated-dev.com/ (303) 722-0567 voice ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
He said that the sender isn’t his own code and isn’t something he can change, and that it is apparently sending raw binary data (in Base64 encoding). If you’re sending raw binary data, you’d *better* define the widths of your fields as part of the specification. Charles Just for clarification: the format of the data inside the xml file I am reading is set in stone. The code I posted was an example snippet as listed in the xml scheme documention showing how the data could be processed. In the end, for me the goal was to go from a Base64 encoded string to an array of float values encapsulated in NSNumber objects. At first I focused too much of getting that snippet to work, but after posting here and digesting all of the comments, I finally figured it out. BTW, the precision is also stored in the xml file, in this case it was '32'. Thanks all for the help. - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 26, 2011, at 7:06 PM, Scott Ribe wrote: I think what you had before would work for the byte swap conversion, assuming that your result was an array of floats; CFSwapInt32HostToBig is not really the right call--when it works it is by accident. Thanks again Scott for thinking along. I replaced: res = CFSwapInt32BigToHost(value); with: res = ntohl((u_int32_t) ((u_int32_t *) value)); Again giving the same expected results. Would this be the correct call, or am I still missing something? Thanks, - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 27, 2011, at 6:06 AM, Koen van der Drift wrote: Again giving the same expected results. Would this be the correct call, or am I still missing something? The purpose of casting? I think you need to review a C reference, you're casting a uint32_t to uint32_t * then back to uint32_t. In prior code you were casting something (unsigned char * ???) to uint32_t *, then dereferencing it, which would be a uint32_t, then casting that to a uint32_t. NSData *base64DecodedData = [NSData dataFromBase64String: @Q5YIjESWO5JDlpIbRzMVL0OW=]; int n = [NSData length] / 4; const uint32_t *buf = (const uint32_t *) [NSData bytes]; float *results = (float *) malloc(n * 4); for( int i = 0; i n; ++i ) ((uint32_t *)results)[n] = ntohl( buf[n] ); Two of the casts, of bytes malloc results, are not necessary in plain C, but they're required in C++ and are habit for me ;-) Of course you might want to check that sizeof( float ) is 4, but the prior suggestion to use it in the code in place of 4 is off, because your input will still be 4 bytes I presume, and if your platform float is not 4, then you can't make it work and have to fail. (Of course, if your platform doesn't adhere to IEE floating point standards, and your input does, or vice versa, you're screwed anyway.) You might want to check that the data length is an exact multiple of 4, or rather 8 since you're expecting floats in pairs. And so on... -- Scott Ribe scott_r...@elevated-dev.com http://www.elevated-dev.com/ (303) 722-0567 voice ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 27, 2011, at 10:02 AM, Scott Ribe wrote: The purpose of casting? I think you need to review a C reference, you're casting a uint32_t to uint32_t * then back to uint32_t. In prior code you were casting something (unsigned char * ???) to uint32_t *, then dereferencing it, which would be a uint32_t, then casting that to a uint32_t. That code is not mine, it came straight out of the docs describing the xml scheme, I copied it as is but couldn't get it to work, hence my initial question. I now simplified my code a bit more, I changed: res = ntohl((u_int32_t) ((u_int32_t *) value)); to: res = ntohl(value); You might want to check that the data length is an exact multiple of 4, or rather 8 since you're expecting floats in pairs. And so on... I will do that. Thanks again for your help and suggestions. - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
First of all, thanks all for the input. After thinking about it a lot this morning, I probably should have approached this with a different question :) Starting with a base64 encoded string, how do I obtain a series of float values out of it? The string is obtained from an XML file (using NSXMLParser), and the structure contents of the xml file are defined by a third party; I am only retrieving and processing it. I found a small example, with the expected outcome, that is not too big to post here. The input is (without the quotes): Q5YIjESWO5JDlpIbRzMVL0OW= In here are contained two pairs of float values: 300.066772460938 1201.86157226562 301.141448974609 45845.18359375 Any suggestions how to tackle this? Thanks, - Koen.___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 25, 2011, at 9:38 PM, Koen van der Drift wrote: Here is what I am doing, I read an NSString from the raw data, an xml file using NSXMLParser. This works ok. Then I convert that to an NSData object, and perform base64 decoding No, you're not ;-) You read a string that is presumably already Base64 encoded, then you stuff it into an NSData, then you Base64 encode that, then you decode it, leaving you with the original Base64 encoding intact... Instead of: NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding]; NSString *base64String = [data base64EncodedString]; NSData * base64DecodedData = [NSData dataFromBase64String:base64String]; I think you just need: NSData * base64DecodedData = [NSData dataFromBase64String:string]; The actual byte-swapping code looks OK: for (n = 0 ; n (2 * count) ; n++) { ((u_int32_t *) result)[n] = ntohl((u_int32_t) ((u_int32_t *) dataToConvert)[n]); } Although there's a superfluous cast there that might be a clue that you're just slinging code in without understanding it... There's also probably not much point to using subdataWithRange instead of bytes... -- Scott Ribe scott_r...@elevated-dev.com http://www.elevated-dev.com/ (303) 722-0567 voice ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 26, 2011, at 4:23 PM, Scott Ribe wrote: No, you're not ;-) You read a string that is presumably already Base64 encoded, then you stuff it into an NSData, then you Base64 encode that, then you decode it, leaving you with the original Base64 encoding intact... Instead of: NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding]; NSString *base64String = [data base64EncodedString]; NSData * base64DecodedData = [NSData dataFromBase64String:base64String]; I think you just need: NSData * base64DecodedData = [NSData dataFromBase64String:string]; I think you are right. This works (see below). Thanks for pointing (no pun) that out. The actual byte-swapping code looks OK: for (n = 0 ; n (2 * count) ; n++) { ((u_int32_t *) result)[n] = ntohl((u_int32_t) ((u_int32_t *) dataToConvert)[n]); } Although there's a superfluous cast there that might be a clue that you're just slinging code in without understanding it... I'm pretty sure I messed up something. One of the reasons I posted it here :) After spitting through the internets, what I am using now is this: NSData *base64DecodedData = [NSData dataFromBase64String: @Q5YIjESWO5JDlpIbRzMVL0OW=]; for (NSInteger n = 0; n 4; n++) { u_int32_t value; [base64DecodedData getBytes: value range: NSMakeRange( n*4, sizeof( u_int32_t ) )]; u_int32_t result = CFSwapInt32HostToBig( value ); NSLog(@%u, result); } This gives: 1133906060 1150696338 1133941275 1194530095 This correspond to the values I am getting using the unpack function in a Perl script that does what I need, so I think I am on the right track. The perl script continues with $float = unpack(f, pack(I, $i));, where $i is one of the four numbers above. This gives the expected values for the floats. So I may not need the ntohl() function at all. Now I need to figure out how to go from u_int_32 to float. - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 26, 2011, at 6:17 PM, Koen van der Drift wrote: Now I need to figure out how to go from u_int_32 to float. I think I figured it out: for (NSInteger n = 0; n 4; n++) { u_int32_t value; [base64DecodedData getBytes:value range:NSMakeRange(n*4, sizeof(u_int32_t))]; u_int32_t res = CFSwapInt32HostToBig(value); float f; memcpy(f, res, sizeof(f)); NSLog(@%f, f); } This gives the expected four float values. Does this look ok, or did I overlook something? Thanks, - Koen.___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
If the source code that is sending you the data in the first place is your own code, you could have that code use the CFConvertFloat32ToSwapped() before sending it, and then your client can use CFConvertFloat32SwappedToHost() to convert it back, nice and easily. At any rate, HostToBig is not what you want in the client, since you’re swapping it *to* the host byte order, not *from* it. CFSwapInt32BigToHost() would be more correct in that case, even though the two functions will both do the same thing in practice on a little-endian machine. Charles On Nov 26, 2011, at 5:24 PM, Koen van der Drift wrote: On Nov 26, 2011, at 6:17 PM, Koen van der Drift wrote: Now I need to figure out how to go from u_int_32 to float. I think I figured it out: for (NSInteger n = 0; n 4; n++) { u_int32_t value; [base64DecodedData getBytes:value range:NSMakeRange(n*4, sizeof(u_int32_t))]; u_int32_t res = CFSwapInt32HostToBig(value); float f; memcpy(f, res, sizeof(f)); NSLog(@%f, f); } This gives the expected four float values. Does this look ok, or did I overlook something? Thanks, - Koen.___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/cocoadev%40charlessoft.com This email sent to cocoa...@charlessoft.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 26, 2011, at 6:37 PM, Charles Srstka wrote: If the source code that is sending you the data in the first place is your own code, you could have that code use the CFConvertFloat32ToSwapped() before sending it, and then your client can use CFConvertFloat32SwappedToHost() to convert it back, nice and easily. No, it is not my code, and I am reading data from a document, there is no server-client interaction. At any rate, HostToBig is not what you want in the client, since you’re swapping it *to* the host byte order, not *from* it. CFSwapInt32BigToHost() would be more correct in that case, even though the two functions will both do the same thing in practice on a little-endian machine. Thanks for pointing that. They indeed give the same result. - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 26, 2011, at 5:44 PM, Koen van der Drift wrote: At any rate, HostToBig is not what you want in the client, since you’re swapping it *to* the host byte order, not *from* it. CFSwapInt32BigToHost() would be more correct in that case, even though the two functions will both do the same thing in practice on a little-endian machine. Thanks for pointing that. They indeed give the same result. Another thing that might be a good idea for general safety reasons is to have f be a Float32 instead of a regular float type. Although I don’t know how likely it is, it’s probably theoretically possible that the float data type could eventually become something other than 32 bits on some platform, and if that happened, doing a memcpy of sizeof(float) bytes from a uint32_t would cause garbage data to be written to the float. Charles___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 26, 2011, at 4:17 PM, Koen van der Drift wrote: I'm pretty sure I messed up something. One of the reasons I posted it here :) After spitting through the internets, what I am using now is this: NSData *base64DecodedData = [NSData dataFromBase64String: @Q5YIjESWO5JDlpIbRzMVL0OW=]; for (NSInteger n = 0; n 4; n++) { u_int32_t value; [base64DecodedData getBytes: value range: NSMakeRange( n*4, sizeof( u_int32_t ) )]; u_int32_t result = CFSwapInt32HostToBig( value ); NSLog(@%u, result); } This gives: 1133906060 1150696338 1133941275 1194530095 This correspond to the values I am getting using the unpack function in a Perl script that does what I need, so I think I am on the right track. The perl script continues with $float = unpack(f, pack(I, $i));, where $i is one of the four numbers above. This gives the expected values for the floats. So I may not need the ntohl() function at all. Now I need to figure out how to go from u_int_32 to float. I think what you had before would work for the byte swap conversion, assuming that your result was an array of floats; CFSwapInt32HostToBig is not really the right call--when it works it is by accident. -- Scott Ribe scott_r...@elevated-dev.com http://www.elevated-dev.com/ (303) 722-0567 voice ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
Koen van der Drift wrote: u_int32_t value; [base64DecodedData getBytes:value range:NSMakeRange (n*4, sizeof(u_int32_t))]; u_int32_t res = CFSwapInt32HostToBig(value); float f; memcpy(f, res, sizeof(f)); NSLog(@%f, f); Since you're just doing a memcpy(), you can simply cast the bits and avoid the copying. Try this: float f = *((float*) res); Or try defining a C union: union foo { float f; u_int32_t u; }; union foo bar; bar.u = CFSwapInt32HostToBig(value); float f = bar.f; -- GG ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Sat, Nov 26, 2011 at 4:17 PM, Greg Guerin glgue...@amug.org wrote: Since you're just doing a memcpy(), you can simply cast the bits and avoid the copying. Try this: float f = *((float*) res); Or try defining a C union: union foo { float f; u_int32_t u; }; union foo bar; bar.u = CFSwapInt32HostToBig(value); float f = bar.f; Neither of these is legal. You are not allowed to alias a pointer to two different types (except pointer-to-char). See Section 6.5, paragraph 7 of the C99 standard: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
byte orders question
I'm trying to implement the following code into my Cocoa project: for (n = 0 ; n (2 * count) ; n++) { ((u_int32_t *) result)[n] = ntohl((u_int32_t) ((u_int32_t *) decoded)[n]); } with the following requirements: // byte order correction decoded has network byte order data // result has the byte order of the host machine decoded is an NSData object after base64 decoding. How do I obtain the network byte order data and byte order of the host machine so I get the correct results? Thanks, - Koen.___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 25, 2011, at 1:58 PM, Koen van der Drift wrote: I'm trying to implement the following code into my Cocoa project: for (n = 0 ; n (2 * count) ; n++) { ((u_int32_t *) result)[n] = ntohl((u_int32_t) ((u_int32_t *) decoded)[n]); } with the following requirements: // byte order correction decoded has network byte order data // result has the byte order of the host machine decoded is an NSData object after base64 decoding. How do I obtain the network byte order data and byte order of the host machine so I get the correct results? Network byte order data is always big-endian. The byte order of the host machine varies depending on the CPU architecture. The preprocessor macro __BIG_ENDIAN__ is always defined on big-endian architectures (e.g. PowerPC), and __LITTLE_ENDIAN__ on little-endian architectures (e.g. X86), but I think what you want to do here is just use the ntoh family of functions as you are doing above, because they will return the correct byte order for the host machine. IOW, they just return the number passed as arguments to them on big-endian CPUs, and they return a reversed byte order number on little-endian CPUs. Nick Zitzmann http://www.chronosnet.com/ ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On 25 Nov 2011, at 12:58 pm, Koen van der Drift wrote: How do I obtain the network byte order data and byte order of the host machine so I get the correct results? Check out NSHostByteOrder(), as well as CFSwapInt32HostToBig() and brethren (with various values for 32 and Big). -b -- Ben Kennedy, chief magician Zygoat Creative Technical Services http://www.zygoat.ca ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 25, 2011, at 4:30 PM, Ben Kennedy wrote: On 25 Nov 2011, at 12:58 pm, Koen van der Drift wrote: How do I obtain the network byte order data and byte order of the host machine so I get the correct results? Check out NSHostByteOrder(), as well as CFSwapInt32HostToBig() and brethren (with various values for 32 and Big). Thanks Ben. So I added long byteOrder = NSHostByteOrder(); and in my case that returns 1. How do I use that value for my results array? What type should this array be? For now I have been using float *results But I guess that needs to be changed based on the value of byteOrder ? Basically what I am trying to do is to convert and NSData object which I know contains an array of pairs of values to a Cocoa usable object, eg an NSArray of NSDictionaries. - Koen.___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
And to clarify it a bit more, in Perl it is done as follows: use MIME::Base64; $base64decoded = decode_base64($inputstring); @hostOrder32 = unpack(N*, $base64decoded); The hostOrder32 array contains a list of host ordered 32 bits entities which need to be converted to floats. - Koen. On Nov 25, 2011, at 5:20 PM, Koen van der Drift wrote: On Nov 25, 2011, at 4:30 PM, Ben Kennedy wrote: On 25 Nov 2011, at 12:58 pm, Koen van der Drift wrote: How do I obtain the network byte order data and byte order of the host machine so I get the correct results? Check out NSHostByteOrder(), as well as CFSwapInt32HostToBig() and brethren (with various values for 32 and Big). Thanks Ben. So I added long byteOrder = NSHostByteOrder(); and in my case that returns 1. How do I use that value for my results array? What type should this array be? For now I have been using float *results But I guess that needs to be changed based on the value of byteOrder ? Basically what I am trying to do is to convert and NSData object which I know contains an array of pairs of values to a Cocoa usable object, eg an NSArray of NSDictionaries. - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 25, 2011, at 3:20 PM, Koen van der Drift wrote: Basically what I am trying to do is to convert and NSData object which I know contains an array of pairs of values to a Cocoa usable object, eg an NSArray of NSDictionaries. As another response suggested, what's wrong with ntohl??? -- Scott Ribe scott_r...@elevated-dev.com http://www.elevated-dev.com/ (303) 722-0567 voice ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 25, 2011, at 5:44 PM, Scott Ribe wrote: As another response suggested, what's wrong with ntohl??? I implemented that function based on some code I found online, but got very weird results, which is why I asked here. My knowledge on these esoteric functions apparently isn't up to par :) As long as I end up with an array of x,y values, I really don't care how I get there. Eventually they will need to end up in an NSArray for further processing though. - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 25, 2011, at 2:56 PM, Koen van der Drift wrote: On Nov 25, 2011, at 5:44 PM, Scott Ribe wrote: As another response suggested, what's wrong with ntohl??? I implemented that function based on some code I found online, but got very weird results, which is why I asked here. My knowledge on these esoteric functions apparently isn't up to par :) As long as I end up with an array of x,y values, I really don't care how I get there. Eventually they will need to end up in an NSArray for further processing though. That function is already implemented on every machine that support networking -- including Mac OS X. You don't need to implement it yourself -- just #include arpa/inet.h Do a man ntohl for more info. -- Glenn L. Austin, Computer Wizard and Race Car Driver Where there's breath, there's hope! http://www.austin-soft.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: byte orders question
On Nov 25, 2011, at 7:07 PM, Glenn L. Austin wrote: On Nov 25, 2011, at 2:56 PM, Koen van der Drift wrote: On Nov 25, 2011, at 5:44 PM, Scott Ribe wrote: As another response suggested, what's wrong with ntohl??? I implemented that function based on some code I found online, but got very weird results, which is why I asked here. My knowledge on these esoteric functions apparently isn't up to par :) As long as I end up with an array of x,y values, I really don't care how I get there. Eventually they will need to end up in an NSArray for further processing though. That function is already implemented on every machine that support networking -- including Mac OS X. You don't need to implement it yourself -- just #include arpa/inet.h Do a man ntohl for more info. I read that but am still confused :( Here is what I am doing, I read an NSString from the raw data, an xml file using NSXMLParser. This works ok. Then I convert that to an NSData object, and perform base64 decoding (using http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html): NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding]; NSString *base64String = [data base64EncodedString]; NSData * base64DecodedData = [NSData dataFromBase64String:base64String]; I then retrieve chunks of 32 bytes as follows: NSRange range = {n, 32}; NSData *chunk = [base64DecodedData subdataWithRange:range]; But I am struggling with how to use this with ntohl, similar to the code I am trying to implement: for (n = 0 ; n (2 * count) ; n++) { ((u_int32_t *) result)[n] = ntohl((u_int32_t) ((u_int32_t *) dataToConvert)[n]); } Any help appreciated. I am happy to send the raw string off-list if anyone wants to give it a try. The end results should give pairs of floats: 300.066772460938 1201.86157226562 301.141448974609 45845.18359375 301.215972900391 1430.0322265625 etc Thanks, - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com