I'm kinda new to C++, having programmed VB6 and VB.NET for some time.
Here's my function:

extern "C" __declspec(dllexport) long* CHARtoINT(char* input);

long* CHARtoINT(char* input)
{
int length = strlen(input);
long* output;

for (int i = 0; i < length; i++)
{
output[i] = (long)input[i];
}

return output;
}

The purpose is to pass in a string, and return a long (would prefer
int) array. I need every letter of the string that comes in to be
it's own array member in the outgoing array in ASCII equivs. For
example, if I pass into it "Hello", I need this returned:

[72],[101],[108],[108],[111]

Should be simple, but after 3 weeks, I still can't get it too work
right. I've even tried to pass a areference to the long* as a
parameter, and still no good. Ohh, BTW, this needs to be able to be
called form VB, and C++, and .NET. Nice added touch, right?


Thanks,
Tibby

Re: Function problem by Victor

Victor
Tue Jun 22 08:33:16 CDT 2004

Tibby wrote:
> I'm kinda new to C++, having programmed VB6 and VB.NET for some time.
> Here's my function:
>
> extern "C" __declspec(dllexport) long* CHARtoINT(char* input);
>
> long* CHARtoINT(char* input)
> {
> int length = strlen(input);
> long* output;

You declared a pointer. Where does it point _to_? Nowhere,
you forgot to initialise it.

>
> for (int i = 0; i < length; i++)
> {
> output[i] = (long)input[i];
^^^^^^^^^
Now, here you're dereferencing the pointer whose value is undefined.
The compiler should have told you so (but only in a warning).

> }
>
> return output;
> }
>
> The purpose is to pass in a string, and return a long (would prefer
> int) array. I need every letter of the string that comes in to be
> it's own array member in the outgoing array in ASCII equivs. For
> example, if I pass into it "Hello", I need this returned:
>
> [72],[101],[108],[108],[111]
>
> Should be simple, but after 3 weeks, I still can't get it too work
> right. I've even tried to pass a areference to the long* as a
> parameter, and still no good. Ohh, BTW, this needs to be able to be
> called form VB, and C++, and .NET. Nice added touch, right?

I don't know nothing about no VB, but you can write it so it will be
valid C++. You need to initialise your 'output' with "new long[length]"
Of course, whoever gets that pointer will need to eventually dispose of
it using 'delete[]'.

If you're new to dynamic memory management, open your favorite C++ book.

Victor

Re: Function problem by Tibby

Tibby
Tue Jun 22 09:30:38 CDT 2004

On Tue, 22 Jun 2004 09:33:16 -0400, Victor Bazarov
<v.Abazarov@comAcast.net> wrote:

<SNIP>

Taking your suggestion, here's how I modified the code:

int* CHARtoINT(char* input)//, int* output)
{
int length = strlen(input);
int* output = new int[length];

for (int i = 0; i < length; i++)
{
output[i] = (int)input[i];
}

return output;
}

This works, calling from C++, so now I need to figure out how to get V
B to play nice with it :)

Thanks again Victor,
Tibby

Re: Function problem by Tibby

Tibby
Tue Jun 22 10:30:28 CDT 2004

On Tue, 22 Jun 2004 09:33:16 -0400, Victor Bazarov
<v.Abazarov@comAcast.net> wrote:

>Tibby wrote:
>> I'm kinda new to C++, having programmed VB6 and VB.NET for some time.
>> Here's my function:
>>
>> extern "C" __declspec(dllexport) long* CHARtoINT(char* input);
>>
>> long* CHARtoINT(char* input)
>> {
>> int length = strlen(input);
>> long* output;
>
>You declared a pointer. Where does it point _to_? Nowhere,
>you forgot to initialise it.
>
>>
>> for (int i = 0; i < length; i++)
>> {
>> output[i] = (long)input[i];
> ^^^^^^^^^
>Now, here you're dereferencing the pointer whose value is undefined.
>The compiler should have told you so (but only in a warning).
>
>> }
>>
>> return output;
>> }
>>
>> The purpose is to pass in a string, and return a long (would prefer
>> int) array. I need every letter of the string that comes in to be
>> it's own array member in the outgoing array in ASCII equivs. For
>> example, if I pass into it "Hello", I need this returned:
>>
>> [72],[101],[108],[108],[111]
>>
>> Should be simple, but after 3 weeks, I still can't get it too work
>> right. I've even tried to pass a areference to the long* as a
>> parameter, and still no good. Ohh, BTW, this needs to be able to be
>> called form VB, and C++, and .NET. Nice added touch, right?
>
>I don't know nothing about no VB, but you can write it so it will be
>valid C++. You need to initialise your 'output' with "new long[length]"
>Of course, whoever gets that pointer will need to eventually dispose of
>it using 'delete[]'.
>
>If you're new to dynamic memory management, open your favorite C++ book.
>
>Victor

Looking more into it, I'm running my head into a wall. I need tro
return a string array, because, if the charcode is 72 (H), I need it
to return as 072, but, this will all be internal to C, so I can forget
VB for a while.... Damn....

Tibby

RE: Function problem by mikem

mikem
Tue Jun 22 15:43:01 CDT 2004

I have used this function to do this before.
AsciiToInt(letter);

Here is a link:
http://www.uni-muenster.de/ZIV/Mitarbeiter/BennoSueselbeck/s-html/helpfiles/AsciiToInt.html

It returns an integer but that sounds like what you wanted.


"Tibby" wrote:

> I'm kinda new to C++, having programmed VB6 and VB.NET for some time.
> Here's my function:
>
> extern "C" __declspec(dllexport) long* CHARtoINT(char* input);
>
> long* CHARtoINT(char* input)
> {
> int length = strlen(input);
> long* output;
>
> for (int i = 0; i < length; i++)
> {
> output[i] = (long)input[i];
> }
>
> return output;
> }
>
> The purpose is to pass in a string, and return a long (would prefer
> int) array. I need every letter of the string that comes in to be
> it's own array member in the outgoing array in ASCII equivs. For
> example, if I pass into it "Hello", I need this returned:
>
> [72],[101],[108],[108],[111]
>
> Should be simple, but after 3 weeks, I still can't get it too work
> right. I've even tried to pass a areference to the long* as a
> parameter, and still no good. Ohh, BTW, this needs to be able to be
> called form VB, and C++, and .NET. Nice added touch, right?
>
>
> Thanks,
> Tibby
>
>