Re: "byte orders" question

2011-12-13 Thread Clark S. Cox III
On Nov 26, 2011, at 4:23 PM, Kyle Sluder wrote: > On Sat, Nov 26, 2011 at 4:17 PM, Greg Guerin 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 {

Re: "byte orders" question

2011-12-08 Thread Scott Ribe
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

Re: "byte orders" question

2011-12-08 Thread Sean McBride
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

Re: "byte orders" question

2011-11-28 Thread Koen van der Drift
> 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 clari

Re: "byte orders" question

2011-11-28 Thread Scott Ribe
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 th

Re: "byte orders" question

2011-11-28 Thread Charles Srstka
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 t

Re: "byte orders" question

2011-11-28 Thread Andreas Grosam
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 sende

Re: "byte orders" question

2011-11-27 Thread Koen van der Drift
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 wo

Re: "byte orders" question

2011-11-27 Thread Scott Ribe
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. I

Re: "byte orders" question

2011-11-27 Thread Koen van der Drift
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 al

Re: "byte orders" question

2011-11-26 Thread Kyle Sluder
On Sat, Nov 26, 2011 at 4:17 PM, Greg Guerin 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 = CFSwapInt

Re: "byte orders" question

2011-11-26 Thread Greg Guerin
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));

Re: "byte orders" question

2011-11-26 Thread Scott Ribe
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: > @"Q5YIjESWO5JDlpIb

Re: "byte orders" question

2011-11-26 Thread Charles Srstka
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 t

Re: "byte orders" question

2011-11-26 Thread Koen van der Drift
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 > conver

Re: "byte orders" question

2011-11-26 Thread Charles Srstka
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 wh

Re: "byte orders" question

2011-11-26 Thread Koen van der Drift
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*

Re: "byte orders" question

2011-11-26 Thread Koen van der Drift
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: > >>

Re: "byte orders" question

2011-11-26 Thread Scott Ribe
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

Re: "byte orders" question

2011-11-26 Thread Koen van der Drift
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 NS

Re: "byte orders" question

2011-11-25 Thread Koen van der Drift
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,

Re: "byte orders" question

2011-11-25 Thread Glenn L. Austin
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

Re: "byte orders" question

2011-11-25 Thread Koen van der Drift
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

Re: "byte orders" question

2011-11-25 Thread Scott Ribe
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??? -

Re: "byte orders" question

2011-11-25 Thread Koen van der Drift
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

Re: "byte orders" question

2011-11-25 Thread Koen van der Drift
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() a

Re: "byte orders" question

2011-11-25 Thread Ben Kennedy
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

Re: "byte orders" question

2011-11-25 Thread Nick Zitzmann
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 orders" question

2011-11-25 Thread Koen van der Drift
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 h