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; Thanks. I think that looks right, but I don't know about part of it... I don't know what using std::cout ...

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

    I think that looks right, but I don't know about part of it... I don't know what using std::cout does

    Would that just output it to the screen, or would it somehow save it for later conversions?

    I don't know if this would work... but...

    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++)
            {
                
    pszString[position] = std::pow(2i);
            }
            
    cout << std::endl;
        }
    }; 
    Would that change it to binary and store it?

    And, if so, how do I reverse that?

  2. #17
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    No, that won't store it.
    using std::cout; says that i'm using the cout object inside namespace std; This statement enables me to use cout << instead of std::cout <<

    To store it, you're going to need a vector or something...

    PHP Code:
    std::vector<bool> *PrintBinary(const char *pszString)
    {
        
    int length std::strlen(pszString);

        
    std::vector<bool> *binaryStore = new std::vector<bool>[length];

        for (
    int position 0position lengthposition++)
        {
            for (
    int i 0<= std::UCHAR_MAXi++)
            {
                
    binaryStore[position]->push_back(pszString[position] & std::pow(2i));
            }
        }
        return 
    binaryStore;
    }; 
    Remember to #include <vector>
    Sorry if this doesn't work.. i'm not too sharp when it comes to templates or the STL. There are many other ways this could be done, too.

  3. #18
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I was thinking... why do you even need to "store it" in the first place? Whenever you need the specific bits just use the loop that I gave you. Instead of printing it out, you can do whatever you want with it. It's utterly pointless to store all those values in a vector of bools.

  4. #19
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I need it stored because I'm not stopping there... I've got layers upon layers of encryption planned for this program.

  5. #20
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    No, you don't need to store it. It's already stored. You just need a method of retriving it. I gave one to you.

    pszString[position] & std:ow(2, i);

    That will tell you whether a particular bit is set or not.

  6. #21
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    ?

  7. #22
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Almost there... one more EASY thing.

    I've gotten it working just fine now, only I have one problem. If the binary value has any leading zeros, then it cuts them off because I convert it as an integer. Is there any EASY and SHORT way to add them back in, storing it as a string? I need it to have all 7 bits.

    So... if the input is 'a' it typecasts so that it gets the ASCII code for it is 97, so the binary value is 110 0001. That works fine... I get 1100001 for 'a'.

    But if the input is '0' for example... ASCII is 48, binary is 011 0000. It works fine but it chopps off the leading zero because it isn't a string... so what is the easiest way to get it to save 0110000 instead of 110000?

    I didn't use your function.
    Last edited by Trauts; 10-25-2002 at 06:11 PM.

  8. #23
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    At a quick glance (I haven't been tracking this conversation very well), I would say check how many characters are in the result, and if there are fewer than 8, add extra 0's to the front until there are 8 characters.
    Just Google It. √

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

  9. #24
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    yeah, I already thought of that but thought there might be a feature. I wanted to do it nice and neat and not a whole bunch of itoa/atoi type things...

    Any easy way? I have it stored as a long, but I need it as a string later. I could use something like

    Code:
    if (iBinvalue < 1000000)
    Right? I still would have the annoying job of shifting the characters over one place in the character array.

  10. #25
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    This is really weird... I'm trying to put the values in a string manually with a zero first, the iBuffer (the char as binary in a long) works fine if I cout it, but if I output any of the multidimentional array sOutput2 (char sOutput2[1024][7]), it gives me a completely different value... I tried substituting manual changes for itoa, but that doesn't seem to be the problem...

    Code:
    	for (i = 0, iLength = strlen (sOutput); i<iLength; i++)
    	{
    		iBuffer = to_binary( int(sOutput[i]) );
    		
    			if (iBuffer < 1000000)		//check to see if there are 7 digits
    			{
    				sOutput2[i][0] = '0';
    				sOutput2[i][1] = char( (iBuffer%1000000)/100000 );
    				sOutput2[i][2] = char( (iBuffer%100000)/10000 );
    				sOutput2[i][3] = char( (iBuffer%10000)/1000 );
    				sOutput2[i][4] = char( (iBuffer%1000)/100 );
    				sOutput2[i][5] = char( (iBuffer%100)/10 );
    				sOutput2[i][6] = char( iBuffer%10 );
    				sOutput2[i][7] = '\0';
    			}
    			else if (iBuffer >= 1000000)
    			{
    				_itoa(iBuffer,sOutput2[i],7);
    			}
    
    	}
    
    
    
    	cout << iBuffer << endl << sOutput2[0] << endl;
    For example... if I input 'a' into sOutput (not sOutput2), then send it off for binary conversion, the cout line ends up giving me something like

    Code:
    1100001
    12231000
    or if I input '0'...

    Code:
    110000
    0:):)
    It does that if it is done manually or with itoa! And yes, those are actually smileys in the program, not : ).

  11. #26
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    char *get_binary(const int binaryOutput)
    {
    	using namespace std;
    
    	char *pszReturn = new char[length];
    	pszReturn[length] = '\0';
    
    	for (int i = 0; i < 8; i++)
    	{
    		pszReturn[i] = (binaryOutput & static_cast<int>(pow(2, i))) ? '1' : '0';
    	}
    	return pszReturn;
    };
    Would that work for your purposes? Input a character, say 'a'... and itt'l return "10000110".

  12. #27
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I've got a working function for the binary part now... but it won't let me convert it to a string.

    Also, I have another small error I got while changing part into a function... its at Returning a multi-dimentional array from a function- how? if you're willing to help me there, too.

    Basically, the conversion for 'a' works perfectly. It gives me 1100001 just like I want when I cout it. Thats when its stored in the integer iBuffer.

    But when I try to convert it to a string in the multi-dimentional array sOutput2 (declared as sOutput2[1024][7]), then cout that, I get 12231000.

    Thanks so much for your help so far!

  13. #28
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I just realized I need it 8 characters, not 7...

    So no matter what it needs a zero before it.
    110000 needs to be 00110000 ('0') and 1100001 needs to be 01100001 ('a')

    I think your function would work fine if I used strrev, but then I don't learn anything and I'll just have the problem the next time I try to do something like this.

  14. #29
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Ack, your code is very difficult to understand. I think you should make a basic function, which takes 1 character, and returns a string containing all of its bits. Try that first. Returning multi-dimensional arrays, and working with multi-dimensional arrays of characters is getting extremely bulky and complicated.

    So basically

    char *toBinary(char input)
    {
    // convert input into a string of its bits and return it
    }

  15. #30
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    OK, thats a good idea. I was trying to get it to do each one. the multidimentional array was so that I could store them all in one place and have a much larger number of characters inputted without a thousand strings.

    With your code:

    PHP Code:
    char *get_binary(const int binaryOutput)
    {
        
    using namespace std;

        
    char *pszReturn = new char[length];
        
    pszReturn[length] = '\0';

        for (
    int i 08i++)
        {
            
    pszReturn[i] = (binaryOutput static_cast<int>(pow(2i))) ? '1' '0';
        }
        return 
    pszReturn;
    }; 
    That doesn't work because "length" is undefined. What do I put there?

    How exactly do I use it?

Page 2 of 4 FirstFirst 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, 07:41 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 08:35 AM
  3. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  4. Reading/Writing in Binary
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2001, 10:32 PM
  5. inserting characters into a binary tree
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-06-2001, 04:08 PM

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