Thread: variable type conversion

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    10

    variable type conversion

    Does anybody know of a complete (extensive) guide for converting various data types. Many times I get compilation errors because different functions use different types of variables, such as unsigned char, BYTE, const char etc. So I'm looking for a an all purpose conversion guide, BYTE to char, unsigned char to char*, etc, etc etc. Its often very frustrating not being able to use the data from different apis since I'm not able to convert between them. Thanks in advance.

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    Safe practice often results in there being no need to perform such conversions. Consider why you have compilation errors before trying to blindly cover them up. The errors are warning you that something untoward is going to happen.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    I understand that it is often better to find the root of the problem in the functions that you program with, but the programs that I do are only for personal use. Basically to automate various tasks that I do on my computer, so it is often easier to do a quick fix rather than have it work flawlessly. Especially since these programs only have to work for me and not on any other system. So if anybody has anything that shows various conversions let me know.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    Basically to automate various tasks that I do on my computer, so it is often easier to do a quick fix rather than have it work flawlessly.
    I would think that one would be more open to quality issues when they are the end user as well as the engineer.
    So if anybody has anything that shows various conversions let me know.
    It depends greatly on the types in question. If a conversion is possible then you wouldn't be getting errors. If you receive a warning then you can silence the warning with a cast though doing so can be unsafe. For some conversions, such as string to integer, a library function can possibly do what you want.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Type-checking is provided as a service, not a hindrance, to us programmers that helps us avoid many of the errors that can occur when casting from one type to another. Of course, if you're absolutely *sure*, and just need a 'quick' cast, you can use a template like this one:

    Code:
    template <class t1, class t2>
     t1 & cast(t1 & dst, const t2 & src)
    {
     dst = (t1)src;
     return dst;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Guys guys, this is C++. Make use of the template casts:

    For normal casts:
    A = static_cast<TYPE>(B);

    For direct bitpattern casts:
    A = reinterpret_cast<TYPE>(B);

    For casting from a const type to a normal type:
    A = const_cast<TYPE>(B);

    There is also a dynamic_cast, but I'm not entirely sure what it does. I believe it's like the static_cast but does type checking dynamically (so it works better in inheritance situations?).
    Last edited by Magos; 02-25-2004 at 05:50 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Originally posted by Magos
    There is also a dynamic_cast, but I'm not entirely sure what it does. I believe it's like the static_cast but does type checking dynamically (so it works better in inheritance situations?). [/B]
    Magos, I think that dynamic cast is suitable were, for example you have two classes from one, like this:

    Code:
    class Base{
       //code
    };
    
    class In_A : public Base{
       //code
    };
    
    class In_B : public Base{
    
    };
    At execution time, a pointer to Base could be pointing to In_A or In_B, depending on input from the user. So, there is no way to assure, at compiling time, when trying to dynamic cast from Base to In_B if the pointer does not point to In_A. Sorry for the bad english, it is just an ideia anyway.
    Nothing more to tell about me...
    Happy day =)

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb Typecasting

    As you have probably gathered from the responses, type conversion is called typecasting.

    Look-up typecase and cast.

    The simplest (and least-safe, I believe) way to cast, is to put the new type in parenthesis in front of the variable:

    Code:
    char c ;
    int i = 1 ;
    
    c = (char)i ;     //  Converts i to type char, then assigns value to c
    unsigned char to char*,
    That's a real error! The function needs a pointer to a c-style string, and you are trying to pass-in a single character.

    A couple of other topics to look-up (if you are not already familiar with them) are:
    function overloading
    templates

    Joining in the chorus: You should always try to avoid typecasting. It's a good way to make a compiler warning/error go away, and create a nasty hard-to-find run-time bug! You should almost never need typcasting for functions you've written yourself... if you've planned carefully. You might need it for library functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. Replies: 6
    Last Post: 07-29-2008, 04:37 AM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM