"Nick Iorio" <
XXXX@XXXXX.COM>wrote
Quote
I have a string called mychar which is length 15 and
when I look at it, its value is: '6-56'#0#0#0#0#0#0#0#0'V5\'
This needs to be converted to 30 decimal digits which will
then be: 544553540000000000000000865392
So, the question is how do I convert the mychar string
to these 30 digits?
Nick, I think that you still have your numbers mixed up,
but here below is a "quick and dirty". Regards, Johnh
Use it with 2 TEdit boxes and 2 TButtons on a form.
const BCDChars: array [0 .. 15] of char = '0123456789ABCDEF';
function BCDDigitsToCharDigits(BCDDigits: string): string;
var i,n: integer; b: byte;
begin
n := 2*Length(BCDDigits);
SetLength(Result,n);
For i := 0 to Length(BCDDigits) -1 do begin
b := ord(BCDDigits[i + 1]);
Result[2*i + 1] := BCDChars[(b div 16)];
Result[2*i + 2] := BCDChars[(b mod 16)];
end;
end;
function CharDigitsToBCDDigits(CharDigits: string): string;
var i,j,k,n: integer; b: byte; c: char;
begin
n := (Length(CharDigits) + 1) div 2;
SetLength(Result,n);
For i := 1 to length(CharDigits) do begin
c := CharDigits[i];
k := high(BCDChars);
While (k>= 0) and (c <>BCDChars[k]) do dec(k);
If (k < 0)
then if c in [#32 .. #126]
then raise Exception.CreateFmt('Illegal char ("%s") in in
input',[c])
else raise Exception.CreateFmt('Illegal char (0x%2.2x) in in
input',[ord(c)]);
If Odd(i)
then b := k*16
else b := b + k;
j := (i - 1) div 2 + 1;
Result [j] := char(b);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var s, t: string;
begin
s := Edit1.Text;
t := BCDDigitsToCharDigits(s);
Edit2.Text := t;
end;
procedure TForm1.Button2Click(Sender: TObject);
var s, t: string;
begin
s := Edit2.Text;
t := CharDigitsToBCDDigits(s);
Edit1.Text := t;
end;