Thread: Substitution problem...

  1. #16
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Ok thanks!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Aha, I know why. It's because strings hold chars, not unsigned chars.

    From the lookup table:
    Code:
    27, 144, 241, 223, 244, 24, 1
    Since char is signed on my platform and yours, 241, 223, and anything above 127 cannot be stored properly. It's stored, but comes out as a negative number, and in dSubstitution you end up accessing a negative array index.

    This works:
    Code:
    #include <iostream>
    #include <string>
    
    using std::string;
    
    void eSubstitution(unsigned char *text)
    {
    	int subTable[256] = {27, 144, 241, 223, 244, 24, 1, 60, 149, 154, 50, 174, 70, 132, 
    167, 86, 38, 188, 3, 233, 128, 130, 39, 118, 175, 22, 46, 84, 191, 42, 19, 113, 153, 225, 227, 
    110, 114, 254, 25, 230, 181, 185, 207, 2, 226, 243, 150, 236, 187, 14, 77, 116, 249, 137, 182, 
    133, 125, 17, 152, 197, 143, 105, 208, 57, 168, 119, 36, 224, 120, 189, 146, 90, 166, 101, 210
    , 201, 11, 82, 196, 23, 56, 43, 44, 159, 12, 157, 123, 5, 156, 96, 97, 160, 242, 9, 171, 121, 220
    , 95, 85, 41, 212, 237, 69, 139, 20, 228, 194, 59, 253, 195, 203, 161, 7, 200, 192, 169, 115, 21
    , 204, 91, 30, 75, 164, 252, 172, 117, 88, 13, 89, 247, 32, 108, 206, 178, 65, 81, 198, 145, 147
    , 170, 190, 209, 54, 67, 0, 179, 79, 6, 26, 4, 64, 124, 98, 62, 29, 193, 127, 104, 45, 140, 74, 
    218, 93, 134, 176, 232, 28, 202, 221, 234, 49, 35, 235, 155, 68, 217, 102, 33, 142, 251, 58, 
    231, 131, 135, 63, 163, 205, 87, 112, 165, 222, 245, 40, 72, 141, 246, 186, 92, 238, 255, 173, 
    37, 111, 240, 15, 76, 126, 248, 199, 122, 148, 213, 16, 48, 34, 78, 103, 47, 158, 219, 52, 73, 
    18, 106, 94, 183, 99, 53, 214, 83, 177, 107, 100, 211, 61, 66, 51, 138, 239, 31, 80, 8, 250, 71, 
    55, 109, 184, 229, 215, 10, 162, 151, 216, 180, 129, 136};
    	//substitute
    	for(int i=0; i<4; i++)
    	{
    		text[i] = ((char)subTable[((int)text[i])]);
    	}
    }
    
    void dSubstitution(unsigned char *text)
    {
    	int subTable[256] = {144, 6, 43, 18, 149, 87, 147, 112, 241, 93, 249, 76, 84, 127, 
    49, 204, 212, 57, 222, 30, 104, 117, 25, 79, 5, 38, 148, 0, 166, 154, 120, 239, 130, 177, 214,
     171, 66, 201, 16, 22, 192, 99, 29, 81, 82, 158, 26, 217, 213, 170, 10, 236, 220, 227, 142, 244
    , 80, 63, 180, 107, 7, 234, 153, 184, 150, 134, 235, 143, 174, 102, 12, 243, 193, 221, 160, 121
    , 205, 50, 215, 146, 240, 135, 77, 229, 27, 98, 15, 187, 126, 128, 71, 119, 197, 162, 224, 97, 
    89, 90, 152, 226, 232, 73, 176, 216, 157, 61, 223, 231, 131, 245, 35, 202, 188, 31, 36, 116, 51
    , 125, 23, 65, 68, 95, 209, 86, 151, 56, 206, 156, 20, 254, 21, 182, 13, 55, 163, 183, 255, 53, 
    237, 103, 159, 194, 178, 60, 1, 137, 70, 138, 210, 8, 46, 251, 58, 32, 9, 173, 88, 85, 218, 83, 
    91, 111, 250, 185, 122, 189, 72, 14, 64, 115, 139, 94, 124, 200, 11, 24, 164, 230, 133, 145, 
    253, 40, 54, 225, 246, 41, 196, 48, 17, 69, 140, 28, 114, 155, 106, 109, 78, 59, 136, 208, 113, 
    75, 167, 110, 118, 186, 132, 42, 62, 141, 74, 233, 100, 211, 228, 248, 252, 175, 161, 219, 96, 
    168, 190, 3, 67, 33, 44, 34, 105, 247, 39, 181, 165, 19, 169, 172, 47, 101, 198, 238, 203, 2, 92
    , 45, 4, 191, 195, 129, 207, 52, 242, 179, 123, 108, 37, 199};
    	//substitute
    	for(int i=0; i<4; i++)
    	{
    		text[i] = (subTable[((int)text[i])]);
    	}
    }
    
    int main() {
        unsigned char from1[] = "abcd", from2[] = "john", from3[] = "zueq";
        
        std::cout << from1 << ", " << from2 << ", " << from3 << std::endl;
        eSubstitution(from1);
        eSubstitution(from2);
        eSubstitution(from3);
        std::cout << from1 << ", " << from2 << ", " << from3 << std::endl;
        dSubstitution(from1);
        dSubstitution(from2);
        dSubstitution(from3);
        std::cout << from1 << ", " << from2 << ", " << from3 << std::endl;
        //std::cout << (int)from2.at(1);
        
        std::cin.get();
        return 0;
    }
    But if you get rid of all instances of the word "unsigned", it doesn't.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    What you did using d then esubstitution, for me the last 3 worked with the regular way, e then d, but the opposite was very messed up. Only one char came out right.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  4. #19
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    There's no way to do it with strings? Cause my whole algorithm uses strings in every single other part of it. Maybe convert the string to a char array temporarily then convert it back?
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, you can do it with strings, you just have to create a lookup table from 0-127. Then you can use isprint() to make sure that each character is within that range.

    Unless you need to process unsigned char values, then you wouldn't be able to use a string.

    [edit] And I still think that a lookup table is a bad idea. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could always do it like this.
    Code:
    #include <climits>
    
    void eSubstitution(string &text)
    {
        /* snip */
    	//substitute
    	for(int i=0; i<4; i++)
    	{
    		text.at(i) = ((char)subTable[((int)text.at(i))-CHAR_MIN]) + CHAR_MIN;
    	}
    }
    
    void dSubstitution(string &text)
    {
        /* snip */
    	//substitute
    	for(int i=0; i<4; i++)
    	{
    		text.at(i) = ((char)subTable[((int)text.at(i))-CHAR_MIN]) + CHAR_MIN;
    	}
    }
    I've tested it, and that works, I think.

    [edit] No, it doesn't really. Give me a minute. [/edit]

    [edit=2] Yes, it does work! Whew!
    Code:
    #include <iostream>
    #include <string>
    #include <climits>
    
    using std::string;
    
    void eSubstitution(string &text)
    {
    	int subTable[256] = {27, 144, 241, 223, 244, 24, 1, 60, 149, 154, 50, 174, 70, 132, 
    167, 86, 38, 188, 3, 233, 128, 130, 39, 118, 175, 22, 46, 84, 191, 42, 19, 113, 153, 225, 227, 
    110, 114, 254, 25, 230, 181, 185, 207, 2, 226, 243, 150, 236, 187, 14, 77, 116, 249, 137, 182, 
    133, 125, 17, 152, 197, 143, 105, 208, 57, 168, 119, 36, 224, 120, 189, 146, 90, 166, 101, 210
    , 201, 11, 82, 196, 23, 56, 43, 44, 159, 12, 157, 123, 5, 156, 96, 97, 160, 242, 9, 171, 121, 220
    , 95, 85, 41, 212, 237, 69, 139, 20, 228, 194, 59, 253, 195, 203, 161, 7, 200, 192, 169, 115, 21
    , 204, 91, 30, 75, 164, 252, 172, 117, 88, 13, 89, 247, 32, 108, 206, 178, 65, 81, 198, 145, 147
    , 170, 190, 209, 54, 67, 0, 179, 79, 6, 26, 4, 64, 124, 98, 62, 29, 193, 127, 104, 45, 140, 74, 
    218, 93, 134, 176, 232, 28, 202, 221, 234, 49, 35, 235, 155, 68, 217, 102, 33, 142, 251, 58, 
    231, 131, 135, 63, 163, 205, 87, 112, 165, 222, 245, 40, 72, 141, 246, 186, 92, 238, 255, 173, 
    37, 111, 240, 15, 76, 126, 248, 199, 122, 148, 213, 16, 48, 34, 78, 103, 47, 158, 219, 52, 73, 
    18, 106, 94, 183, 99, 53, 214, 83, 177, 107, 100, 211, 61, 66, 51, 138, 239, 31, 80, 8, 250, 71, 
    55, 109, 184, 229, 215, 10, 162, 151, 216, 180, 129, 136};
    	//substitute
    	for(int i=0; i<4; i++)
    	{
            std::cout << '[' << (int)text[i];
    		text[i] = ((char)subTable[((int)text[i])-CHAR_MIN])+CHAR_MIN;
    		std::cout << ',' << (int)text[i] << ']';
    	}
    	
    	std::cout << std::endl;
    }
    
    void dSubstitution(string &text)
    {
    	int subTable[256] = {144, 6, 43, 18, 149, 87, 147, 112, 241, 93, 249, 76, 84, 127, 
    49, 204, 212, 57, 222, 30, 104, 117, 25, 79, 5, 38, 148, 0, 166, 154, 120, 239, 130, 177, 214,
     171, 66, 201, 16, 22, 192, 99, 29, 81, 82, 158, 26, 217, 213, 170, 10, 236, 220, 227, 142, 244
    , 80, 63, 180, 107, 7, 234, 153, 184, 150, 134, 235, 143, 174, 102, 12, 243, 193, 221, 160, 121
    , 205, 50, 215, 146, 240, 135, 77, 229, 27, 98, 15, 187, 126, 128, 71, 119, 197, 162, 224, 97, 
    89, 90, 152, 226, 232, 73, 176, 216, 157, 61, 223, 231, 131, 245, 35, 202, 188, 31, 36, 116, 51
    , 125, 23, 65, 68, 95, 209, 86, 151, 56, 206, 156, 20, 254, 21, 182, 13, 55, 163, 183, 255, 53, 
    237, 103, 159, 194, 178, 60, 1, 137, 70, 138, 210, 8, 46, 251, 58, 32, 9, 173, 88, 85, 218, 83, 
    91, 111, 250, 185, 122, 189, 72, 14, 64, 115, 139, 94, 124, 200, 11, 24, 164, 230, 133, 145, 
    253, 40, 54, 225, 246, 41, 196, 48, 17, 69, 140, 28, 114, 155, 106, 109, 78, 59, 136, 208, 113, 
    75, 167, 110, 118, 186, 132, 42, 62, 141, 74, 233, 100, 211, 228, 248, 252, 175, 161, 219, 96, 
    168, 190, 3, 67, 33, 44, 34, 105, 247, 39, 181, 165, 19, 169, 172, 47, 101, 198, 238, 203, 2, 92
    , 45, 4, 191, 195, 129, 207, 52, 242, 179, 123, 108, 37, 199};
    	//substitute
    	for(int i=0; i<4; i++)
    	{
            std::cout << '[' << (int)text[i];
    		text[i] = (subTable[((int)text[i])-CHAR_MIN])+CHAR_MIN;
    		std::cout << ',' << (int)text[i] << ']';
    	}
    	
    	std::cout << std::endl;
    }
    
    int main() {
        string from1 = "abcd", from2 = "john", from3 = "zueq";
        
        std::cout << from1 << ", " << from2 << ", " << from3 << std::endl;
        eSubstitution(from1);
        eSubstitution(from2);
        eSubstitution(from3);
        std::cout << from1 << ", " << from2 << ", " << from3 << std::endl;
        dSubstitution(from1);
        dSubstitution(from2);
        dSubstitution(from3);
        std::cout << from1 << ", " << from2 << ", " << from3 << std::endl;
        //std::cout << (int)from2.at(1);
        
        std::cin.get();
        return 0;
    }
    Enjoy. [/edit]
    Last edited by dwks; 06-27-2007 at 04:08 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #22
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    i did it a bit simpler (the conversion bit) and it works great.

    Thanks so much DWKS, as always, you're a great help!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM