Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string conversion

Status
Not open for further replies.

rshtykh

Programmer
Joined
Jul 11, 2003
Messages
3
Location
JP
I want to convert from one string to another and return it as a pointer *out_s? or as a string "res" will be ok as well. But my code doesn't work...
For example, copying res[n] = (char *)&out; doesn't work.
What is wrong with my code?(I am new to C, so please bear with me)
int readAndConvert(char *in_s, char *out_s, int len){
int n=0;
int *out, *in;
char *res = malloc(sizeof(char)*len);
for(n=0; n<len; n++){
in = (int*)(in_s[n]);
out = (int*)(out_s[n]);
convertChar(&in, &out,1);
res[n] = (char *)&out;
}
res;
return 0;
}

int convertChar(int *in_char, int *out_char, int enc){
uchar *table;
table = ki;
if(*in_char & 0x80){
*out_char = table[*in_char & 0x7F];
}
else{
*out_char = *in_char;
}
return 0;
}
 
By the way, I use it in main() like this:

uchar *test = &quot;Test&quot;;
int size = sizeof(test);
uchar *outp = calloc(size, sizeof(uchar));

if(outp!=NULL){
readAndConvert(&test, &outp, 4, 0);
printf(&quot;%s\n&quot;, outp);
// free(outp);
}
and can't free memory by doing free(outp); Seems memory block is gone somewhere because I can't access it. I get error in ASSERT in DBHEAP.c:
/*
* If this ASSERT fails, a bad pointer has been passed in. It may be
* totally bogus, or it may have been allocated from another heap.
* The pointer MUST come from the 'local' heap.
*/
_ASSERTE(_CrtIsValidHeapPointer(pUserData));
 
OK, here's a bit of a tidy up
Code:
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char uchar;

int convertChar(char *in_char, char *out_char, int enc)
{
    uchar *table;
    table = ki;                 // I assume this is something you have
    if (*in_char & 0x80) {
        *out_char = table[*in_char & 0x7F];
    } else {
        *out_char = *in_char;
    }
    return 0;
}

int readAndConvert(char *in_s, char *out_s, int len)
{
    int n = 0;
    for (n = 0; n < len; n++) {
        convertChar(&in_s[n], &out_s[n], 1);
    }
    return 0;
}


int main()
{
    char *test = &quot;Testing 1 2 3&quot;;
    int size = strlen(test);    // sizeof(test) is the size of the pointer, not the length of the string
    char *outp = calloc(size + 1, sizeof(char));

    if (outp != NULL) {
        readAndConvert(test, outp, size);
        outp[size] = '\0';      // make it a proper string
        printf(&quot;%s\n&quot;, outp);
        free(outp);
    }
    return 0;
}
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top