69,369
社区成员
发帖
与我相关
我的任务
分享
在C语言里面Char是八位的,但是在C#里面char是16位的,应该是这里出问题了
如果是这里出问题的话,
就全部都是去16位 来试试!c语言里char也当成16位读
FRESULT f_read (
FIL *fp, /* Pointer to the file object */
void *buff, /* Pointer to data buffer */
UINT btr, /* Number of bytes to read */
UINT *br /* Pointer to number of bytes read */
)
{
FRESULT res;
DWORD clst, sect, remain;
UINT rcnt, cc;
BYTE csect, *rbuff = buff;
*br = 0; /* Initialize byte counter */
res = validate(fp->fs, fp->id); /* Check validity */
if (res != FR_OK) LEAVE_FF(fp->fs, res);
if (fp->flag & FA__ERROR) /* Aborted file? */
LEAVE_FF(fp->fs, FR_INT_ERR);
if (!(fp->flag & FA_READ)) /* Check access mode */
LEAVE_FF(fp->fs, FR_DENIED);
remain = fp->fsize - fp->fptr;
if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
for ( ; btr; /* Repeat until all data read */
rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
if (!csect) { /* On the cluster boundary? */
if (fp->fptr == 0) { /* On the top of the file? */
clst = fp->sclust; /* Follow from the origin */
} else { /* Middle or end of the file */
#if _USE_FASTSEEK
if (fp->cltbl)
clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */
else
#endif
clst = get_fat(fp->fs, fp->clust); /* Follow cluster chain on the FAT */
}
if (clst < 2) ABORT(fp->fs, FR_INT_ERR);
if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
fp->clust = clst; /* Update current cluster */
}
sect = clust2sect(fp->fs, fp->clust); /* Get current sector */
if (!sect) ABORT(fp->fs, FR_INT_ERR);
sect += csect;
cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */
if (cc) { /* Read maximum contiguous sectors directly */
if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */
cc = fp->fs->csize - csect;
if (disk_read(fp->fs->drv, rbuff, sect, (BYTE)cc) != RES_OK)
ABORT(fp->fs, FR_DISK_ERR);
#if !_FS_READONLY && _FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */
#if _FS_TINY
if (fp->fs->wflag && fp->fs->winsect - sect < cc)
mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs));
#else
if ((fp->flag & FA__DIRTY) && fp->dsect - sect < cc)
mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs));
#endif
#endif
rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
continue;
}
#if !_FS_TINY
if (fp->dsect != sect) { /* Load data sector if not in cache */
#if !_FS_READONLY
if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
ABORT(fp->fs, FR_DISK_ERR);
fp->flag &= ~FA__DIRTY;
}
#endif
if (disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK) /* Fill sector cache */
ABORT(fp->fs, FR_DISK_ERR);
}
#endif
fp->dsect = sect;
}
rcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Get partial sector data from sector buffer */
if (rcnt > btr) rcnt = btr;
#if _FS_TINY
if (move_window(fp->fs, fp->dsect)) /* Move sector window */
ABORT(fp->fs, FR_DISK_ERR);
mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
#else
mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
#endif
}
LEAVE_FF(fp->fs, FR_OK);
}
[/quote]
你用c语言代码去加密,再解密看看,是不是乱码?
FRESULT f_read (
FIL *fp, /* Pointer to the file object */
void *buff, /* Pointer to data buffer */
UINT btr, /* Number of bytes to read */
UINT *br /* Pointer to number of bytes read */
)
{
FRESULT res;
DWORD clst, sect, remain;
UINT rcnt, cc;
BYTE csect, *rbuff = buff;
*br = 0; /* Initialize byte counter */
res = validate(fp->fs, fp->id); /* Check validity */
if (res != FR_OK) LEAVE_FF(fp->fs, res);
if (fp->flag & FA__ERROR) /* Aborted file? */
LEAVE_FF(fp->fs, FR_INT_ERR);
if (!(fp->flag & FA_READ)) /* Check access mode */
LEAVE_FF(fp->fs, FR_DENIED);
remain = fp->fsize - fp->fptr;
if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
for ( ; btr; /* Repeat until all data read */
rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
if (!csect) { /* On the cluster boundary? */
if (fp->fptr == 0) { /* On the top of the file? */
clst = fp->sclust; /* Follow from the origin */
} else { /* Middle or end of the file */
#if _USE_FASTSEEK
if (fp->cltbl)
clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */
else
#endif
clst = get_fat(fp->fs, fp->clust); /* Follow cluster chain on the FAT */
}
if (clst < 2) ABORT(fp->fs, FR_INT_ERR);
if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
fp->clust = clst; /* Update current cluster */
}
sect = clust2sect(fp->fs, fp->clust); /* Get current sector */
if (!sect) ABORT(fp->fs, FR_INT_ERR);
sect += csect;
cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */
if (cc) { /* Read maximum contiguous sectors directly */
if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */
cc = fp->fs->csize - csect;
if (disk_read(fp->fs->drv, rbuff, sect, (BYTE)cc) != RES_OK)
ABORT(fp->fs, FR_DISK_ERR);
#if !_FS_READONLY && _FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */
#if _FS_TINY
if (fp->fs->wflag && fp->fs->winsect - sect < cc)
mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs));
#else
if ((fp->flag & FA__DIRTY) && fp->dsect - sect < cc)
mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs));
#endif
#endif
rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
continue;
}
#if !_FS_TINY
if (fp->dsect != sect) { /* Load data sector if not in cache */
#if !_FS_READONLY
if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
ABORT(fp->fs, FR_DISK_ERR);
fp->flag &= ~FA__DIRTY;
}
#endif
if (disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK) /* Fill sector cache */
ABORT(fp->fs, FR_DISK_ERR);
}
#endif
fp->dsect = sect;
}
rcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Get partial sector data from sector buffer */
if (rcnt > btr) rcnt = btr;
#if _FS_TINY
if (move_window(fp->fs, fp->dsect)) /* Move sector window */
ABORT(fp->fs, FR_DISK_ERR);
mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
#else
mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
#endif
}
LEAVE_FF(fp->fs, FR_OK);
}
/*
读取文件里面的一段数据
drv: 0 SD 卡
1 NAND FLASH
path: 文件路径
Output 输出缓冲区
Offset 文件偏移量
Lenght 读取长度
*/
u16 f_ReadFile(BYTE drv,const char *path,char *Output,u32 Offset,u16 Lenght)
{
FIL file;
FRESULT res;
UINT nReadLen;
nReadLen = 0 ;
//Offset = 72 ;
//Lenght = 72 ;
res = f_mount(drv, &g_SDfs);
res = f_open(&file, path, FA_OPEN_EXISTING | FA_READ);
if(res!=FR_OK)
{
//Uart_DebugString((u8*)"\r\n 打开文件失败,请查看SD卡中是否存在文件!") ;
//Uart_DebugString((u8*)"\r\n 等待中.....") ;
return 0 ;
//while(1);
}
// Uart_DebugString((u8*)"\r\n 打开文件成功!") ;
// memset(data,0,20);
//while(1)
{
res = f_lseek(&file, Offset); //指针移到文件最后
f_read(&file,Output,Lenght,&nReadLen);
/* if(fgets(Output, Lenght+1, &file)==NULL)
{
//break;
nReadLen = 0 ;
}
else
{
// sprintf(g_CommonBuf,"Data=%02x %02x %02x %02x %02x %02x %02x %02x\r\n",data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]);
// Uart_DebugString(g_CommonBuf);
// sprintf(g_CommonBuf,"Data=%02x %02x %02x %02x %02x %02x %02x %02x\r\n",data[8],data[9],data[10],data[11],data[12],data[13],data[14],data[15]);
// Uart_DebugString(g_CommonBuf);
nReadLen = Lenght ;
}*/
}
f_close(&file);
res = f_mount(drv, NULL);
return nReadLen ;
}
void HexDump(char *buf,int len) {
int i,j,k;
char binstr[80];
for (i=0;i<len;i++) {
if (0==(i%16)) {
sprintf(binstr,"%04x -",i);
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
} else if (15==(i%16)) {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
sprintf(binstr,"%s ",binstr);
for (j=i-15;j<=i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
} else {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
}
}
if (0!=(i%16)) {
k=16-(i%16);
for (j=0;j<k;j++) {
sprintf(binstr,"%s ",binstr);
}
sprintf(binstr,"%s ",binstr);
k=16-k;
for (j=i-k;j<i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
}
}