Qbasicnews.com
September 17, 2019, 05:26:27 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Convert a sentence into ASCII codes  (Read 8104 times)
jakeman922
Member
*
Posts: 74



« on: September 22, 2005, 10:27:18 PM »

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 Smiley .
Logged

quote="Bruce Raeman"]Anatomy (n): something everyone has, but which looks better on a girl[/quote]
Deleter
Na_th_an
*****
Posts: 1292



WWW
« Reply #1 on: September 22, 2005, 10:47:43 PM »

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
Logged

whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #2 on: September 22, 2005, 11:48:15 PM »

(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
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
rpgfan3233
Ancient Guru
****
Posts: 617



« Reply #3 on: September 23, 2005, 12:43:35 AM »

Quote from: "jakeman922"
By the way, you can use either QB, FB or any other language that suits your fancy Smiley .

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. :-)
Logged

974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!