Thread: Help~~something went wrong with my encoding code

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    1

    Help~~something went wrong with my encoding code

    hi guys, im new to C programming here
    i was given an assignment to encode a 60 characters string using a 2d array substitution box.

    I have fix my code below to generate output from sbox[0] (row0),but it gave me numbers from sbox[3] instead,
    even from sbox[3], the digit can be convert correctly but not from column a-f

    Anyone knows which part goes wrong?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<time.h>
    
    
    main()
    
    
    {
        int box_id, i,col;
        char input[100] = { "1312e7972714d01a46664a3523366a2f58560b42c485853674561c360d77" };
        char sbox[8][16] = {
            { '2', '4', 'c', '5', '9', 'a', '3', '7', 'e', '1', '0', 'f', '8', '6', 'b', 'd' },
            { '7', 'e', 'a', '4', '1', 'd', '9', 'f', '2', 'b', '6', '0', 'c', '5', '8', '3' },
            { '3', '8', 'f', '1', 'a', '6', '5', 'b', 'd', 'e', '4', '2', '7', '0', '9', 'c' },
            { '8', 'f', '7', '9', '3', 'd', '4', 'b', 'c', '2', '0', 'a', '1', '5', '6', 'e' },
            { '1', 'f', '8', '3', 'c', '0', 'b', '6', '2', '5', '4', 'e', '9', 'a', '7', 'd' },
            { '1', 'd', 'f', '0', 'e', '8', '2', 'c', '7', '4', 'b', 'a', '3', '5', '9', '6' },
            { 'a', '3', '4', '7', 'b', '8', 'd', '2', '6', 'c', 'f', 'e', '0', '5', '1', '9' },
            { '3', 'c', '4', '2', '7', 'a', '0', '8', '5', 'b', 'f', '1', 'd', '6', '9', 'e' }
        };
    
    
    
    
        printf("%s", input);
        printf("\n\n");
    
    
        //srand(time(NULL));
        //box_id=rand()%7;
        //printf("%d\n\n\n",box_id);
    
    
        for (i = 0; i < 60; i++)
        {
            
    
    
            if (input[i]=='a')
            {
                col = 10;
            }
    
    
            else if (input[i] == 'b')
            {
                col = 11;
            }
    
    
            else if (input[i] == 'c')
            {
                col = 12;
            }
    
    
            else if (input[i] == 'd')
            {
                col = 13;
            }
    
    
            else if (input[i] == 'e')
            {
                col = 14;
            }
    
    
            else if (input[i] == 'f')
            {
                col = 15;
            }
    
    
            else
            {
                col = i;
            }
            printf("%c", sbox[0][input[col]]);
        }
    
    
        printf("\n\n");
        system("pause");
    Last edited by yssmile; 10-14-2015 at 05:43 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The
    Code:
    else
            {
                col = i;
            }
    should be
    Code:
    else
            col = input[i] - '0'
    because you are wanting the numeric equivalent of digits '0' to '9'.

    Also you'd want to output 'sbox[0][col]', NOT indexed by input[col].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using facet to covert from encoding to encoding
    By Dave11 in forum C++ Programming
    Replies: 6
    Last Post: 07-28-2015, 07:33 AM
  2. Encoding a data structure based on TLV encoding
    By Sajas K K in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2013, 10:39 PM
  3. How to convert string in url encoding to html encoding?
    By Jerel2k11 in forum C Programming
    Replies: 6
    Last Post: 11-06-2011, 09:05 AM
  4. Encoding/decoding morse code
    By bigboybz in forum C++ Programming
    Replies: 15
    Last Post: 02-17-2011, 06:14 AM
  5. Help with code for encoding and decoding letters
    By decxan in forum C Programming
    Replies: 1
    Last Post: 10-28-2010, 12:39 PM