Home > Enterprise >  how to convert long to char in C?
how to convert long to char in C?

Time:03-27

This is all my code. I compiled it and tested it with the word 'Hello' , it encrypts and decrypts correctly. It doesnt seem to work for bigger values.

And thanks in advance for everyone's answers.

All the function are compiled and tested.

(I am just gonna type a few more words so they let me post it :p because apparently it's all code)

        long modpow_rec(long a, long m, long n) /* returns a^m % n*/
        {
            long res;
            if(m==1)
            {
                return a%n;
            }
            if(m%2==0)
            {
                res=modpow_rec(a, m/2, n);
                res= (res*res)%n;
            }
            else
            {
                res=a%n;
                res=(res*modpow_rec(a, m-1, n))%n;
            }
            return res;
        }
        
        int witness( long a , long b , long d , long p ) { /Returns 1 if a is a witness of Miller of p*/
            long x = modpow_rec (a ,d , p ) ;
            if ( x == 1) {
                return 0;
            }
            for ( long i = 0; i < b ; i   ) {
                if ( x == p -1){
                    return 0;
                }
                x = modpow_rec (x ,2 , p ) ;
            }
            return 1;
        }
        
        long rand_long( long low , long up ) {
            return rand () % ( up - low  1)   low ;
        }
        
        int is_prime_miller ( long p , int k ) { /*returns 1 if p is a prime*/
            if ( p == 2) {
                return 1;
            }
            if (!( p & 1) || p <= 1) {
                return 0;
            }
            long b = 0;
            long d = p - 1;
            while (!( d & 1) ) { 
                d = d /2;
                b = b  1;
            }
            long a ;
            int i ;
            for ( i = 0; i < k ; i   ) {
                a = rand_long (2 , p-1) ;
                if ( witness (a ,b ,d , p ) ) {
                    return 0;
                }
            }
            return 1;
        }
        
        long random_prime_number(int low_size, int up_size, int k ){
            long min = powl(2, low_size-1); /*smallest long coded with exactly low_size bytes*/
            long max = powl(2, up_size)-1; /*biggest long coded with exactly up`enter code here`_size bytes*/
            long alea= rand_long(min, max);
            int premier=is_prime_miller(alea,k);
            while (premier!=1){
                alea = rand_long(min, max);
                premier=is_prime_miller(alea,k);
            }
            return alea;
        }
        
        long extended_gcd(long s, long t, long *u, long *v){
            if (s==0){
                *u=0;
                *v=1;
                return t;
            }
            long uPrim, vPrim;
            long gcd=extended_gcd(t%s, s, &uPrim,&vPrim);
            *u=vPrim - (t/s)*uPrim;
            *v=uPrim;
            return gcd;
        }
        
        void generate_key_values(long p, long q, long *n, long *s, long *u){
            *n=(p*q);
            long t=(p-1)*(q-1);
            long v=0;
            *s=rand_long(2,t-1);
            long pgcd_st=extended_gcd(*s,t,u,&v);
            while (pgcd_st!=1){
                *s=rand_long(2,t-1);
                pgcd_st=extended_gcd(*s,t,u,&v);
            }
        }
    
    long *encrypt(char *chaine, long s, long n){
            int size = strlen(chaine);
            long *c = (long*)malloc(size*sizeof(long));
            for(int i=0; i<size; i  ){
                c[i]=modpow((int)chaine[i], s, n);
                printf("(int)chaine[i] = %d\n", (int)chaine[i]);
            }
            return c;
        }
    
    /*In the function below, printf("res= %s",res) prints symbols and not the actual letters I am trying to decrypt. Also, res[i] is giving me negatif values.*/
    
        char* decrypt(long *crypted, int size, long u, long n){
            char *res = (char)malloc((size 1)*sizeof(char));
            for(int i=0; i<size; i  ){
                res[i]=(char)modpow(crypted[i], u, n); /* I believe the problem is in the casting */
                printf("res[i] = %d\n", (int)res[i]);
            }
            res[size]='\0';
            return res;
        }

CodePudding user response:

The code you posted can't be reproduced.

If, we skip that, you are probably looking for a way to convert a long to a string or a NULL terminated array of characters.

You can use a printf family function.

  • sprintf()
#include <stdio.h>

int main(void)
{
    long x = 5000;
    char buff[500];

    sprintf(buff, "%ld", x);
    
    return 0;
}

Disadvantage : buff can only hold up to 500 characters. In this case, it shouldn't be an issue. But if you ever find yourself with more than 500 characters in to be printed in to buff, this will be an issue.

  • snprintf()
#include <stdio.h>

int main(void)
{
    long x = 500;
    char buff[500];

    snprintf(buff, sizeof(buff), "%ld", x);

    return 0;
}

In this case, you provide the size of buff, so you don't have to worry about going out of bounds.

  • asprintf()

If you are under GNU or BSD, asprintf() provides automatic memory allocation in case you don't want to worry about the array being bigger or smaller than you originally expected.

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>


int main(void)
{
    long x = 500;
    char *buff;

    asprintf(&buff, "%ld", x);

    free(buff); // dont forget to free

    return 0;
}

CodePudding user response:

long *encrypt(char chaine, long s, long n){
    int size = strlen(chaine);

Since chaine is a char, it makes no sense to pass it to strlen. The strlen function requires a string, not a character.

    long c = (long)malloc(size*sizeof(long));

What's the point of casting a memory address to a long?

for(int i=0; i<size; i  ){
    c[i]=modpow((int)chaine[i], s, n);

You made c a long. What does c[i] mean since c is not an array?

    printf("(int)chaine[i] = %d\n", (int)chaine[i]);

Umm, chaine is a char, that is, a character. What does chaine[i] mean?

char* decrypt(long *crypted, int size, long u, long n){
    char res = (char)malloc((size 1)*sizeof(char));

Why is res a char? Why are you casting the location of the memory that you allocated to a character type?

    for(int i=0; i<size; i  ){
        res[i]=(char)modpow(crypted[i], u, n); /* I believe the problem is in the casting */

Since res is a char and not an array or pointer, res[i] doesn't make any sense.

Bluntly, you need to learn c if you want to write c code.

  • Related