6655

How to do a reinterpret_cast with Obj-C?

Question:

<strong>Edit:</strong> I am looking for reinterpret_castin Objective-C so the following is meaningless for my intended question.

<blockquote>

Are there static casts in Objective-C?

e.g. in this C++ example static_cast is used for a good reason:

float rnd = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);

I can think of using a union or pointers to directly access the integer value as a float but that would make the code much more complex.

How would I do the same thing as in the C++ example conveniently in Objective-C?

</blockquote>

Answer1:

After the clarification you are after reinterpret_cast:

You are correct you can do it with a union. If you want to do it inline you can use a cast, address-of and indirection...

The C-style cast (type)expr in both (Objective-)C and C++ works similarly: if the equivalent of a static_cast is appropriate it does that, e.g. between int and float and other value types; otherwise it will do the equivalent of a reinterpret_cast, e.g. between pointer-types.

So you just need to convert to a pointer, cast, and indirect. E.g.:

int z = 0xDEADBEEF; float y = *(float *)&z; NSLog(@"%d -> %f", z, y);

Yes it's a bit ugly, but then what you are doing is as well ;-) To make it a bit nicer define it as a macro:

#define REINTERPRET(type, expr) (*(type *)&(expr))

which you can use as:

int z = 0xDEADBEEF; float y = REINTERPRET(float, z); NSLog(@"%d -> %f", z, y);

As with reinterpret_cast, you should use this sparingly and with care!

HTH

Answer2:

Objective C is a superset of C so do this the C way and simple casts

float rnd = (float) (rand()) / (float)(RAND_MAX);

Recommend

  • why is srand required to be called before generating a random number using rand
  • How to get pseudo-random uniformly distributed integers in C good enough for statistical simulation?
  • Failing to link c code to lapack: undefined reference
  • How do I generate totally a random number at a time?
  • Bundled scripts not working MVC
  • How do I stop js files being cached in IE?
  • How to generate random events in android?
  • Yii: any way to save the images in compressed form?
  • css Star-rating html
  • C++ Text File Reading
  • eC (Ecere) how to not worry about private data fields of a class
  • including Python.h in C++ file CDT
  • Cast boost::array for function with argument (const float (&arr)[12])
  • Linq Full Outer Join on Two Objects
  • How to align an image side by side with a heading element?
  • distinct values from multiple fields within one table ORACLE SQL
  • Inversing an interpolation of rotation
  • How to unpack 32bit integer packed in a QByteArray?
  • Web.config system.webserver errors
  • Z3: Convert between FP and BitVector?
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • How to define and use opencv mat of user type
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Record samples being played with OpenAL
  • Scrapy recursive link crawler
  • Counter field in MS Access, how to generate?
  • AES padding and writing the ciphertext to a disk file
  • Convert array of 8 bytes to signed long in C++
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Apache 2.4 - remove | delete | uninstall
  • 0x202A in filename: Why?
  • Unanticipated behavior
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Benchmarking RAM performance - UWP and C#
  • File not found error Google Drive API
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?