Facebook Sharer
选择您要替换的背景颜色:
【农历新年】背景图片:
个性化设定
 注册  找回密码
查看: 1180|回复: 0
打印 上一主题 下一主题

如何將string轉換成hex

[复制链接]

23

主题

0

好友

122

积分

高级会员

Rank: 3Rank: 3Rank: 3

跳转到指定楼层
1#
发表于 2011-4-10 12:30 AM |只看该作者 |倒序浏览
本帖最后由 狂天使 于 2011-4-10 12:34 AM 编辑

各位高手,我想問下該如和將string轉換成hex?
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define MAX 100

  5. int main()
  6. {
  7.     FILE *fp1,*fp2;
  8.     int row=0,count=0,start_address=0;
  9.     int locctr[MAX];
  10.     char label[MAX],mnemonic[MAX],operand[MAX],op_mnemonic[MAX],op_format[MAX],opcode[MAX];
  11.     char new_label[MAX][MAX],new_mnemonic[MAX][MAX],new_operand[MAX][MAX];
  12.     char new_op_menmonic[MAX][MAX],new_op_format[MAX][MAX],new_opcode[MAX][MAX];
  13.    
  14.     fp1=fopen("source.txt","r"); //open the file
  15.    
  16.     if(fp1==NULL) //If fp1 get NULL value mean cannot open the file
  17.     {
  18.         perror("Error Message"); //print out Error Message
  19.     }
  20.     else //if fp1 open the file successfuly
  21.     {
  22.         while(!feof(fp1)) //If fp1 not End of File then store the string into the array
  23.         {
  24.            fscanf(fp1,"%s%s%s",label,mnemonic,operand);  //store the string into array
  25.            //copy the array to new array to indicate what row it is
  26.            strcpy(new_label[row],label);  
  27.            strcpy(new_mnemonic[row],mnemonic);
  28.            strcpy(new_operand[row],operand);
  29.            row++;   //Count row of the program
  30.            
  31.            
  32.         }      
  33.     }
  34.    
  35.    

  36.     if(strcmp(new_mnemonic[count],"START")==0)
  37.     {

  38.        start_address=atoi(new_operand[count]);  //convert string to integer
  39.        printf("%X",start_address);
  40.     }
  41.     else
  42.        printf("123");
  43.    
  44.     row=0;
  45.    
  46.     fp2=fopen("OPTAB.txt","r"); //open the file OPTAB
  47.      
  48.     if(fp2==NULL)   //If fp2 get NULL value mean cannot open the file
  49.     {
  50.        perror("Error Message"); //print out Error Message
  51.     }
  52.     else //if fp1 success open the file
  53.     {
  54.         while(!feof(fp2)) //If fp1 not End of File then store the string into the array
  55.         {
  56.            fscanf(fp2,"%s%s%s",op_mnemonic,op_format,opcode);
  57.            strcpy(new_op_menmonic[row],op_mnemonic);  
  58.            strcpy(new_op_format[row],op_format);
  59.            strcpy(new_opcode[row],opcode);
  60.           // printf("%5s %10s %10s\n",new_op_menmonic[row],new_op_format[row],new_opcode[row]);
  61.            row++;  //Count row of the program
  62.         }
  63.     }
  64.    
  65.    
  66.    
  67.     system("pause");
  68.     return 0;
  69.    
  70. }

复制代码
主要是這裡。

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

先謝謝大家




收藏收藏0
您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-7-8 10:40 AM , Processed in 0.111383 second(s), 26 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Ultra High-performance Dedicated Server powered by iCore Technology Sdn. Bhd.
Domain Registration | Web Hosting | Email Hosting | Forum Hosting | ECShop Hosting | Dedicated Server | Colocation Services
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部