Base10 to Base2 String conversion.

Greetings. This is my first official post on the C++ Forums. I have very little experience with the C++ language - but I've been sitting on an idea for a program for a couple years now. So I've decided to try to make a prototype of the most basic function of the program I have in mind. Something to build from and test with.

Again, I have very little programming ability, I've never taken any formal classes or ever completed one of those "For Dummies" books. So what I have managed to scrape by - I've gleaned from other sources.

So without cluttering this post up with too much back story the program needs to do the following:

Step 1: Load from a file with an undetermined amount of ASCII decimal characters (0-9) approximately 8192 characters. (the file could hold millions of characters - but it just needs to grab the first 8192.

Step 2: Process the 8192 characters grabbed - by applying 1022 different keys. The keys would act as switches to turn the decimal characters directly into a binary string. For instance if the string included the numbers 1923, and the key was 1010010000 (0123456789) then the output of the string 1923 would be 0010. I want to apply 1022 out of a possible 1024 keys, excluding the keys that would produce all 1's or all 0's.

Step 3: For each key applied to the string - I want to create a file containing the binary output. So there would be 1022 files created. And a text log file created to store the filename of the file created and 2 hash values (CRC32 and ADLER32 should work) With good formatting so it looks organized when viewed in notepad.

Step 4: A window should pop up - asking if I would like to proceed to the next set of characters in the file. If yes is selected, the program should repeat, but this time - it should grab 8192 characters from the file, starting at the 2nd character in the file. (so it only shifted over 1 character) A new folder with the time/date should be created and filled with the next 1022 filed + log file. This process should be able to be repeated either manually by pressing yes every time, or automatically by selecting a "don't ask again" checkbox.

Sorry if that's a lot of information. But that's what I need. And I'm not sure how to get there from here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 #include <iostream>
#include <string>
#include <fstring>

Using namespace std:



int main() {
	
  ofstream fout("C:\\Pi.txt");
  string my_input, my_ouput;

  // Initialize my_input somehow

 for (i = 0; i < my_input.size(); ++i) {
     my_output = my_output + (char) convert_char(my_input[i]);
}


int convert_char(int c) {
   
     if (keyed_value(c - '0'))   // Otherwise, return a '1' or '0'.
          return '1';
     else
          return '0';
}

int key_array[10];

bool get_key_value(int n) {
           return (key_array[n] == 1);
     }

bool get_key_value(int n) {
          int i = 9 - n;
          return (key_array[i] == '1');
     }

// Return a string that contains all the values in the argument, key_array, but
//  with embedded spaces removed.

     string get_temporary_string(string key_array) {
           string temp_str;
           for (i = 0; i < key_array.length(); ++i) {
               if (key_array[i] != ' ') {
                    temp_str = temp_str + (char) key_array[i];
               }
           ]
           return temp_str;
     }
     }


I realize there's a lot to do on this - and I'm not asking anyone to write it for me or anything like that - I just need a little help with the different pieces so I can put it together. Any help would be appreciated - I thank you all in advance.

-Nyxe
So without cluttering this post up with too much back story the program needs to do the following:

Never do that, again. You get help by asking short and specific questions, like "how do I convert from base 10 to base 2?" then maybe post your own attempt which can be verified by others and corrected.

So where exactly are you stuck? I can't speak for others, but I myself don't have the patience to decipher a wall of prose to figure out what you need me to help you with.
I didn't look at the code yet. I was trying to understand this paragraph:
Step 2: Process the 8192 characters grabbed - by applying 1022 different keys. The keys would act as switches to turn the decimal characters directly into a binary string. For instance if the string included the numbers 1923, and the key was 1010010000 (0123456789) then the output of the string 1923 would be 0010. I want to apply 1022 out of a possible 1024 keys, excluding the keys that would produce all 1's or all 0's.

I'm afraid I didn't follow this at all.
Questions:
• How does 1010010000 relate to 0123456789? When I convert 1010010000 from binary to decimal I get 656. Or 0123456789 converted to binary gives 111010110111100110100010101
• In turn how does the above convert 1923 to 0010?
• Which are "the keys that would produce all 1's or all 0's" - I can guess (though I shouldn't have to) what these might be in binary, but I don't know what decimal number this would correspond with?

Last edited on
Just from the example, it seems like the "key" maps the numbers 0123456789 each to either 0 or 1. The example key, then, lines up like this:
1010010000
0123456789

0, 2, and 5 map to 1, and the other digits map to 0. Hence why 1923 maps to 0010. Does that make sense?

Following from that, the "keys that produce all 1's or all 0's" would probably be the keys 1111111111 and 0000000000.

Never do that, again. You get help by asking short and specific questions, like "how do I convert from base 10 to base 2?" then maybe post your own attempt which can be verified by others and corrected.


- I apologize if you felt offended by the wording of my question. It wasn't my intention to make you feel like I was wasting your time.

So where exactly are you stuck? I can't speak for others, but I myself don't have the patience to decipher a wall of prose to figure out what you need me to help you with.


I'm stuck at the part where I don't know how to program in C++ at all. The only thing I have is a map in my head of how I want the program to work. And some snippets of code I've gathered that I *BELIEVE* pertain to my program. Unfortunately - It appears I have come off like a bit of an ass.


Just from the example, it seems like the "key" maps the numbers 0123456789 each to either 0 or 1. The example key, then, lines up like this:
1010010000
0123456789
0, 2, and 5 map to 1, and the other digits map to 0. Hence why 1923 maps to 0010. Does that make sense?

Following from that, the "keys that produce all 1's or all 0's" would probably be the keys 1111111111 and 0000000000.


You are absolutely correct Zhuge. Again I'm sorry for not being clear in my question.
If you have no idea how to program in C++ at all, then I personally think you'd be better served staring with something simpler. It's good that you have an idea for a program you want to create, but if you have an algorithm but can't figure out how to translate that into code, that's a sign that you need to improve your skills with the language. Try reading the tutorial on this site; it'll get you a very good basic foundation for the language and then you can put that knowledge to use in the implementation of the algorithm you have in your head.
I apologize if you felt offended by the wording of my question. It wasn't my intention to make you feel like I was wasting your time.

Don't be ridiculous, I and others have the intention to help you.

Step 2: Process the 8192 characters grabbed - by applying 1022 different keys. The keys would act as switches to turn the decimal characters directly into a binary string. For instance if the string included the numbers 1923, and the key was 1010010000 (0123456789) then the output of the string 1923 would be 0010. I want to apply 1022 out of a possible 1024 keys, excluding the keys that would produce all 1's or all 0's.

Hmm.
So when applying a "key", you simply transform the present digits into 1 and the missing digits into 0?

For example like this?
0123456789
0001000000

input = 3354
output = 1100

Correct Catfish4.

And I just want to run them in sequence from 0000000001 to 1111111110. Excluding 0000000000 and 1111111111.

And thank you for the advice Zhuge. I will definitely dig in and take a look. Still hoping to get some input from the forums.

I really appreciate the help - regardless.

-Nyxe
Well a relatively straightforward way to do this is by using an std::map of char to char.

A map is a data structure associating one value (of a type) with another value (of possibly another type).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <map>
#include <ostream>
#include <stdexcept>
#include <string>

std::map<char, char> make_map_from_key(const std::string &key)
{
    const std::string digits("0123456789");
    std::map<char, char> dictionary;

    if (key.length() != digits.length()) // encode the given digits, right?
        throw std::length_error("Bad key length.");

    for (std::string::const_iterator ci = key.begin(), cd = digits.begin(); ci != key.end(); ++ci, ++cd)
        if (*ci == '1' || *ci == '0')
            dictionary[*cd] = *ci;
        else
            throw std::domain_error("Bad key contents.");

    return dictionary;
}

std::string encode(const std::string &s, const std::string &key)
{
    std::string r;
    const std::map<char, char> dictionary = make_map_from_key(key);

    for (std::string::const_iterator ci = s.begin(); ci != s.end(); ++ci)
        r += dictionary.at(*ci);

    return r;
}

int main()
{
    // ................... 0123456789
    const std::string key("0010100010");
    std::cout << encode("2780", key) << std::endl;
}
1010

Last edited on
Thank you very much for your help Catfish4.

Would it be possible for you to comment and explain each part so I can better understand what's going on? I'm sorry for asking - but it's a little confusing.
Would it be possible for you to comment and explain each part so I can better understand what's going on?


If you only began learning C++, I strongly suggest that you read the Tutorial on this site.
http://www.cplusplus.com/doc/tutorial/

After that, you'll need to get comfortable with the C++ library. The library is a collection of prebuilt stuff that you can use in your programs (for instance std::string and std::map).
http://www.cplusplus.com/reference/

On the topic of the program I'm not 100% sure if this kind of "base 10 to base 2 conversion" is truly what you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// inclusion of needed header files
#include <iostream> // for std::cout
#include <map> // for std::map
#include <ostream> // for std::endl
#include <stdexcept> // for std::length_error and std::domain_error
#include <string> // for std::string

// because I'm not
// using namespace std;
// I need to qualify the names by prefixing them with "std::"

// function builds and returns a map of char to char from a key string
// the key string is passed as a const reference for speed
// if it were passed simply as (std::string key) it would be needlessly copied
std::map<char, char> make_map_from_key(const std::string &key)
{
    const std::string digits("0123456789"); // helper string containing digits
    std::map<char, char> dictionary; // this map will associate char to char

    // key lengths must be 10, which obviously coincides with the length of the digits string
    if (key.length() != digits.length()) // encode the given digits, right?
        throw std::length_error("Bad key length."); // throw an exception to signal the error

    // iterate the contents of the key string by using a constant iterator (a const_iterator type)
    // constant iterators make it clear that we don't want to modify the contents;
    // also iterate the digits string as we go along
    for (std::string::const_iterator ci = key.begin(), cd = digits.begin(); ci != key.end(); ++ci, ++cd)
        // the key string is only supposed to contain '1's and '0's
        if (*ci == '1' || *ci == '0')
            dictionary[*cd] = *ci; // associate the current decimal digit with the 1 or 0
            // for instance, if 3 gets associated with 0 this would be like dictionary[3] = 0
            // if this is confusing, remember that dictionary is not an array but an std::map
        else
            // well the key contains something other than '1' and '0', so we complain about that
            throw std::domain_error("Bad key contents.");

    return dictionary;
}

std::string encode(const std::string &s, const std::string &key)
{
    std::string r;
    const std::map<char, char> dictionary = make_map_from_key(key);

    for (std::string::const_iterator ci = s.begin(); ci != s.end(); ++ci)
        r += dictionary.at(*ci);

    return r;
}

int main()
{
    // ................... 0123456789
    const std::string key("0010100010");

    std::cout << encode("2780", key) << std::endl;
}
Topic archived. No new replies allowed.