Go Premium for a chance to win a PS4. Enter to Win


Translating a variable from C to Delphi

Posted on 2002-03-12
Medium Priority
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 46

Accepted Solution

aikimark earned 150 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;  

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

926 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