Help with outputing char in int

This is a discussion on Help with outputing char in int within the C++ Programming forums, part of the General Programming Boards category; I want to output c[0] and c[1] in int , should I use type casting ( and how ? ) ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    Question Help with outputing char in int

    I want to output c[0] and c[1] in int , should I use type casting ( and how ? ) or atoi ( it requires casting since the proto is const char * ?)

    Code:
    #include <iostream>
    
    using namespace std;
    union swap_byte{
        void swap();
        void set_byte(unsigned short i);
        void show_word();
        unsigned short u;
        unsigned char c[2];
    };
    void swap_byte::swap()
    {
        unsigned char t;
        t=c[0];
        c[0]= c[1];
        c[1]=t;
    }
    void swap_byte::show_word()
    {
        cout<<u;
    }
    void swap_byte::set_byte(unsigned short i)
    {
        u=i;
    }
    int main()
    {
        swap_byte b;
        b.set_byte(49034);
        b.swap();
        b.show_word();
        cout<<b.c[1];
        cout<<b.c[0];
        
        
        int x;
        cin>>x;
        return 0;
    }

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Im not sure what your trying to do...

    But, cout << (int)b.c[1];

    should work.

    If thats not what you need, try to explain exactly what you want your program to do.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If you want to convert an character (or string) to an integer, check out the faq

    But this doesn't look good:
    Code:
    void swap_byte::swap()
    {
        unsigned char t;
        t=c[0];
        c[0]= c[1];
        c[1]=t;
    }
    None of those variables have meaningful values in them...Hopefully you haven't completed this code yet?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Yeah, I get lost when reading your code.. I just cant figure out what your wanting to do...

    If you simply just wanting to store a number into a char and then display that number

    Code:
    #include <iostream>
    
    int main()
    {
    
        unsigned char one_byte = 20;
    
        std::cout << (int)one_byte;
    
        return 0;
    
    }

  5. #5
    Banned
    Join Date
    Aug 2004
    Posts
    128
    if your not against using a little c in your c++ program
    this works real easy, im not sure if it will address every
    problem you may come across but if it helps with the current
    then yaya?, anyways :

    Code:
    char mystring[6] = "hello";
    printf("%i  %i",mystring[0],mystring[1]);
    output :

    104 101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04: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, 02:27 PM

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