Consider "2b" as a character array.
I think it should be 10 1100010 in binary.
Is this wrong ? I am not sure !
Kindly guide.
Consider "2b" as a character array.
I think it should be 10 1100010 in binary.
Is this wrong ? I am not sure !
Kindly guide.
I believe it's like so:
Code:2 b \0 0011 0010|0110 0010|0000 0000|
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; }
Many thanks to you for the program, I shall try it out tomorrow !
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 ?
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.
Thanks again, what is formula to know the binary representation of character 2 ? I aware of the formula for integer 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.Originally Posted by AnishaKaul
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.Originally Posted by AnishaKaul
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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".
Bloody hell. -_-
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.Originally Posted by AnishaKaul
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.