Keith Keith - 3 months ago 46
Pascal Question

Pascal String to cstring

Trying to write a simple pascal to cstring conversion method. The method takes a void* ptr to the pascal string. The resulting cstring returns the correct number of characters for the pascal string that gets passed. But all it contains is the hex value for the length of the string. For instance, a pascal strings first element is the length of the string. So if the number in the first element is 15, for instance, the return statement is returning a cstring with 15 characters all set to FF. its as though the pointer isn't being incremented. Not sure how to fix this. Any help would be appreciated.

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

char* pascal_convert(void* ptr) {
int len = *((int*)ptr)+1;
char *cString = malloc(sizeof(char) * (len));
(int*)ptr + 1;
for (int i = 0; i < len; i++) {
cString[i] = *((char*)ptr);
(char*)ptr + 1;
cString[len] = '\0';
return cString;



You need to store current address of string somewhere

int currentPtr = (int*)ptr + 1;
for (int i = 0; i < len; i++) {
    cString[i] = *((char*) currentPtr);