📄 cvt_asc2c.c
字号:
/***
*Cvt_Asc2C.c - 转换Ascii的文本到由C格式的数组
*
*Purpose:
* 用于处理中文, 韩文时, 当编译器无法识别全角字符的场合.
*
*Copyright:
* Modified by Denny
* 2004-08-07 14:27
* All rights reserved.
*
*Environment:
* Editor - SourceInsight 3.5
* Edit font - Consolas - 12
* Tab width - 4 (Uses spaces in place of Tabs)
* Compiler - Turbo C 2.0
* Run - Microsoft DOS
*
*Exceptions:
* 转换实例:
* .src:
* 1234
* "5678"
* const char c[] = {"asdf"};
* .obj
* 0x31,0x32,0x33,0x34
* 0x35,0x36,0x37,0x38,0x00
* 0x63,0x6F,0x6E,0x73,0x74,0x20,0x63,0x68,0x61,0x72,0x20,0x63,0x5B,0x5D,0x20,0x3D,0x20,0x7B,0x61,0x73,0x64,0x66,0x00,0x7D,0x3B
*
****/
#include "stdio.h"
#include "string.h"
#define TRUE 1
#define FALSE 0
const unsigned long Ver = (~102); /* Encrypt the version */
const char Head_Info1[] = {"\nFile convert function(Version "};
const char Head_Info2[] = {").\x01\n\n"};
const char Head_Info3[] = {"Created by Windforce(R), at 2004-4-22 22:06.\n"};
const char Head_Info4[] = {"Modified on 2004-8-7 14:24.\n"};
const char Head_Info5[] = {"Copyright (c) WeiHua LTD.\n"};
const char Head_Info6[] = {"All rights reserverd.\n\n"};
void main(int argc, char *argv[])
{
char src_buf[80]; /* Source file input buffer */
char obj_buf[80]; /* Object file input buffer */
char *src_file; /* Source file string */
char *obj_file; /* Object file string */
FILE *src_fp; /* Source file pointer */
FILE *obj_fp; /* Object file pointer */
printf(Head_Info1); /* Print the head information */
putchar((~Ver)/100%10 + '0');
putchar('.');
putchar((~Ver)/10%10 + '0');
putchar((~Ver)/1%10 + '0');
printf(Head_Info2);
printf(Head_Info3);
printf(Head_Info4);
printf(Head_Info5);
printf(Head_Info6);
if(argc <= 1) /* Did't input the source file */
{
printf("Source file:");
src_file = src_buf;
scanf("%s", src_file);
}
else /* argc > 1 */
{
if((strcmp(argv[1], "/?") == 0) || (strcmp(argv[1], "/H") == 0) || (strcmp(argv[1], "/h") == 0))
/* Display the usage */
{
printf("Usage:\n");
printf(" .\\Cvt.exe [Source filename] [Destination filename]\n");
return;
}
src_file = argv[1];
}
do{
if((strcmp(src_file, "q") == 0) || (strcmp(src_file, "Q") == 0) || (strcmp(src_file, "exit") == 0)
|| (strcmp(src_file, "EXIT") == 0) || (strcmp(src_file, "Exit") == 0) || (strcmp(src_file, "eXIT") == 0))
{
return;
}
src_fp = fopen(src_file, "rb");
if(src_fp == NULL)
{
printf("Source file not found!");
getch();
putchar('\n');
printf("Source file:");
src_file = src_buf;
scanf("%s", src_file);
}
else
break;
}while(TRUE);
if(argc <= 2) /* Did't input the object file */
{
printf("Object file:");
obj_file = obj_buf;
scanf("%s", obj_file);
}
else
obj_file = argv[2];
if(strcmp(src_file, obj_file) == 0) /* The same file name */
{
fclose(src_fp);
return;
}
do{
if((strcmp(obj_file, "q") == 0) || (strcmp(obj_file, "Q") == 0) || (strcmp(obj_file, "exit") == 0)
|| (strcmp(obj_file, "EXIT") == 0) || (strcmp(obj_file, "Exit") == 0) || (strcmp(obj_file, "eXIT") == 0))
{
fclose(src_fp);
return;
}
system("cd .");
obj_fp = fopen(obj_file, "wb");
if(obj_fp == NULL)
{
printf("Object file can't be created!");
getch();
putchar('\n');
printf("Object file:");
obj_file = obj_buf;
scanf("%s", obj_file);
}
else
break;
}while(TRUE);
{
int src_byte;
char cvt_byte; /* Object is the ASCII code */
char wr_buf[5+1];
unsigned char posi;
unsigned char head_flag = 1;
unsigned char i;
unsigned char spec_char_cnt = 0;
src_byte = fgetc(src_fp);
while(src_byte != EOF) /* First write the source file's information to object file */
{
fputc(src_byte, obj_fp);
src_byte = fgetc(src_fp);
}
fputs("\x0D\x0A=====>>>>>\x0D\x0A", obj_fp);
rewind(src_fp);
printf("Destination file: %s\n", obj_file);
printf("Converts result:\n");
for(i = 0; i < 60; i ++)
putchar(0xC4);
putchar('\n');
src_byte = fgetc(src_fp);
while(src_byte != EOF)
{
if(src_byte < 0x20) /* Write it a blank */
{
head_flag = 1;
spec_char_cnt = 0;
if((src_byte == 0x0D) || (src_byte == 0x0A))
{
fputc(src_byte, obj_fp);
putchar(src_byte);
src_byte = fgetc(src_fp);
continue;
}
else
{
strcpy(wr_buf, " ");
}
}
else
{
posi = 0;
if(src_byte == '\"')
{
spec_char_cnt ++;
if(spec_char_cnt == 2)
{
spec_char_cnt = 0;
strcpy(wr_buf, ",0x00");
posi = 5;
}
else
{
src_byte = fgetc(src_fp);
continue;
}
}
else
{
if(head_flag) /* Is the head */
head_flag = 0;
else
wr_buf[posi ++] = ',';
wr_buf[posi ++] = '0';
wr_buf[posi ++] = 'x';
cvt_byte = (src_byte>>4) + '0';
if(cvt_byte > '9')
cvt_byte += 7; /* Character 'A' ~ 'F' */
wr_buf[posi ++] = cvt_byte;
cvt_byte = (src_byte&0x0F) + '0';
if(cvt_byte > '9')
cvt_byte += 7; /* Character 'A' ~ 'F' */
wr_buf[posi ++] = cvt_byte;
wr_buf[posi ++] = 0;
}
}
fputs(wr_buf, obj_fp);
printf("%s", wr_buf);
src_byte = fgetc(src_fp);
}
putchar('\n');
for(i = 0; i < 60; i ++)
putchar(0xC4);
putchar('\n');
}
printf("File convert finish!\n");
fclose(obj_fp);
fclose(src_fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -