C/C++ :: Writing A Program Which Includes Encoding And Decoding A Message?
Oct 31, 2014
I am writing a porgram which includes encoding and decoding a message.Now I am doing the encoding part.
The goal is to encode a message using similar approach as Caesar Cipher. But instead of alphabets, I am going to use the entire ASCII code of total 128 codes to be the base.
And it is not a simple shifting, it is a rotation. This means if you want to shift 4 units from char <DEL>, it will come back to the first code in ASCII Code which is at dec 0 char <NUL> and starts the shifting again. It won't jump if the limit of ASCII Code is reached.
Here we've got 7 different malls' names and I am going to encode them with a shift n. I think n is free to set, so here I just set it as 5. This is also called the key.
I have written something and I think it should work, but after all it doesn't. And I don't get what is wrong.
Here is my program:
#include <iostream>
using namespace std;
const int NUMBER_OF_MALLS = 7;
const int MALL_NAME_LENGTH = 13;
const int NAME_SIZE = MALL_NAME_LENGTH + 1;
void encode(int key, char plain[], int length, char encoded[]) {
for (int i = 0; i < MALL_NAME_LENGTH; i++)
[code].....
Note that I am not going to use other libraries, just <iostream>.
Depending on the message ID different messages represent different values for one project.For example msg with ID 10 can include in the 8 bytes something like:
Printing the message is no big deal. But here comes the tricky part. I want to print out the specific information hidden in the 8 bytes. I can define the structures for every msg ID and compile the program with this "special" header file, but I want to do it during runtime of the program, loading the information regarding the msgs, because i can have different projects where the information for different msg IDs can differ.
I've a non-C file, where basically all the information is written. Lets stay frame named
GetStatus{ bit 0 - 7 width bit 8 - 15 height . . } etc.
How to read it on runtime and decode the messages? On runtime I'm not able to create variables and structures anymore!
I am struggling with the concept of having different ccp's and header files. I made a really bad example project for representation, but basically my question is are any of the #includes unnecessary that I have? Technically it functions, but if I am doing it wrong I want to prevent myself from starting bad habits in the future. My code just basically uses strings and sets a name and prints it. My code is really bad, but I wanted to just use includes in such a way for a quick example.
//MAIN.CCP #include "functions.h" using namespace std; int main()
I'm trying to parallelize bottom's up merge sort algorithm. Firstly, i must have working procedural code.
I rember I had my code working last week but I made some changes ( probably to arguments passed to sort function ), i run my program several times without rebuilding it so i though it's still working.
Now i came back to my code and my program terminates without any message which confuses me. I debbuged merge function and it seems to work well, at the end i have sorted array.
I want to be able to type a message into another program (such as notepad) using C++. How would I do that? Is there a function to do this, or an event, or something?
I'm trying to write a code that is read character user 'e' or ' ' space also numbers I mean a number 'e' or space 'e' a number 'e' or space so forth.But i get absurd numbers. The program shows me the added number. If ' ' entered the taking numbers will stop(scanf will stop).
Example input:
e 1 8 7 2 3 6 or e 1 e 8 e 7 e 2 e 3 e 6
Code: #include <stdio.h> #define MAX 10 void addq ( int *, int, int *, int * ) ; void test();
I was assigned to make a vigenere cipher using the function void vegenere(char* to_encrypt, char* key, char* encrypted) I got it to work for the encryption but i have to be able to decrypt the phrase too. I was assigned to write in a flag which indicated encryption or decryption. I tried to implement this but now it wont decrypt and i dont know why, all it does is put the same copy of decryption (which is really encrypted) multiple times until it crashes.
Ok i found my dumb error of putting decrypt on an infinite loop which i fixed by putting it into the if statement after the encryption output. However now it says that the encrypted and decrypted outputs are identical without decrypting the ciphertext
I need to have a program display an error message if the variable entered isn't an integer but then I want it to cin again. I have this but it doesn't work:
cout << "Enter an Integer: " ; for (;;) { cin >> var; if (!cin) {
[Code] ....
I am not sure how to do what I want and this doesn't work, it just repeats That wasn't an int.. over and over again.
This for loop replaces the stars ******** in an array that contains a word to be guessed with the correct letter (c) that the user inputs if the word contains that letter.
Problem: After every guess the same hidden string of stars ******* is displayed instead of ex: ***W**** (assuming W was entered by the user)
How can I update the old ******** string in the Stars array with the new string that includes the correct letters chosen, so after every correct guess at a letter in the word a new string is displayed including the correct letters chosen?
I'm pretty sure I have to use strcpy but not sure how to implement it using a loop.
Code: for(i = 0; i < strlen(unscrambledWord); i++) { if(unscrambledWord [i] == c) { Stars[i] = c; } }
I'm having some problems in receiving fileNames from Server to Client(C++) in Mac OS X. I send a serialized object , which has a char pointer with the fileName or sometimes a string object, when i receive it in the client, it seems to be having %F6 or %E9 ,etc . This issue don't arise in Windows OS though, even thought it's the same code. Is there anyway decoding these '%' characters back to their original form in Mac OS & Linux ..?
Fex characters i got into problems with : ǡ ȅ ȉ
It would be difficult to change the code in server, so if there's a way decoding the characters back to its original form, it would be easier.I'm using Boost Library for Serialization and i'm just looking for ways to decode %F6 back to ȅ in C++, like if some library is available ..?
I am writing a program to auto login in a web site. Before making next attempt I have to wait for some time like some 10 seconds(it is configurable). But during waiting UI should not freeze. I wrote code something like this. Here event m_hEvent[1]) will be set by another thread after 10 seconds.The problem is UI still freezes some times!
while(1) { //m_hEvent[1] will be set by another thread after 10 seconds dwRet = MsgWaitForMultipleObjects(1, &m_hEvent[1], FALSE, dwMilliseconds, QS_ALLINPUT); ResetEvent(m_hEvent[1]);
I currently am stuck at getting the needed outcome data from my RFID card. I got it decoded but now I need to do a few more things in order to get the final card number off the back of the card.
The cryptic value was E********B**0**E** (covered to protect card) Decrypting it turned into 0000003048D1263B
Now I have 3 more steps to take in order to get to my wanted card number.
Quote Step 1) Mask off the lower 20-bits (which should give me 0x1263B) I am unsure of how to go about doing that using C++.
I currently am stuck at getting the needed outcome data from my RFID card. I got it decoded but now I need to do a few more things in order to get the final card number off the back of the card.
The cryptic value was E********B**0**E** (covered to protect card)
Decrypting it turned into 0000003048D1263B
Now I have 3 more steps to take in order to get to my wanted card number.
Step 1) Mask off the lower 20-bits (which should give me 0x1263B) I am unsure of how to go about doing that using C++.
Step 2) Divide by 2 to strip off the lower parity bit (which should be 0x931d). And again, I'm unsure of how to go about doing this in C++.
Step 3) Convert hexadecimal value to decimal value (which would equal my wanted card number). This should be easily done using C++ at this point - though hard to confirm that since I am on step 1).
Code: const char* original = "0x931d"; unsigned long n = std::strtoul(original, nullptr, 16);
All of this looks to me like RegEX does to most people - complicated and not understanding why it does what it does but gives the correct output.
I've got an application here which connects over the internet to a webserver and sends some json strings. This is all working already.
Now I want to encode one string via mcrypt (because it seemed the easiest library of all) AES and send it over to the other server where it should get decrypted again and checked for validity.
I'll be using this sample code as "starting base". i've found it on the internet:
The code works as it is, it encodes my string into a ciphered text and displays the text via the display function to stdout.
From previous projects I know I usually used Base64 for transporting strings via json, but in this example the string is encoded into "%d" - so decimals.. It works too, and I dont care if I send a base64 encoded string or these decimals but how would I calculate back the %d encoded string ? So how would a "undisplay()" class look alike ?
Or maybe there is an easier way to transport the string and re-decode it ?
I'd expect something like:
base64string = base64encode(ciphertext); ... send base64string to host2 ... at host2: ciphertext = base64decode(base64string) ... and then mcrypt_decode that ciphertext...
I am writing a program to zip and unzip files using Huffman Encoding. I have successfully Built my Huffman Tree but I am unable to traverse through it to retrieve code the code for each character in sample files using the GetCode() method.
Specifically I am having trouble with the following segment in zip.cpp where I want to go through my built tree and print out each character and it's associated ascii value using the GetCode() method in huffman.cpp
cout << " Here are the codes: "<< endl; for(char ch = char(0); ch <= char(255); ch++) //for(int i= 1; i < 256; i++ ) { //int x = (char) i; if(mytree.inTree('*'))
I'm trying to parse some binary data in the form of an array of bytes and I've come across something that is confusing me related to the representation of data as chars versus ints. It's a bit of a long story, but the byte array contains a mixture of character data and integer data which I' having trouble unravelling. The problem seems to arise from the issue below:
Basically, I am inserting data from an ODBC connection into a PostgreSql database using the COPY query, but the COPY query stops and returns this error...
Quote ERROR: invalid byte sequence for encoding "UTF8": 0x92 CONTEXT: COPY [TableName], line 1: "189572|1-00-1202|1-|00-|1202||AP||1... STATEMENT: COPY [TableName] FROM STDIN (DELIMITER '|', NULL '')
I have a char *pch that points to an integer digit 1 or 2 or ... 9. To get the character that's 1 less, instead of converting to int, minus 1, then converting back to char, I tried (*pch -1) and that seemed to work. I suppose that's because the particular character encoding on my system is such that the digits are encoded in the same order and spacing as the integers they represent. So the question is does this "convenience" feature hold true for all character encoding systems?
program that I am working on. I want to use fgets() in my program so I could handle multiple words from a text(to be able to handle spaces). I get a weird result when running the program.
I'm currently finishing writing some small application. I want to be able to log important information about the program execution to a logfile, and I have several questions.
First of all - I'd prefer to make the part that logs information to a file separate from the code I've already written. So, what interface should I expose to the rest of the program? Is one function void log(const char*); enough?
Another thing that came to my mind; my program runs two threads, and I want to be able to write to the log file from both threads, so the question is: Should I make sure that the writing to the file is mutually exclusive?
And if so, whose responsibility is it to make the logging to the file thread-safe? The actual part that does the logging (void log(const char*) for that matter), or the parts of the program that calls log(const char*) ?
And lastly, and probably less importantly, where is it customary to save the logfile? (the user's home folder maybe?)