Thread: casting void*

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    32

    casting void*

    Hello,

    How could I cast a void* into an integer pointer that points to integers of 1 byte size?

    I was thinking about something like this,

    Code:
    void* = vp;
    short int* ip = reinterpret_cast<short int*>(vp);
    but that will give me a pointer to a 2 byte integer.

    Seron

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Cast it to a char*.
    - lmov

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    32
    Thanks, that works.

    Is there a way to make custom casts, like for a word of 15 bytes or for one of half a byte?

    Another problem I have are the following casts. The first cast works. The 2nd gives the error "Cannot cast from 'int' to 'unsigned char'".

    Code:
    int value
    
    unsigned char uchvalue = (unsigned char)value;
    
    unsigned char uchvalue = reinterpret_cast<unsigned char>(value);

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Is there a way to make custom casts, like for a word of 15 bytes or for one of half a byte?
    Assuming that char is 8 bits:
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    typedef unsigned char byte;
    inline byte get_first(byte b)  { return b >> 4; }
    inline byte get_second(byte b) { return b & 0xf; }
    inline void set_first(byte& b, byte d)  { b |= d << 4; }
    inline void set_second(byte& b, byte d) { b |= d & 0x0f; }
    
    int main() {
        // Declare two half-bytes
        byte b = 0;
        set_first(b, 0xa);
        set_second(b, 0x5);
    
        cout << "1st half-byte = 0x" << hex << static_cast<int>(get_first(b)) << '\n';
        cout << "2nd half-byte = 0x" << hex << static_cast<int>(get_second(b)) << '\n';
    }
    Or something of the kind. Of course you can wrap this up in a class and together with operator overloading provide a cleaner interface. The 15 byte type will be more difficult and quite expensive in computing power. Are you sure you need such big numbers?
    Another problem I have are the following casts.The first cast works. The 2nd gives the error "Cannot cast from 'int' to 'unsigned char'".
    Code:
    int value
    
    unsigned char uchvalue = (unsigned char)value;
    
    unsigned char uchvalue = reinterpret_cast<unsigned char>(value);
    Use static_cast for such casts. You can also let it convert it implicitly, though most compilers will give a warning about possible loss of data.
    - lmov

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    32
    Thanks Imov, just what I needed.

    There is no need for a 15 byte type. I only wish to understand casting better.

    Seron

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    I made a little mistake. The set_* functions should be:
    Code:
    inline byte set_first(byte& b, byte d)  { b &= 0x0f; b |= d << 4; }
    inline byte set_second(byte& b, byte d) { b &= 0xf0; b |= d & 0x0f; }
    - lmov

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting
    By morvick in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2007, 11:06 PM
  2. Casting Question (I think)
    By fayte in forum C Programming
    Replies: 6
    Last Post: 03-08-2006, 05:31 PM
  3. casting the system exstracted date into seperate ints
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-30-2005, 12:17 AM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 02:16 PM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 05:01 AM