5 Replies - 760 Views - Last Post: 19 February 2013 - 06:13 AM Rate Topic: -----

#1 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

Phrase to Binary

Posted 15 February 2013 - 11:50 AM

Hi all,

Been a while since I've posted on here since I don't have a C++ course this semester. I just got back into today and wanted to see if I could come up with a program that takes a phrase and "encrypts" it into ASCII binary code.
This is what i have so far.
#include <iostream>
#include <string>

using namespace std;

void conversion(string phrase) {

char a = 01100001;
char A = 01000001;
char b = 01100010;
char B = 01000010;
char c = 01100011;
char C = 01000011;
char d = 01100100;
char D = 01000100;
char e = 01100101;
char E = 01000101;
char f = 01100110;
char F = 01000110;
char g = 01100111;
char G = 01000111;
char h = 01101000;
char H = 01001000;
char i = 01101001;
char I = 01001001;
char j = 01101010;
char J = 01001010;
char k = 01101011;
char K = 01001011;
char l = 01101100;
char L = 01001100;
char M = 01001101;
char n = 01101110;
char N = 01001110;
char o = 01101111;
char O = 01001111;
char p = 01110000;
char P = 01010000;
char q = 01110001;
char Q = 01010001;
char r = 01110010;
char R = 01010010;
char s = 01110011;
char S = 01010011;
char t = 01110100;
char T = 01010100;
char u = 01110101;
char U = 01010101;
char v = 01110110;
char V = 01010110;
char w = 01110111;
char W = 01010111;
char x = 01111000;
char X = 01011000;
char y = 01111001;
char Y = 01011001;
char z = 01111010;
char Z = 01011010;


}

int main(int argc, char** argv)
{

string phrase;

    cout << "Enter the text you would like to be encrypted." << endl;
    cin >> phrase;
    getline (cin, phrase);
    conversion(phrase);
    cout << phrase;

    return 0;
}



I figured I'd need tell the program what each character stood for in ASCII binary. Was I correct in listing it like that in a voided function? Obviously it is missing the conversion, I didn't know where to start with that one. Ideas?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Phrase to Binary

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,132
  • Joined: 07-September 06

Re: Phrase to Binary

Posted 15 February 2013 - 12:30 PM

Not quite. You are listing the characters in their binary form in the function, but that doesn't do anything and further, the compiler will treat it as a base 10 number, not a base 2 number (binary), which will then get cut off at 255 (chars are only 8 bits long and 2^8 - 1 = max value of a char... or 255 (NOTE unsigned char)).

You actually don't need to do any sort of look up to determine the binary value of a character. All chars are stored in memory as an 8-bit number, and as a result you can just treat them like a number.

With that said, approach the problem like this (hopefully it will make more sense that way):
How would you go about outputting a number in base 2?

Say you have 14, that in binary is 1110, what steps are needed to get the decimal form of the number to the binary form of the same number?
Was This Post Helpful? 0
  • +
  • -

#3 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

Re: Phrase to Binary

Posted 15 February 2013 - 01:56 PM

Well to convert the decimal number you need to divide by to and save the remainder correct?

14 / 2 = 7 R = 0
7 / 2 = 3 R = 1
3 / 2 = 1 R = 1
1 / 2 = 0 R = 1

Re-writing from bottom up we get 1110. With that logic, I'd say I need to gather a cin and divide it by two and have it save the remainder until the formula reaches 1 or 0. On the right track?
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,132
  • Joined: 07-September 06

Re: Phrase to Binary

Posted 15 February 2013 - 02:53 PM

Yes, that is the way I would approach the problem. Also keep in mind that you can loop through each separate character in a string, so you only need to worry about a single character (8-bit number) at a time.
Was This Post Helpful? 0
  • +
  • -

#5 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Phrase to Binary

Posted 18 February 2013 - 09:43 PM

One possibility would be to use getline() as you suggest and fout (plus the necessary flag settings)to write to a binary file. Print the binary file. The problem is that a binary file is difficult for humans to read.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3534
  • View blog
  • Posts: 10,941
  • Joined: 05-May 12

Re: Phrase to Binary

Posted 19 February 2013 - 06:13 AM

buffalobill on most systems that will not work. That is because when you open and write to a binary files, it will write out a series of bytes. It does not write out a series of ones and zeroes like the OP is looking for.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1