- 分享
- 0
- 人气
- 0
- 主题
- 23
- 帖子
- 124
- UID
- 186218
- 积分
- 122
- 阅读权限
- 13
- 注册时间
- 2008-12-16
- 最后登录
- 2012-3-17
- 在线时间
- 358 小时
|
本帖最后由 狂天使 于 2011-4-10 12:34 AM 编辑
各位高手,我想問下該如和將string轉換成hex?- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 100
- int main()
- {
- FILE *fp1,*fp2;
- int row=0,count=0,start_address=0;
- int locctr[MAX];
- char label[MAX],mnemonic[MAX],operand[MAX],op_mnemonic[MAX],op_format[MAX],opcode[MAX];
- char new_label[MAX][MAX],new_mnemonic[MAX][MAX],new_operand[MAX][MAX];
- char new_op_menmonic[MAX][MAX],new_op_format[MAX][MAX],new_opcode[MAX][MAX];
-
- fp1=fopen("source.txt","r"); //open the file
-
- if(fp1==NULL) //If fp1 get NULL value mean cannot open the file
- {
- perror("Error Message"); //print out Error Message
- }
- else //if fp1 open the file successfuly
- {
- while(!feof(fp1)) //If fp1 not End of File then store the string into the array
- {
- fscanf(fp1,"%s%s%s",label,mnemonic,operand); //store the string into array
- //copy the array to new array to indicate what row it is
- strcpy(new_label[row],label);
- strcpy(new_mnemonic[row],mnemonic);
- strcpy(new_operand[row],operand);
- row++; //Count row of the program
-
-
- }
- }
-
-
- if(strcmp(new_mnemonic[count],"START")==0)
- {
- start_address=atoi(new_operand[count]); //convert string to integer
- printf("%X",start_address);
- }
- else
- printf("123");
-
- row=0;
-
- fp2=fopen("OPTAB.txt","r"); //open the file OPTAB
-
- if(fp2==NULL) //If fp2 get NULL value mean cannot open the file
- {
- perror("Error Message"); //print out Error Message
- }
- else //if fp1 success open the file
- {
- while(!feof(fp2)) //If fp1 not End of File then store the string into the array
- {
- fscanf(fp2,"%s%s%s",op_mnemonic,op_format,opcode);
- strcpy(new_op_menmonic[row],op_mnemonic);
- strcpy(new_op_format[row],op_format);
- strcpy(new_opcode[row],opcode);
- // printf("%5s %10s %10s\n",new_op_menmonic[row],new_op_format[row],new_opcode[row]);
- row++; //Count row of the program
- }
- }
-
-
-
- system("pause");
- return 0;
-
- }
复制代码 主要是這裡。
if(strcmp(new_mnemonic[count],"START")==0)
{
start_address=atoi(new_operand[count]); //convert string to integer
printf("%X",start_address);
}
以下是我的file
source.txt
COPY START 1000
FIRST STL RETADR
CLOOP JSUB RDREC
- LDA LENGTH
- COMP ZERO
- JEQ ENDFIL
- JSUB WRREC
- J CLOOP
ENDFIL LDA EOF
- STA BUFFER
- LDA THREE
- STA LENGTH
- JSUB WRREC
- LDL RETADR
- RSUB -
EOF BYTE C’EOF’
THREE WORD 3
ZERO WORD 0
RETADR RESW 1
LENGTH RESW 1
BUFFER RESB 4096
RDREC LDX ZERO
- LDA ZERO
RLOOP TD INPUT
- JEQ RLOOP
- RD INPUT
- COMP ZERO
- JEQ EXIT
- STCH BUFFER,X
- TIX MAXLEN
- JLT RLOOP
EXIT STX LENGTH
- RSUB -
INPUT BYTE X’F1’
MAXLEN WORD 4096
WRREC LDX ZERO
WLOOP TD OUTPUT
- JEQ WLOOP
- LDCH BUFFER,X
- WD OUTPUT
- TIX LENGTH
- JLT WLOOP
- RSUB -
OUTPUT BYTE X’05’
- END FIRST
先謝謝大家 |
|