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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pchar Initialization?

Status
Not open for further replies.

beltijs

Programmer
Sep 11, 2003
25
AR
Hi!
I write a function that looks like this:

function codigo_tipofact(nro_ingresado: string): string;
var
pcnro_ingresado : pchar;
pre_guion : string;
pcpre_guion : pchar;
post_guion: string;
posicion : Integer;
begin
pcnro_ingresado := pchar(nro_ingresado);
posicion := ansipos('-', pcnro_ingresado);
StrMove(pcpre_guion,pcnro_ingresado,posicion-1);
pre_guion := RightStr('0000' + trim(string(pcpre_guion)),4);

posicion := ansipos('-', pcnro_ingresado);
post_guion:= rightstr(pcnro_ingresado,(length(nro_ingresado) - posicion));
post_guion := RightStr('00000000' + post_guion,8);

result := pre_guion + '-' + post_guion;
end;

The input is some string with a '-' in some position. i have to return de same number but with 4 digit before the '-' and 8 digits after the '-'. ej:
input = '0-1', output = '0000-00000001'
This function works great until today when i try to call it from another function, then i saw that when i input
'0-1' from the calling funtion the ouput is '00%¾-00000001'.
When i watch the variables during a debug i saw that the pcpre_guion variable has this value 0%¾, before i put anything into it. So i don't know how to initialize that variable.
i don't know, may be the whole function is wrong...
help?

 
Yes, the whole function is wrong.

A pchar is a pointer. You don't need to use pchars for this function. Something like this should work:
Code:
function codigo_tipofact(nro_ingresado: string): string;
var
 posicion : Integer;
begin
 posicion := Pos('-', nro_ingresado);
 Assert ( posicion <> -1, 'Bad Input' );
 result := RightStr('0000'+Copy(nro_ingresado,1,posicion-1),4) +
           '-' +
           RightStr('00000000'+Copy(nro_ingresado,posicion+1,8),8);
end;

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top