Characters to binary... hexadecimal to binary

This is a discussion on Characters to binary... hexadecimal to binary within the C++ Programming forums, part of the General Programming Boards category; Basically what the title says... I need to be able to convert a string to binary... then convert that to ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Characters to binary... binary to characters

    Basically what the title says... I need to be able to convert a string to binary... then convert that to hexadecimal (cout << hex << stringname; works fine for that part, but I can't save the output), and then the ability to change hexadecimal to binary and convert the binary to the original string... basically code and decode it...

    I need the part converted from the characters (it has to have letter support) to binary to be a string so it uses hexadecimal for 0 and 1, not for the letters... so the integer data type wouldn't work.

    I'm using Microsoft Visual C++ 6.0 as a compiler, and this is for a console application (DOS).

    ANY help would be much appreciated!

    Thanks!
    Last edited by Trauts; 10-23-2002 at 08:57 PM.

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Do you mean a string that containes "10001011101" and to convert it into binary?

    Maybe you can tokenize the string then use atoi().

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Lets say I input the string "programming" into the program. It takes it as a string and puts the value "111 0000" for 'p' and "111 0010" for 'r' and so on.

    So I'll end up with a long binary number representing the word "programming"

    I don't want to use an insanely long switch statement, so a function would be nice.

    I need to convert that to hexadecimal, and store that, too.


    Then I need another routine that will convert the hexadecimal back to the binary, and convert the binary to text.


    I don't think it is as hard as it sounds, but I don't know how to get the binary values for characters easily.

    I'd also need to be able to convert numbers to binary.

    THANKS!

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    for gods sake. computers only understand binary. Your chars are stored as a binary representation anyway. say char is 65 thats 00100001 in 1 byte. You can access the individual bits with c/c++ bitwise operators. This has been dealt with many times before so do a board search.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I did do a search.

    And chill out... I know everything is stored in binary, but C++ doesn't have a command that converts it to binary... I know it is STORED in binary, but I need it usable.

    I need it to convert NUMBERS and LETTERS.

    And how might I convert to HEXADECIMAL and from HEXADECIMAL to BINARY?

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Also, where can I learn about XOR encryption? I found an old thread and I need to start from what I already have of this encryption program.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I know everything is stored in binary, but C++ doesn't have a command that converts it to binary
    What are you trying to say? Is this what you are trying to do?

    Code:
    char stuff[] = "abcd";
    char* asdf = changetobinary(stuff); //convert "abcd" to binary and store the string version in asdf
    I need it to convert NUMBERS and LETTERS
    To what?

    And how might I convert to HEXADECIMAL and from HEXADECIMAL to BINARY?
    If you're talking about converting an int to hexadecimal/binary, then you are a retard, and can shut up right about now. Otherwise, if you are trying to convert a string which represents a hexadecimal number to a string representing a binary number, you can do the research yourself... first, figure out how to convert from hex to bin (and vice versa) and then figure out how to do it digit by digit or something; or, there might be useful functions for you to use that can convert a string in a certain base into an int, and then convert the int into a string using a certain base.

    P.S. don't use caps; they don't make people want to help you. Instead, they make people want to smack you over the head with a large trout.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I meant to convert from base 10 to hexadecimal, yes, I know how to do it manually, but I don't want to use a switch statement if it is avoidable.

    if the string was "Hello" (this is simplified because I don't want people to know everything about my program, or its useless except as an exersize...) then (yes, this is basically taken from another tutorial, it doesn't have everything I need, though http://www.cpp-home.com/print_t.pl?102)...

    H - 72
    e - 101
    l - 108
    l - 108
    o - 111

    so overall:
    72 101 108 108 111

    72 - 01001000
    101 - 01100101
    108 - 01101100
    108 - 01101100
    111 - 01101111

    so...

    01001000 01100101 01101100 01101100 01101111

    and...

    0100100001100101011011000110110001101111

    divide that every 6 bits...

    010010 000110 010101 101100 011011 000110 1111

    and converting it back to base 10:

    18 6 21 44 27 6 15

    just going to hexadecimal for output is simple... like I said before, cout << hex << varname;

    but I need it stored.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    divide every six bits
    There's your first problem. Hexadecimal doesn't mean dividing every 6 bits. But anyways, to do what you're trying, this is what I would do:

    1) convert each letter of the input into a binary number, and append each such binary number (I mean, its digits) to a std::string.

    2) create a tempory char[7] (or 6? Not sure if you need the null 0...).

    3) Copy the first six characters of the std::string to the char array.

    4) Use atoi() or strtol() or something to convert it into an integer, and store the result in a vector of ints. (no, I don't want to start up the old atoi() vs strtol() war again I heard strtol() is better though, but you can pick whatever you choose.)

    5) Repeat 3 and 4 until finished.

    Of course, you'll have to make sure you don't do an array index out-of-bounds error or anything, when you get to the last batch of characters in the std::string, in case you have fewer than 6 chars to copy to the temporary array.. you can use std::string::size() to check the size of the string, and then use modulus to figure out how many chars you have left at the end of the string, and only copy that many to the temporary array.

    Hope this helps!

    P.S. This is all theoretical, and might not work - but it's the best I can do right now, since I'm supposed to be doing homework
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I know everything is stored in binary, but C++ doesn't have a command that converts it to binary...
    Do you listen man?
    You can access the individual bits with c/c++ bitwise operators.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    lol i dont think that's what he meant in his question... look at his latest post
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    but I need it stored.
    Ever heard of stringstreams? Very similar to ostreams except they write to memory instead of the screen.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  13. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Hunter2, thanks for looking over some of my obvious stupidities and helping me anyway... lol

    I meant that that was one layer of encrypting, the way the tutorial had it. I've thought about it for a little while, and the hexadecimal part I had planned IS pointless pretty much.

    So really all I need (at the moment, I know how to do the rest) is how to save the text/number string in binary (as a string, not an integer... I need the leading 0s)

    I also need to know how to reverse it.

    I also need to understand XOR a lot better so that I can use that, too. How would I implement XOR as a function, so that I can run all this stuff different times... so a function to binary and back, etc...

    I hope I make sense this time

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I need to be able to go from a string like "Hello" to the ASCII code, and from that to binary.

    I don't know how to do that without using a very very long switch statement.

    I also have 100% no clue at all how to use "bitwise" or what "stringstreams" are


    Oh, and one more thing: the 6 bits thing was dividing it up so that it would change the output... simple to do manually... char[x], etc.
    Last edited by Trauts; 10-23-2002 at 08:30 PM.

  15. #15
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    PHP Code:
    void PrintBinary(const char *pszString)
    {
        
    int length std::strlen(pszString);
        
    using std::cout;

        for (
    int position 0position lengthposition++)
        {
            for (
    int i 0<= std::UCHAR_MAXi++)
            {
                
    cout << pszString[position] & std::pow(2i);
            }
            
    cout << std::endl;
        }
    }; 
    Something like that? May need some tweaking, I didn't test it... and i'm extremely tired.

    Oh, and the headers you'll need
    <climits>
    <iostream>
    <string>
    <cmath>

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. Reading/Writing in Binary
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2001, 09:32 PM
  5. inserting characters into a binary tree
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-06-2001, 03:08 PM

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