Thread: Snippet ascii->hex->binary

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    Snippet ascii->hex->binary

    Does anybody have a snippet for converting a string from ascii to hex and then each character in hex to binary?

    This would be greatly appriciated.

    No this isn't schoolwork, I am currently working on an encryption program.

    Thank you for even reading this thread.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Do you mean something like:

    Code:
    #include <bitset>
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        string str = "abcdefghijkl";
    
        for( string::iterator itr = str.begin(); itr != str.end(); ++itr )
            cout << *itr << " = " << hex << static_cast<int>(*itr)
                 << ' ' << bitset<8>(*itr) << endl;
    
        return 0;
    }
    Output:
    Code:
    a = 61 01100001
    b = 62 01100010
    c = 63 01100011
    d = 64 01100100
    e = 65 01100101
    f = 66 01100110
    g = 67 01100111
    h = 68 01101000
    i = 69 01101001
    j = 6a 01101010
    k = 6b 01101011
    l = 6c 01101100
    [edit]oops, forgot the using namespace std; part[/edit]
    Last edited by hk_mp5kpdw; 02-09-2006 at 02:23 PM. Reason: Added using namespace std;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Ahhhh, a work of art.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    basically I want it to convert A to 41 and then 4 to 0100 and 1 to 0001

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by bikr692002
    basically I want it to convert A to 41 and then 4 to 0100 and 1 to 0001
    You mean just like hk_mp5kpdw showed? And if you are not talking about the text representation as an output, that in fact you mean to "do this in binary", then the whole thing is a no-op and you need to first understand representations and values.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    yeah, but I want the function to return the whole string converted to binary to the main program

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Use a stringstream in a similar fashion and return the string?
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <bitset>
    
    std::string foo(char letter)
    {
        std::ostringstream oss;
        oss << std::bitset<8>(letter);
        return oss.str();
    }
    
    int main()
    {
        char letter = 'A';
        std::string result = foo(letter);
        std::cout << letter << " -> " << result << std::endl;
        return 0;
    }
    
    /* my output
    A -> 01000001
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Ok, I don't know if I'm understanding you
    Heres my code (I'm just learning the time functions so They are completely wrong)
    Code:
    #include <iostream>
    #include <time.h>
    int main()
    {
        char UserInput[1024];
        char DecryptedInput[1024];
        char Option[1];
        int UserInputToBinary;
        int Time;
        int EncryptedString;
        int Day;
        int Month;
        
        std::cout<<"Please press E to encrypt, D to decrypt";
        std::cin>>Option;
        if (Option)
        {
                     std::cout<<"Please enter string to be enrypted: ";
                     std::cin>>UserInput;
                     Time=struct tm *localtime( const time_t *tm_hour );
                     Time=Time*3600;
                     Day=struct tm *localtime( const time_t *tm_mday );
                     Month=struct tm *localtime( const time_t *tm_mon );
                     Year=struct tm *localtime( const time_t *tm_year );
                     std::cout<<"Please save this, this is key1: "<<Time;
                     std::cout<<" Key2: "<<Day<<" Key3: "<<Month;<<" Key4: "<<Year;
                     ConvertE(UserInput);
                     UserInputToBinary=UserInput;
                     EncryptedString=UserInputToBinary*3.14358/Time*Year/10.9574/Day*Month;
                     std::cout<<"The encrypted string is: "<<EncryptedString;
                     std::cin.get();
                     return 0;
        }
        if (Option=D)
        {
                     /*std::cout>>"Please enter string to be decrypted: ";
                     std::cin<<UserInput;
                     std::cout>>"\nPlease enter key1: ";
                     std::cin<<Time;
                     std::cout>>"\nPlease enter key2: ";
                     std::cin<<Day;
                     std::cout>>"\nPlease enter key3: ";
                     std::cin<<Month;
                     std::cout>>"\nPlease enter key4: ";
                     std::cin<<Year;
                     UserInputToBinary=Month/Day*10.9574*Year/Time*3.14358/UserInput;
                     ConvertD(UserInputToBinary);
                     DecryptedInput=UserInputToBinary;
                     std::cout>>"\nThe decrypted message is: ">>DecryptedInput;
                     */std::cin.get();
                     return 0;
        }
        return 0;
    }
    and the ConvertE and ConvertD are what I'm trying to do, they are in a seperate header.
    Last edited by bikr692002; 02-09-2006 at 02:55 PM.

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Quote Originally Posted by Dave_Sinkula
    Use a stringstream in a similar fashion and return the string?
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <bitset>
    
    std::string foo(char letter)
    {
        std::ostringstream oss;
        oss << std::bitset<8>(letter);
        return oss.str();
    }
    
    int main()
    {
        char letter = 'A';
        std::string result = foo(letter);
        std::cout << letter << " -> " << result << std::endl;
        return 0;
    }
    
    /* my output
    A -> 01000001
    */
    Theproblem with that is that it returns the value of A, not the two hex value bites into binary like i.e. A->E2(i know not real vaalue)and have it convert to (Its incorrect) 0110 0010 instead of 0100001

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM