Thread: ASCII to Binary and Hex

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    ASCII to Binary and Hex

    Back for a quicky :

    Code:
    #include <iostream.h>
    #include <conio.c>
    
    char textascii[] = "";
    
    char conbin(char* textstring)
    {
        for(int i = 0; i < 10; i++)
        {
            textstring[i] = (char)textascii[i];
        }
    }
    
    //////////////////////////////////////
    
    int main()
    {
        cout << "Enter text...\n";
        char text[100];
        cin >> text;
        conbin(text);
        cout << endl << endl << textascii;
        cin.get();
        cin.get();
        return 0;
    }


    I'm not getting any result, where I'm HOPING to get the ASCII value for the letter I'm typing in. I know there is a much simpler way to do it, and that way works, but I suck as things like this ( using arrays, chars, char*s, and making them work together ) which is why I need to work on it.

    I also want to make a code to convert a letter / string of text into binary code ( pure groups of 1s and 0s in groups of 8, per letter ), and also into hex...

    Finally, how can you read 0x00-style code ( which I believe is Hex too ) ?

    Thanks for replies.

  2. #2
    Dude to get ASCII code do this:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    char letter;
    cout<<"Char: ";
    cin>>letter;
    cout<<"ASCII: "<<(int)letter;
    return 0;
    }
    But this topic relates back to what I was coding for you. I can't seem to get it to work.

    It seems right but it doesn't do the job I expect it to... Can anybody see the mistake?

    Code:
    #include <iostream>
    using namespace std;
    char bin(int x)
    {
        char binn[31];
        for(int y=0;y<32;y++)
        {
            if(x>=(2^(32-y)))
            {
                binn[y] = '1';
                x = x - (2^(32-y));
            }else{
                binn[y] = '0';
            }
        }
        return *binn;
    }
    
    int main()
    {
        int number;
        cout<<"ASCII Value: ";
        cin>>number;
        cout<<bin(number);
        return 0;
    }
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    So you are trying to output a binary value?

    Code:
    int val = 255;
    const unsigned int mask[] = {0x80, 0x40, 0x20, 0x10, 0x08, 
    0x04, 0x02, 0x01};
    const unsigned int mask2[] = { 10000000,1000000,100000,10000,1000,100,10,1};
    int result = 0;
    
    int binconvert(int num)
     {
         int x=0;
     for (int i=0;i<8;i++)
     {
      if (num&mask[i])
            x+=mask2[i];   }
      return x;
     }
    "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
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    #include <iostream>
    #include <conio.c>
    using namespace std;
    
    int textascii[100] = {0};
    
    void conbin(char* textstring)
    {
        int index=0;
        while (textstring[index] != '\0')
        {
            textascii[index] = (int) textstring[index];
            index++;
        }
    }
    
    int main()
    {
        char text[100]={'\0'};
        cout << "Enter text...\n";
        cin >> text;    
        conbin(text);
    
        int index=0;
        while (textascii[index] != 0)
        {
            cout << textascii[index++] << " ";
        }
        
        cout << endl;
        system("PAUSE");
    }

  5. #5
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Rather than this:
    Code:
    cout << "ASCII: " << (int)letter << endl;
    You should be using the C++ typecasts, the one you need in this case is:
    Code:
    cout << "ASCII:" << static_cast<int>(letter) << endl;
    Correct me if I'm wrong.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    22
    cout<<"Ox"<<(int *)letter; //will output hex such as 0x000000FF
    cout<<int(letter); //will output decimal

    or,

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	char letter;
    
    	cout<<"char: ";
    	cin>>letter;
    	cout<< setbase(16) << int(letter); //for hex
            cout<< setbase(8) << int(letter);//for octal
            cout<< setbase(10) << int(letter);//for decimal the set base 
                                              //isn't really needed on for   decimal                                                       
    }
    or,

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char letter;
    	cout<<"char: ";
    	cin>>letter;
    	cout<< hex << int(letter); //hex
            cout<< oct << int(letter);   //oct
            cout<< dec << int(letter);  //dec, you don't really need to
                                       //specify dec here either.
    }
    sorry to be redundant, just wanted to show a few ways to do it
    Last edited by axlton; 07-28-2003 at 01:01 AM.

  7. #7
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thanks a lot, that was very useful. However, I've seen some different syntax I'm not sure about :

    Code:
    char h = "h";
    cout << (char) h;
    is the same as
    Code:
    char h = "h";
    cout << char(h);
    correct ?

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    char h = "h";
    should be
    Code:
    char h = 'h';

  9. #9
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    I'm aware of a few errors, as this was a quicky, but that doesn't tell me if those two syntaxes are the same thing or if there is a slight difference between them.

    I still haven't got this to work. I'm not aiming to cout the binary numbers, I'm aiming to have it stored in a variable. I have tried many codes so far, all from this topic as well, and I can't get what I want. Here I have another error, can anyone see it ? The program is always giving me "1" instead of a whole 8bit binary code.

    Code:
    #include <iostream.h>
    #include <conio.c>
    
    
    
    int tobin(char string)
    {
        int binnumber[8];
        for(int i = 0; i < 8; i++)
        {
            if(((int)string) >=(2^(7-i)))
            {
                    binnumber[i] = 1;
                    ((int)string) = ((int)string) - (2^(7-1));
            }
            else
            {
                    binnumber[i] = 0;
            }
        }
       return * binnumber;
    }
    
    
    
    
    int main()
    {
        char number;
        cout << "Text : ";
        cin >> number;
        cout << tobin(number);
        cout << endl;
        system("pause");
        return 0;
    }
    Last edited by Korhedron; 07-29-2003 at 01:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memo Box to Binary
    By peckitt99 in forum C++ Programming
    Replies: 8
    Last Post: 11-14-2007, 05:22 PM
  2. ASCII v's hex
    By sononix in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2005, 05:18 PM
  3. Printing Hex as Binary
    By Mystic_Skies in forum C Programming
    Replies: 6
    Last Post: 11-22-2004, 04:18 PM
  4. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 09:02 AM
  5. More hex and binary conversions
    By sean in forum C Programming
    Replies: 5
    Last Post: 06-08-2002, 07:57 PM