Linux kernel scriptes bin2c "\x"

/****************************************************************************
 *                    Linux kernel scriptes bin2c "\x"           
 * 聲明:
 *     早上在閱讀Linux kernel scriptes中的源代碼的時候發現bin2c的源代碼,
 * 因而打算看一下,結果發現"\x"的寫法,因而查了點資料,看了一下它的用法。
 *
 *                                    2015-12-29 深圳 南山平山村 曾劍鋒
 ***************************************************************************/


1、參考資料:
    1. \x49\x51\x5a\x56\x54\ 這種是什麼編碼?
        http://zhidao.baidu.com/link?url=tDpw0M4dsxvnldOAzJg0HiS25vYl3ebTBwJOF0ULHxBGEF-0nYLiewaX29d870N5cro-yv2blYWgG2Kx4xFPXK
    2. \X在C語言裏表示什麼意思?
        http://zhidao.baidu.com/link?url=HC1lXt3Cv2yE1gQaLchbyhlaAwU9X9hVEQNz_dRqqvP4lTO1dTypMFnKT7rI8mmH9lUsSAjNPgo_fyzJYngcLa

2、Linux內核scripts/bin2c.c
    /*
     * Unloved program to convert a binary on stdin to a C include on stdout
     *
     * Jan 1999 Matt Mackall <mpm@selenic.com>
     *
     * This software may be used and distributed according to the terms
     * of the GNU General Public License, incorporated herein by reference.
     */
    
    #include <stdio.h>
    
    /**
     * 1. 程序將第二個參數做爲數組的名字;
     * 2. 程序經過讀取標輸入數據,並將數據轉換成十六進制輸出到標準輸出;
     * 3. 因爲是經過標準入獲取數據,那麼經常使用的方式應該是採用管道進行數據傳輸;
     * 4. 目前沒搞懂下面這條語句爲何要轉成"\x":
     *     printf("\\x%02x",ch);
     */
    int main(int argc, char *argv[])
    {
        int ch, total=0;
    
        if (argc > 1)
            printf("const char %s[] %s=\n",
                argv[1], argc > 2 ? argv[2] : "");
    
        do {
            printf("\t\"");
            while ((ch = getchar()) != EOF)
            {
                total++;
                printf("\\x%02x",ch);
                if (total % 16 == 0)
                    break;
            }
            printf("\"\n");
        } while (ch != EOF);
    
        if (argc > 1)
            printf("\t;\n\nconst int %s_size = %d;\n", argv[1], total);
    
        return 0;
    }

3、"\x"數據測試代碼:
    #include <stdio.h>
    
    int main (int argc, char **argv) {
        printf("%c, %s.\n", '\x30', "\x31\x32");
    }
相關文章
相關標籤/搜索