Hiie

i have to convert unsigned char to char array. i m using ultoa() function that converts unsigned long char to a string. but it is giving me an error of Type Mismatch.

actualy i have tsend my data to server side where it is recieving in char array. so on client side data is stored in char array. but now i have to encrypt that data so encrypted data is unsigned char *

and i have to convert that unsigned char * to char array.... oherwise it is giving segmentation fault if i do not convert.

plzz plzzz helpp...
i m posting my code also.

//encrypt the message shorter than 8 bytes.
	  strcpy((unsigned char *) input, plaintext);
 	  DES_ecb_encrypt(input, output, mysched, 1);
 	  ptr = (unsigned char *) output;
 	  for(j=0; j<8; j++) printf("%X", *(ptr+j)); printf("\n");
	send_data=ultoa(ptr);
	// sendto fucntion used to send data to server
	sendto(sock,send_data, strlen(send_data), 0,
              (struct sockaddr *)&server_addr, sizeof(struct sockaddr));

Please post the way you declared those variables. Is input a character array? You generally don't have to do a thing to convert char* to unsigned char*. typecasting is sufficient. And ultoa() takes an unsigned long int as the first parameter, not an unsigned char.

Edited 6 Years Ago by Ancient Dragon: n/a

hi sorry for posting my other code so late.. heres how i decleared the variables

unsigned char *plaintext;
 
  int j;
  unsigned char *ptr;
  DES_cblock *key;
  DES_cblock *input;
  DES_cblock *output;
 
  DES_key_schedule *mysched;
  mysched = malloc(sizeof(DES_key_schedule));
  key = malloc(sizeof(DES_cblock));
  strcpy((unsigned char *) key, "12345678");
  input = malloc(sizeof(DES_cblock));
  output = malloc(sizeof(DES_cblock));
  
  DES_set_key(key, mysched);

heres how variables used in sendto() are declared

int sock,bytes_read,addr_len;
struct sockaddr_in server_addr;
struct hostent *host;
char send_data[1024];

the code to encrypt the data in other program is working fine. but when i put it here it gives error of 'TYpe Miss Match at

send_data=utoa(ptr);

Check out the use of the "utoa" function and use it correctly in ur code.
You can check with the man pages or can simply google for the information. Still I am posting the syntax and use of the function 4 u.

#include <stdlib.h>
char *utoa( unsigned int value, char *buffer, int base);

The utoa() function converts the unsigned binary integer value into the equivalent string in base "base" notation, storing the result in the character array pointed to by "buffer".
A null character is appended to the result. The size of buffer must be at least (8 * sizeof(int) + 1) bytes when converting values in base 2.
So, our function "converts an unsigned binary integer to a string".

Here's a simple example to clear up the idea...

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

void main()
{
    int base;
    char buffer[18];

    for(base = 2; base <= 16; base = base+2)
        printf("%2d %s\n", base, utoa((unsigned) 12765, buffer, base));
}

which produces the output:

2 11000111011101
4 3013131
6 135033
8 30735
10 12765
12 7479
14 491b
16 31dd

This article has been dead for over six months. Start a new discussion instead.