| | |
ASCII to BINARY, & VICA VERCA
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
•
•
Originally Posted by keir.whitlock
Can anyone help?
Does anyone know of a simple peice of code in c/c++ that will allow me to convert ASCII characters into their relevent binary code and back again?
Cheers.
to do the reverse, if you used the assigning tactic, take the binary, and get the char equvilent by assigning the interger value to the char. if you used the atoi method, just take the binary convert it to an int, and use the itoa mehtod
check out this site:
http://nickciske.com/tools/binary.php
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
To continue where BountyX left off, to convert from integer to binary, use what I call the "calculating change" method - because it reminds me of one of the first C++ programs I learned was given 200 cents, calculate the equivalent in the smallest number of coins.
i.e. is number larger than 128, if yes, then a 1 ... is what is left larger than 64, if no, then a 0 ... is what is left larger than 32 ... and, well, you get the idea
i.e. is number larger than 128, if yes, then a 1 ... is what is left larger than 64, if no, then a 0 ... is what is left larger than 32 ... and, well, you get the idea
•
•
Join Date: Mar 2004
Posts: 77
Reputation:
Solved Threads: 2
use the & and << operators. remember that a character is just a sequence of bits. u cant test for each bit and output the result.
C++ Syntax (Toggle Plain Text)
int main() { char x = 'a'; int y; // the value of 'a' in hex is 0x61, or 97 decimal // in binary, that would be: 0 1 1 0 0 0 0 1 // or 64 + 32 + 1, or 2^7 + 2^6 + 2^0 // //loop for number of bits in a character //this will print out bits in reverse order //least significant bit first for(y = 0; y < sizeof(char) * 8; y++) printf("%c ", ( x & (1 << y) ) ? '1' : '0' ); puts(""); return 0; }
![]() |
Similar Threads
- Code Snippet: JavaScript Binary/ASCII Converter (JavaScript / DHTML / AJAX)
- ASCII to Binary (Python)
- Reading a file as ascii and then binary? (C++)
- ascii to binary then to hex (C)
Other Threads in the C++ Forum
- Previous Thread: C++ Error : opening file a second time for a read
- Next Thread: help for program involving switch loops and file
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







