casting

How to cast correctly a struct in C++

Consider a code excerpt below: typedef struct tagTHREADNAME_INFO { DWORD dwType; LPCTSTR szName; DWORD dwThreadID; DWORD dwFlags; } THREADNAME_INFO; const THREADNAME_INFO info = { 0x1000, threadName, CurrentId(), 0}; ::RaiseException(kVCThreadNameException, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info...

Bit conversion operations in PHP

Hello, I find myself in need of performing bit-level conversion on variables in PHP. In more detail, I have a bit stream that is read as an integer by hardware, and I need to do some operations on the bits to make it into what its actually supposed to be (a float). I have to do this a few times for different formats, and the functionali...

Cast or Convert?

I have am getting an error when I try and alter a date column: Arithmetic overflow error for type varchar, value = 20100601.000000. I would like it to go from float to the datetime format of 20100601. Since Alter doesn't work, how can I use cast or convert to change the datatype for every entry in the table? ...

Hex to bin after logical operations

I want: 111 || 100 ---> 111, not 1 100 && 100 ---> 100, not 1 101 && 010 ---> 000, not 0 Broken code #include <stdio.h> main(void){ string hexa = 0xff; strig hexa2 = 0xf1; // CONVERT TO INT??? cast int hexa3 = hexa || hexa2; int hexa4 = hexa && hexa2; puts(hexa3); puts(...

GCC doesn't like C++ style casts with spaces

I am porting some C++ code to GCC, and apperantly it isn't happy with C++ style casting when sapces are involved, as in unsigned int(-1), long long(ShortVar) etc... It gives an error: expected primary-expression before 'long'. Is there any way to make peace with GCC without going over each one of those and rewrite in c-style? ...

Is this casting safe?

I need to write a Util function (in my c++cli app) that converts a String to a Double or Float or Int. template<typename T> static T MyConvert(String^ str) { return static_cast<T>(System::Convert::ToDouble(str)); } Is this safe? Can it somehow convert 2 to 1.999 and then to 1 if I call MyConvert<int>("2") ? I was wondering why th...

Trying to cast a boxed int to byte

Code to illustrate : int i = 5; object obj = i; byte b = (byte)obj; // X When run, this generates a System.InvalidCastException ("Specified cast is not valid") at line "X". Doing a double cast works : byte b = (byte)(int)obj; I would have thought that you ought to be able to cast a boxed int (if it ...

Can I access type int (*)[] with [][]?

Hi coming from this question "What does (int (*)[])var1 stand for?" I tried to access the result of the cast like a multidimensional array. But I get following error: "assignment from incompatible pointer type" followed by a segmentation fault. I tried also some other variations, but none of them worked. How can I access the elements in...

Fastest way in C# to read block of bytes from file and converting to float[]

Hi! I need a fast way in C# leanguage of converting/casting array of bytes encoding one short (int16) value for 2bytes into float representation, as fast as possible. Performance bottleneck was method: samples[sample] = (float)binraryReader.readInt16(); (huge ammount of IO calls so i had to convert to block read) Basically i have fil...

How do I get generics to work with return values that require careful casting?

I've got some data access layer code calls a stored proc and returns scalar values of various datatypes. The syntax is ExecuteDecimal, ExecuteString, etc. I want it to be Execute<string> or Execute<decimal> I try this implementation and I can't compile, unless I'm doing casting using "(T) value", if I try to check the type and call a ...

Why is casting and comparing in PHP faster than is_*?

While optimizing a function in PHP, I changed if(is_array($obj)) foreach($obj as $key=>$value { [snip] } else if(is_object($obj)) foreach($obj as $key=>$value { [snip] } to if($obj == (array) $obj) foreach($obj as $key=>$value { [snip] } else if($obj == (obj) $obj) foreach($obj as $key=>$value { [snip] } After learning about ==...

XName x = "string" ??

I forgot casting mechanism when i encounter it :) . So can u make me remember it again ? Because it looks not acceptable for me at the moment. ...

Can you cast an object to one that implements an interface? (JAVA)

Can you cast an object to one that implements an interface? Right now, I'm building a GUI, and I don't want to rewrite the Confirm/Cancel code (A confirmation pop-up) over and over again. So, what I'm trying to do is write a class that gets passed the class it's used in and tells the class whether or not the user pressed Confirm or Canc...

std::bad_cast from parent to child?

For simplicity, class Parent {} class Child1 : Parent {} class Child2 : Parent {} Elsewhere, I created instances of Child1 and Child2 and store it in same vector under Parent: // . . . in .h file, for example vector<Parent> vector_of_parent; // . . . in one particular method Child1 c1; Child2 c2; vector_of_parent.push_back(c1); ve...

Cast string with coma to float with dot

Hello ! When I try to cast $value = floatval('14,5833'); to a float type I expect a value with dot like 14.5833 but it returns me 14,5833. How should I do this ? I wouldn't like to use any string replace functions. ...

How to develop asp.net web service to create the web method which can take the parameter of type windows form control?

I am developing asp.net web service. I am developing this web service so that OPC ( OLE for process control) client application can use it. In this web service I am using the built-in functions provided by the namespaces using OPC, using OPCDA, using OPCDA.NET. I have also added the namespace using System.Windows.Forms in this web servi...

Non-member conversion functions; Casting different types, e.g. DirectX vector to OpenGL vector

I am currently working on a game "engine" that needs to move values between a 3D engine, a physics engine and a scripting language. Since I need to apply vectors from the physics engine to 3D objects very often and want to be able to control both the 3D, as well as the physics objects through the scripting system, I need a mechanism to c...

IEnumerable.Cast not calling cast overload

I'm not understanding something about the way .Cast works. I have an explicit (though implicit also fails) cast defined which seems to work when I use it "regularly", but not when I try to use .Cast. Why? Here is some compilable code that demonstrates my problem. public class Class1 { public string prop1 { get; set; } public ...

boost::shared_ptr<const T> to boost::shared_ptr<T>

I want to cast the const-ness out of a boost::shared_ptr, but I boost::const_pointer_cast is not the answer. boost::const_pointer_cast wants a const boost::shared_ptr, not a boost::shared_ptr. Let's forego the obligitory 'you shouldn't be doing that'. I know... but I need to do it... so what's the best/easiest way to do it? For clari...

Need help for this syntax: "#define LEDs (char *) 0x0003010"

I'm doing programming of a softcore processor, Nios II from Altera, below is the code in one of the tutorial, I manage to get the code working by testing it on the hardware (DE2 board), however, I could not understand the code. #define Switches (volatile char *) 0x0003000 #define LEDs (char *) 0x0003010 void main() { while (1) *LEDs = *...