Solved

Translating a variable from C to Delphi

Posted on 2002-03-12
4
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

696 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