Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8661709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:33:45+00:00 2026-06-12T16:33:45+00:00

I want to encode a string in C using a key string. The encodeMSG

  • 0

I want to encode a string in C using a key string.
The encodeMSG function returns int array, so that:
intArr[i] = the int value of msg[i] + the int value of key[i].
If the length of the key string is shorter the the msg string, it should go back to the beginning (cycle).

I’m not sure how I should do this although it doesn’t seem too complicated.
I also wasn’t sure whether I should use atoi(msg + i) or a simple cast like (int)(*(msg + i)).

int *encodeMSG(char *msg, char *key)
{
   int i, msgLen;
   int *encodedArr = (int *)malloc(strlen(msg) * sizeof(int));
   char *keyBackup = key;
   msgLen = (strlen(msg));

   for (i = 0; i < msgLen; ++i)
   {
       if (*(key + i) == '\0')
           key = keyBackup;

       *(encodedArr + i) = *(msg + i); //creating an integer-represented array of the char array [msg]
       *(encodedArr + i) += *(key + i); //adding the [key] array integer values to the integer-represented array of the message
   }

   return encodedArr;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:33:47+00:00Added an answer on June 12, 2026 at 4:33 pm

    Try this:

    int *encodeMSG(char *msg, char *key)
    {
        char *key0 = key;
        int *result = (int*) malloc( sizeof(int)*(strlen(msg)+1) );
          // +1 to include the final zero of msg
        int *result0 = result;
        while( *msg ) // i assume it's zero-terminated
        {
            *result++ = ((int)*msg) + ((int)*key);
            ++msg;
            ++key;
            if(!*key)key=key0; // reset it to the beginning of the key
        }
        *result = 0; // the final zero
        return result0;
    }
    

    This will use the ascii values directly, atoi is not useful here.
    Note that *msg is the actual char msg points to. As msg is zero-terminated, the end of the string is 0 of false. Same applys for *key and when *key == 0 (or !*key) then just reset it to the start of the key phrase.

    By the way: It returns an int[] with 1 more int than chars in msg, last int is zero.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of bytes (any length), and I want to encode this
`I want to encode my string using .net and pass it to a java
How can I encode string using PHP Base64_encode function with passing some Salt String
I want to encode an image into a string using the base64 module. I've
I want to encode and decode Bitmap object in string base64 . I use
I want to send base64 string through ajax I encoded it through JavaScript function
I have string that displays UTF-8 encoded characters, and I want to convert it
I want to encode DNS protocol header using C and create a UDP datagram.
I am using the md5 function to hash a string into a 32 digit
I'm using as3corelib to decode/encode JSON strings. In my little experiment I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.