急!! Delphi 代码转换为 C 代码.... ( 我写的有错?? )

cmain83 2005-06-21 04:05:18
C 代码编译成 DLL 文件后, 执行结果与 Delphi 的 DLL 结果不一样.
代码如下:

///////////////////////////////////////////////
//
// Delphi 代码
//
///////////////////////////////////////////////
const
constACMAdptive: array[0..15] of SmallInt = (230, 230, 230, 230, 307, 409,
512, 614, 768, 614, 512, 409, 307, 230, 230, 230);

constACMCoef: array[0..1, 0..6] of SmallInt = (
(256, 512, 0, 192, 240, 460, 392),
(0, -256, 0, 64, 0, -208, -232));

procedure CreateACMData(Source: array of Byte; var Dest: array of SmallInt);
var
i, j, Prec: Integer;
Sample, Delta: LongInt;
Samp1, Samp2: SmallInt;
Code: Byte;
begin
Prec := Source[0];
Delta := (Source[2] shl 8) or Source[1];
Samp1 := (Source[4] shl 8) or Source[3];
Samp2 := (Source[6] shl 8) or Source[5];
Dest[0] := Samp2;
Dest[1] := Samp1;

for i := 0 to 248 do
begin
for j := 0 to 1 do
begin
if j = 0 then
begin
Code :=(Source[i + 7] and $0F0);
Code := Code shr 4;
end
else begin
Code := Source[i + 7] and $0F;
end;

Sample := (Samp1 * constACMCoef[0, Prec] + Samp2 *
constACMCoef[1, Prec]) div 256;
if (Code > 7) then
begin
Sample := Sample + Delta * (Code - $10);
end
else begin
Sample := Sample + Delta * Code;
end;

if Sample > 32767 then
begin
Sample := 32767;
end;

if Sample < -32768 then
begin
Sample := -32768;
end;

Dest[i * 2 + j + 2] := Sample;
Delta := (Delta * constACMAdptive[Code]) div 256;

if Delta < 16 then
begin
Delta := 16;
end;

Samp2 := Samp1;
Samp1 := Sample;
end;
end;
end;


///////////////////////////////////////////////
//
// C 代码
//
///////////////////////////////////////////////
short constACMAdptive[16] = {230, 230, 230, 230, 307, 409,
512, 614, 768, 614, 512, 409, 307, 230, 230, 230};

short constACMCoef[2][7] = {
{256, 512, 0, 192, 240, 460, 392},
{0, -256, 0, 64, 0, -208, -232}};

void WINAPI CreateACMData(byte *Source, short *Dest)
{
int i, j, Prec;
LONG Sample, Delta;
short Samp1, Samp2;
byte Code;

Prec = Source[0];
Delta = (Source[2] << 8) | Source[1];
Samp1 = (Source[4] << 8) | Source[3];
Samp2 = (Source[6] << 8) | Source[5];
Dest[0] = Samp2;
Dest[1] = Samp1;

for (i = 0; i <= 248; i++)
{
for (j = 0; j <= 1; j++)
{
Code = (j==0) ? ((Source[i + 7] & 0xF0) >> 4) : (Source[i + 7] & 0x0F);
Sample = (Samp1 * constACMCoef[0][Prec] + Samp2 * constACMCoef[1][Prec]) / 256;
Sample = (Code > 7) ? (Sample + Delta * (Code - 0x10)) : (Sample + Delta * Code);
Sample = (Sample > 32767) ? 32767 : Sample;
Sample = (Sample < -32768) ? -32768 : Sample;
Dest[i * 2 + j + 2] = (byte)Sample;
Delta = (Delta * constACMAdptive[Code]) / 256;
Delta = (Delta < 16) ? 16 : Delta;
Samp2 = Samp1;
Samp1 = (short)Sample;
}
}
}
...全文
203 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cmain83 2005-07-02
  • 打赏
  • 举报
回复
自己解决了...

void WINAPI CreateACMData(byte *Source, short *Dest)
{
int i, j, Prec;
LONG Sample, Delta;
short Samp1, Samp2;
byte Code;

Prec = Source[0];
Delta = (Source[2] << 8) | Source[1];
Samp1 = (Source[4] << 8) | Source[3];
Samp2 = (Source[6] << 8) | Source[5];
Dest[0] = Samp2;
Dest[1] = Samp1;

for (i = 0; i <= 248; i++)
{
for (j = 0; j <= 1; j++)
{
Code = (j==0) ? ((Source[i + 7] & 0xF0) >> 4) : (Source[i + 7] & 0x0F);
Sample = (Samp1 * constACMCoef[0][Prec] + Samp2 * constACMCoef[1][Prec]) / 256;
Sample = (Code > 7) ? (Sample + Delta * (Code - 0x10)) : (Sample + Delta * Code);
Sample = (Sample > 32767) ? 32767 : Sample;
Sample = (Sample < -32768) ? -32768 : Sample;
Dest[i * 2 + j + 2] = (short)Sample;
Delta = (Delta * constACMAdptive[Code]) / 256;
Delta = (Delta < 16) ? 16 : Delta;
Samp2 = Samp1;
Samp1 = (short)Sample;
}
}
}

出错在:
Dest[i * 2 + j + 2] = (byte)Sample;

正确:
Dest[i * 2 + j + 2] = (short)Sample;

这时粗心所造成.
呵呵.希望各位写代码不要粗心... 不然, 你会很痛苦D...
:)
foochow 2005-06-23
  • 打赏
  • 举报
回复
学习学习
风中老长 2005-06-23
  • 打赏
  • 举报
回复
我这没有delphi环境,有的话可以帮你跟踪一下,建议你在每个计算结果后面加一个输出,看看到底差在什么地方,然后再纠正错误的地方。
cmain83 2005-06-23
  • 打赏
  • 举报
回复
还是不行...

:(
cmain83 2005-06-23
  • 打赏
  • 举报
回复
up
cmain83 2005-06-22
  • 打赏
  • 举报
回复
up
mostideal 2005-06-22
  • 打赏
  • 举报
回复
楼上说的应该是一样的。。。
beyondtkl 2005-06-22
  • 打赏
  • 举报
回复
此函数的调用如下.( 已经固定的模式的 )

byte Source[256];
short Dest[500];

从一个文件中读取256个值放入Source中

CreateACMData(&Source, &Dest);

------------>
不对吧。。。Source就是首地址了。。。Source == &Source[0]; 看看 是不是这个错误?
cmain83 2005-06-22
  • 打赏
  • 举报
回复
没人关注吗?

我可以加分的。
rick29 2005-06-21
  • 打赏
  • 举报
回复
int a = 10, b = 20, c = 0;
c = a / b; // c = 0;
c = b / a; // c = 2;

如果是整型运算,则/得到的结果就是舍去小数部分的整数。
cmain83 2005-06-21
  • 打赏
  • 举报
回复
C好像没有整除运算符吧.
都三年多没用C了...
并且现在手头也没资料... 晕.
rick29 2005-06-21
  • 打赏
  • 举报
回复
楼主可以去找找DELPHI转BCB工程的工具。
风中老长 2005-06-21
  • 打赏
  • 举报
回复
delphi div 是整除,c的/不是整除,结果当然不一样啦!

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧