Solved

Translating a variable from C to Delphi

Posted on 2002-03-12
4
158 Views
Last Modified: 2010-04-05
I am currently, not very successfully, attempting to convert some C files I was given to use in a Delphi application I am writing. I have never used C before but have found it easy to read and translate.
One area I am finding difficult to get to grips with is being able to distinguish between a variable which looks like a PChar but sometimes behaves like a dynamic array.

For Example the variable B:

C Code
------
EXTERN unsigned char  *A;
EXTERN unsigned char *B;
EXTERN unsigned smallint i,j;  

A := B
B[i + j] := 128

If A and B was a PChars in Delphi then A := B would be acceptable but the following line would require an array type which B is not.
If A was a PChar but B was an Array of Char then the first line would generate an error whereas the second line would not.

Could someone please tell me how this variable, B, should be defined in Delphi.

Thank you
0
Comment
Question by:Rabster
  • 2
4 Comments
 
LVL 45

Accepted Solution

by:
aikimark earned 50 total points
ID: 6856687
From http://www.cpcug.org/user/clemenzi/technical/Languages/DataTypes.htm

it looks like your best bets for A,B definition are:
B: string;
B: ShortString;
B: AnsiString;  
0
 

Author Comment

by:Rabster
ID: 6856788
Aikimark,
Thanks for your comments. The data types sheet has enabled me to understand the differences in the language types.
Cheers
Rabster
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6856796
Only Shortstring could work. string and AnsiString are definitely incompatible.

Use PChar for A and B. Signed vs unsigned is not that important.
You can always make your own type usable for [] in Delphi.

type
 PByte = ^ByteArray;
 ByteArray = array [0..MaxInt] of Byte;

A Pascal programmer calling C easily readable? You are definitely wierd ;-)
0
 

Author Comment

by:Rabster
ID: 6857256
I have tried creating a Byte Array previously. This is OK for the majority of my program until I hit the following as an example:

C Code
------
unsigned char *CFrom, *CTo;
EXTERN unsigned char *CAvg;

*To++ := Avg[(smallint)(228*(From[0]+From[1])+256)>>9];

My Delphi Code
--------------
var
  CFrom, CTo, CAvg : ByteArray;

CTo := CAvg[(228*(CFrom[0] + CFrom[1])+256) SHR 9];

PS
Have no idea what *To++:=, thought it may mean
To := To + in Delphi

PPS
You may be right about the weird thing.



0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question