Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert a sentence into ASCII codes
#1
Ladies and Gentelmen (and perhaps both for some), the challenge is to make a program that takes a sentence, converts the characters into ASCII codes, then takes the numbers left over and converts them to binary. After all that, it should display the results.

By the way, you can use either QB, FB or any other language that suits your fancy Smile .
quote="Bruce Raeman"]Anatomy (n): something everyone has, but which looks better on a girl[/quote]
Reply
#2
um ok then
you mean like this?
(FB CODE)
Code:
dim as string bla
dim as integer tempcount,tempcount2
bla = "hahahaha"
for tempcount = 0 to len(bla) - 1
    for tempcount2 = 7 to 0 step -1
        if tempcount2 > 0 then print str$(bit(bla[tempcount],tempcount2)*-1); else print str$(bit(bla[tempcount],tempcount2)*-1) + " ";
    next    
next
sleep
[Image: freebasic.png]
Reply
#3
(FB CODE)
Code:
dim blah as string, i as integer
blah="I like cats"
for i=0 to len(blah)-1
print bin$(blah[i])
next

=P
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#4
Quote:By the way, you can use either QB, FB or any other language that suits your fancy Smile .
Well, if you insist, here is a C++ version:
[syntax="C++"]#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;

string charCodeToBin (int myCode);
string numToStr (int num);
int strToNum (string substring);

int main() {
string sentence, code, charCodes = "", result = "";
cout << "Enter a sentence: ";
getline(cin, sentence);
for (long letter = 0; letter < sentence.length(); letter++) {
code = numToStr((int)sentence[letter]);
for (int pad = 0; pad < 3 - code.length(); pad++)
code = '0' + code;
charCodes += code;
}
for (long asc = 0; asc < charCodes.length(); asc+=3)
result += charCodeToBin(strToNum(charCodes.substr(asc, 3)));
cout << sentence << endl << charCodes << endl << result << endl;
system("pause");
return 0;
}

string charCodeToBin (int myCode) {
string bin = "";
int n = 0;
for (int i = 0; i < 8; i++) {
n = myCode % 2;
myCode /= 2;
bin = numToStr(n) + bin;
}
return (bin + ' ');
}

string numToStr (int num) {
ostringstream buffer;
buffer << num;
return buffer.str();
}

int strToNum (string substring) {
istringstream buffer(substring);
int code;
buffer >> code;
return code;
}[/syntax]:-)
I created my own string-to-number and number-to-string functions, just for my own practice with streams. The other function is for converting the ASCII codes to binary. It could be A LOT shorter, but I figured this was the easiest and funnest to figure out (for the most part). Enjoy. :-)
974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)