int to char

This is a discussion on int to char within the C++ Programming forums, part of the General Programming Boards category; Hi everyone! Can anyone tell me how the compiler cast an int to a char? I need this information for ...

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    int to char

    Hi everyone! Can anyone tell me how the compiler cast an int to a char? I need this information for a assembly work.
    Thanks any help!
    Nothing more to tell about me...
    Happy day =)

  2. #2
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    If I understand your question correctly...

    a char is just an 8-bit integer, because a char is just one of the 255 ASCII codes.

    Same as the short datatype.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
    int x = 5;
    
    char c = (char)x; //Here, x gets explicitly demoted to a char
    char d = x;          //Here, x gets implicitly demoted to a char

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by gustavosserra
    Hi everyone! Can anyone tell me how the compiler cast an int to a char? I need this information for a assembly work.
    Thanks any help!

    Well, if you were working with a big-endian machine, just generate the appropriate 'mov byte' opcode, for little-endian, index the 4th byte of the integer (use that offset) in the opcode.


    >> Same as the short datatype.


    These days, shorts are typically 16 bits.
    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;
    }

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    C++ cast:
    Code:
    int Alpha = 35;
    char Beta = static_cast<char>(Alpha);
    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.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    >> Can anyone tell me how the compiler cast an int to a char?

    As Sebastiani posted, you'll want to write assembly that accomplishes the following:

    Code:
        unsigned int   I = 0xabcdef12;
        unsigned short S = I;
        unsigned char  C = I;
    
        cout << hex;
        cout << "I = 0x" << I << endl;
        cout << "S = 0x" << S << endl;
        cout << "C = 0x" << (int)C << endl;
    gg

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Assembly. Right! If you use VC++, write a program that does a c ast then add the /FA flag in the compilation options. This will produce a formatted assembly output file every time you compile. Then all you have to do is sniff out the part that does the casting.
    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.

  8. #8
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Thanks everyone! Unfortunately I did not explain my problem well. I know how to tell the compiler to cast, but do not know how it implements. Thanks anyway =)
    Sebastiani: thanks man! Iĺl do this way!
    Nothing more to tell about me...
    Happy day =)

  9. #9
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    I didn't see the later posts! Thanks Codeplug and Magos!!!
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21