zoukankan      html  css  js  c++  java
  • Linux kernel scriptes bin2c "x"

    /****************************************************************************
     *                    Linux kernel scriptes bin2c "x"           
     * 声明:
     *     早上在阅读Linux kernel scriptes中的源代码的时候发现bin2c的源代码,
     * 于是打算看一下,结果发现"x"的写法,于是查了点资料,看了一下它的用法。
     *
     *                                    2015-12-29 深圳 南山平山村 曾剑锋
     ***************************************************************************/
    
    
    一、参考资料:
        1. x49x51x5ax56x54 这种是什么编码?
            http://zhidao.baidu.com/link?url=tDpw0M4dsxvnldOAzJg0HiS25vYl3ebTBwJOF0ULHxBGEF-0nYLiewaX29d870N5cro-yv2blYWgG2Kx4xFPXK
        2. X在C语言里表示什么意思?
            http://zhidao.baidu.com/link?url=HC1lXt3Cv2yE1gQaLchbyhlaAwU9X9hVEQNz_dRqqvP4lTO1dTypMFnKT7rI8mmH9lUsSAjNPgo_fyzJYngcLa
    
    二、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=
    ",
                    argv[1], argc > 2 ? argv[2] : "");
        
            do {
                printf("	"");
                while ((ch = getchar()) != EOF)
                {
                    total++;
                    printf("\x%02x",ch);
                    if (total % 16 == 0)
                        break;
                }
                printf(""
    ");
            } while (ch != EOF);
        
            if (argc > 1)
                printf("	;
    
    const int %s_size = %d;
    ", argv[1], total);
        
            return 0;
        }
    
    三、"x"数据测试代码:
        #include <stdio.h>
        
        int main (int argc, char **argv) {
            printf("%c, %s.
    ", 'x30', "x31x32");
        }
  • 相关阅读:
    架构基础-容量评估
    golang版本实现版本号比较-从易到解决bug
    数组模拟栈
    稀疏数组
    密码生成器
    01-gopsutil包使用
    02从零开始学习GO语言--标识符、关键字、变量和常量
    Go语言简介
    从零开始学习GO语言-搭建Go语言开发环境-快速开发入门第一个小程序
    ES6学习总结之 Module
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/5085064.html
Copyright © 2011-2022 走看看