type-casting

SQL server 2005 numeric precision loss

Hello, Debugging some finance-related SQL code found a strange issue with numeric(24,8) mathematics precision. Running the following query on your MSSQL you would get A + B * C expression result to be 0.123457 SELECT A, B, C, A + B * C FROM ( SELECT CAST(0.12345678 AS NUMERIC(24,8)) AS A, CAST(0 AS NUMERIC(...

PHP money string conversion to integer error

I have a small financial application with PHP as the front end and MySQL as the back end. I have ancient prejudices, and I store money values in MySQL as an integer of cents. My HTML forms allow input of dollar values, like "156.64" and I use PHP to convert that to cents and then I store the cents in the database. I have a function that...

check if X is derived of Y via typeid

i need to convert pointers to long (SendMessage()) and i want to safely check if the variable is correct on the otherside. So i was thinking of doing dynamic_cast but that wont work on classes that are not virtual. Then i thought of doing typeid but that will work until i pass a derived var as its base. Is there any way to check if the ...

Type Conversion in C#

I'm trying to make a generic method for type conversions which gets an object and the type of object to be cast. By using Convert.ChangeType() I can do what I want, but it takes too much time on run time. What is the best method to make a generic class like I want. My old code looks like that; public static ConvertTo<T>(object data) ...

How do C/C++ compilers handle type casting?

I am curious to know how Type Casting happens without loss of data inside the compiler. For example: int i = 10; UINT k = (UINT) k; float fl = 10.123; UINT ufl = (UINT) fl; // data loss here? char *p = "Stackoverflow Rocks"; unsigned char *up = (unsigned char *) p; How does the compiler handle this type of typecasting. A lo...

How do I check if a string is a number in Python?

What is the best possible way to check if a string can be represented as a number in Python? The function I currently have right now is: def is_number(s): try: float(s) return True except ValueError: return False This seems clunky, but I haven't found a better method because calling float in the main ...

Casting to a Class which is determined at run-time

I have a method fetchObjects(String) that is expected to return an array of Contract business objects. The className parameter tells me what kind of business objects I should return (of course this doesn't make sense in this construed case because I already said I will return Contracts, but it's basically the situation I have in my real ...

Need help-typecasting in Python

Help me people Last week I asked about unicode conversion.I got only one reply.I need more suggestions. I need to convert strings in Python to other types such as unsigned and signed int 8 bits,unsigned and signed int 16 bits,unsigned and signed int 32 bits,unsigned and signed int 64 bits,double,float,string,unsigned and signed 8 bi...

VB6 Can IsNumeric be wrong?

Is it possible to test a string with IsNumeric() and for it to return true, but when you cast that same string to an integer using CInt() and assign it to a variable of type integer that it will give a type mismatch error? I ask because I was getting a type mismatch error, so I used IsNumeric() to check the string was numeric before try...

Casting to Unknown Type When Only Given Class Name as a String of That Type

I currently posses a List of Objects(Using Java 1.3), and let's say that I wanted to cast one of the Objects returned from list.get(i) to a type of which I only know the name of the Class as a String. Essentially, how do I Object o = (classname)list.get(i); where className is a String variable of a className. I thought that I could use...

PHP Array extracting object

Suppose I have an array of a objects of user defined class. Wanted to know how do I extract the elements of the array in PHP. // class definition class User { public $fname; public $lname; } // array of objects of the class defined above $objUser1 = new User(): $objUser2 = new User(): $objUser3 = new User(): $objUser4 = new User(): $a...

Type casting, easy question for people good at C

Hello, i have a library which i have to pass (char **)&return_string to the function hci_scan as seen in this excerpt: char return_string[250]; int num_hosts; if ((num_hosts = hci_scan((char **) & return_string, 0x03)) > 0) { //case where one or more devices are found... } else { //case where zero devices are found... } afte...

Avoiding dynamic_cast/RTTI

I was recently working on a piece of C++ code for a side project (the cpp-markdown library, for the curious), and ran into a coding question that I'd like some opinions on. cpp-markdown has a base class called Token, which has a number of subclasses. Two of the main subclasses are Container (which holds collections of other Tokens) and ...

Converting an object of the type Object to another type using a Type object

Having an object like this: object integerObject=1; And an object like this: Type integerType=typeof(Int32); How can be the integerType object used to cast the integerObject to the type of Int32 ...

C++ style cast from unsigned char * to const char *

I have: unsigned char *foo(); std::string str; str.append(static_cast<const char*>(foo())); The error: invalid static_cast from type ‘unsigned char*’ to type ‘const char*’ What's the correct way to cast here in C++ style? ...

Type aliases for Java generics

I have a fairly complicated set of generic classes in Java. For example, I have an interface interface Doable<X,Y> { X doIt(Y y); } and the implementation class DoableImpl implements Doable<Foo<Bar<Baz,Qux>>,Foo<Bar<Zot,Qux>>> { Foo<Bar<Baz,Qux>> doIt(Foo<Bar<Zot,Qux>> fooBZQ) { ... } } In the real implementation, Doable has qu...

Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?.

Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?. I have tried it and it creates a run-time error. ...

How can I call explicitly implemented interface method from PowerShell?

Code: add-type @" public interface IFoo { void Foo(); } public class Bar : IFoo { void IFoo.Foo() { } } "@ -Language Csharp $bar = New-Object Bar ($bar -as [IFoo]).Foo() # ERROR. Error: Method invocation failed because [Bar] doesn't contain a method named 'Foo'. ...

Why is the result of this explicit cast different from the implicit one?

Why is the result of this explicit cast different from the implicit one? #include <stdio.h> double a; double b; double c; long d; double e; int main() { a = 1.0; b = 2.0; c = .1; d = (b - a + c) / c; printf("%li\n", d); // 10 e = (b - a + c) / c; d = (long) e; printf("%li\n", d); ...

C#: No casting within Generics?

While I can upcast a string to an object, I cannot upcast an IList of strings to an IList of objects. How come? What to do now other that coping all items to a new IList? static void ThisWorks() { IList<object> list = new List<object>(); list.Add("I can add a string since string : object"); } static void ThisDoesNotWork() { ...