Translating a variable from C to Delphi

Posted on 2002-03-12
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
Question by:Rabster
  • 2
LVL 45

Accepted Solution

aikimark earned 50 total points
ID: 6856687

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

Author Comment

ID: 6856788
Thanks for your comments. The data types sheet has enabled me to understand the differences in the language types.
LVL 11

Expert Comment

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.

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

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

Author Comment

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
  CFrom, CTo, CAvg : ByteArray;

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

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

You may be right about the weird thing.


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