turbo c 中字节转换字符串问题!!!

ladofwind 2003-12-12 12:38:08
#include ...
int toInt(char param[]);

void main(){

fp = fopen("info.dat","rb");
int ldate;
int ltime;
unsigned char sdate[4]; /*日期*/
unsigned char stime[4]; /*时间*/
fread(sdate,4,1,fp);
fread(stime,4,1,fp);
ldate = toInt(sdate);
ltime = toInt(stime);
printf("long ldate is %d\n ",ldate);
printf("long ltime is %d\n",ltime);
}

int toInt(char param[])
{
int ch1 = param[3] & 0xFF;
int ch2 = param[2] & 0xFF;
int ch3 = param[1] & 0xFF;
int ch4 = param[0] & 0xFF;
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}

程序运行结果:本来ldate应该是20030603 但输出结果确是-1526644982
本来ltime应该是082518 但输出结果却是905969999

请各位帮忙看一下是什么原因,急用!!谢谢!

...全文
78 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
TimesNewRoman 2003-12-12
  • 打赏
  • 举报
回复
我用TC2写了一个:
#include <stdio.h>

unsigned long toInt(char param[]) {
unsigned long ch1,ch2,ch3,ch4;
unsigned long rtn;
ch1 = param[3] & 0xFF;
ch2 = param[2] & 0xFF;
ch3 = param[1] & 0xFF;
ch4 = param[0] & 0xFF;
rtn = (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0);
return (rtn);
}
void main(){
unsigned long ldate;
unsigned long ltime;
unsigned char sdate[4];
unsigned char stime[4];
FILE *fp = fopen("info.dat","rb");
fread(&sdate,4,1,fp);
fread(&stime,4,1,fp);
ldate = toInt(sdate);
ltime = toInt(stime);
printf("long ldate is %ld\n ",ldate);
printf("long ltime is %ld\n",ltime);
}
当然rtn也可能是ch1<<0+ch2<<8+ch3<<16+ch4<<24,这要看你的info.dat中是怎样的格式了。但最重要的是几个变量要用unsigned
TimesNewRoman 2003-12-12
  • 打赏
  • 举报
回复
楼主如果用tc2.0的话要把ldate,ltime定义成unsigned long的,toInt的返回类型也必须是unsigned long。
njuhuangmy 2003-12-12
  • 打赏
  • 举报
回复
主要还是要看你的数据

另外, 你看看,是不是给 int赋值得时候

用 unsigned int , 或者 把 << 24 << 16 << 8 << 0 ,这四个交换一下次序呢??

要注意 文件的 data 采用的是 BIG-ENDIAN 还是 LITTLE-ENDIAN
kama525 2003-12-12
  • 打赏
  • 举报
回复
toint 函数是要用的,把四个字节读出后必须要转换,才能得到有意义的数字
kama525 2003-12-12
  • 打赏
  • 举报
回复
arfi():老兄你有MSN 吗,我的homail 是 kama525@hotmail.com
kama525 2003-12-12
  • 打赏
  • 举报
回复
我把变量ldate,ltime,定义成long 型也是同样的结果
arfi 2003-12-12
  • 打赏
  • 举报
回复
怎么感觉你的toInt没什么用,你是一下下面的程序:

void main()
{
fp = fopen("info.dat","rb");
unsigned long ldate;
unsigned long ltime;

fread(&ldate, 4, 1, fp);
fread(<ime, 4, 1, fp);

printf("long ldate is %ld\n ",ldate);
printf("long ltime is %ld\n",ltime);
}
arfi 2003-12-12
  • 打赏
  • 举报
回复
1179504,你不会是打印的地址把,

用printf("%lx", *(unsigned long *)state);打印一下看看
kama525 2003-12-12
  • 打赏
  • 举报
回复
“想知道在调用toInt时,state[]是什么。”
在调用toint之前,我按字符串打印sdate[]时,什么都没有,按整型数printf是1179504
daily1980 2003-12-12
  • 打赏
  • 举报
回复
int ldate;
int ltime;
用INT类型 -32768~32767
20030603 >> 32767
kama525 2003-12-12
  • 打赏
  • 举报
回复
info.dat 是一个有固定格式的数据文件,它的开头是一组二进制数据4个字节为一数据。
layman2008 2003-12-12
  • 打赏
  • 举报
回复
fread(sdate,4,1,fp);
fread(stime,4,1,fp);

好像有问题!

size_t fread( void *buffer, size_t size, size_t count, FILE *stream );

arfi 2003-12-12
  • 打赏
  • 举报
回复
printf("long ldate is %d\n ",ldate);
printf("long ltime is %d\n",ltime);
把其中的 %d 换成 %lu 试一下。
想知道在调用toInt时,state[]是什么。
illegal 2003-12-12
  • 打赏
  • 举报
回复
楼主好象很擅长汇编呀,^_^,看不大懂,
希望你能给出info.dat的内容
misu 2003-12-12
  • 打赏
  • 举报
回复
注意一下你编译器,是不是TC ,还是Win-tc,如果是的话,那就是int型字长在这两种环境下只有16bit

70,020

社区成员

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

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