Thread: Representation of string "2b" in binary

  1. #1
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115

    Representation of string "2b" in binary

    Consider "2b" as a character array.
    I think it should be 10 1100010 in binary.
    Is this wrong ? I am not sure !
    Kindly guide.

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I believe it's like so:
    Code:
            2         b        \0 
    0011 0010|0110 0010|0000 0000|

  3. #3
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    You could also implement some code to display your values in binary:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
            const char *hex = "2b";
            int i = 0, j = 0;
            unsigned char test = 128;
    
            for(i=0; i<strlen(hex) + 1; i++) {
                    printf("hex[%d]: %c ", i, hex[i]);
                    for(j=0; j<8; j++) {
                            if(hex[i] & test)
                                    printf("1");
                            else
                                    printf("0");
                            test = test >> 1;
                    }
                    printf("\n");
                    test = 128;
            }
    
            return 0;
    }

  4. #4
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by matrixx333 View Post
    You could also implement some code to display your values in binary:
    Many thanks to you for the program, I shall try it out tomorrow !

    Quote Originally Posted by msh View Post
    I believe it's like so:
    Code:
            2         b        \0 
    0011 0010|0110 0010|0000 0000|
    Thanks for bothering !
    Kindly explain why do you think '2' should be represented like '0011 0010' and not like '10' !
    I mean how is the char array effecting the binary representation of 2 ?

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Because 0011 0010 is the binary representation of character 2, and not integer 2. They are stored differently. Note that the decimal code for ASCII character '2' is 50, whose binary representation happens to be 0011 0010.
    Last edited by msh; 09-16-2010 at 08:30 AM.

  6. #6
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Thanks again, what is formula to know the binary representation of character 2 ? I aware of the formula for integer 2 !

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AnishaKaul
    Kindly explain why do you think '2' should be represented like '0011 0010' and not like '10' !
    I mean how is the char array effecting the binary representation of 2 ?
    I think that you should have specified that you were trying to represent 0x2b in a binary representation. msh interpreted "2b" as two ASCII characters.

    Quote Originally Posted by AnishaKaul
    Thanks again, what is formula to know the binary representation of character 2 ?
    It depends on what is your input, and how you want to convert it. For example, you could use a table, or you could use bitwise operators, etc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    Now I feel bad for giving you the code instead of encouraging you to figure it out. It's important that you understand how the code works, not just to "use it".

  9. #9
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Bloody hell. -_-

  10. #10
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by laserlight View Post
    I think that you should have specified that you were trying to represent 0x2b in a binary representation. msh interpreted "2b" as two ASCII characters.
    I think msh has interpreted it correctly, I had actually memcpyed, "2b" in a char array, and now I want to understand the formula for representing that '2' in binary.

  11. #11
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by matrixx333 View Post
    Now I feel bad for giving you the code instead of encouraging you to figure it out. It's important that you understand how the code works, not just to "use it".
    There is no need to feel bad for anything, I don't cram code without understanding it ! I shall definitely try to understand how it works.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AnishaKaul
    I think msh has interpreted it correctly, I had actually memcpyed, "2b" in a char array, and now I want to understand the formula for representing that '2' in binary.
    Ah. So, you claim that you know how to represent 2 in binary right? As such, you presumably know how to represent 50 in binary. As was already pointed out to you, '2' has a value of 50 in ASCII, so you're done.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by laserlight View Post
    Ah. So, you claim that you know how to represent 2 in binary right? As such, you presumably know how to represent 50 in binary. As was already pointed out to you, '2' has a value of 50 in ASCII, so you're done.
    Perhaps you are referring to your Post number 7, in which you said "you could use a table". I couldn't understand that you meant "'2' has a value of 50 in ASCII".

    Thanks for pointing out again.

  14. #14
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by AnishaKaul View Post
    I think msh has interpreted it correctly, I had actually memcpyed, "2b" in a char array, and now I want to understand the formula for representing that '2' in binary.
    There really isn't a formula for it. It's sort of arbitrary. Any ASCII table worth its name should have a character and its corresponding decimal code listed. Like already pointed out, the binary representation of any ASCII character is that of it's corresponding decimal code.
    Last edited by msh; 09-16-2010 at 08:44 AM. Reason: Bold-ified the important part.

  15. #15
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by msh View Post
    There really isn't a formula for it. It's sort of arbitrary. Any ASCII table worth its name should have a character and its corresponding decimal code listed. Like already pointed out, the binary representation of any ASCII character is that of it's corresponding decimal code.
    Thanks again for explaining ! :hattip:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Binary Representation of String
    By Allessandro in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 05:11 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM