【问题标题】:Writing a hex pattern via sscan f通过 sscan f 写入十六进制模式
【发布时间】:2012-04-10 07:21:03
【问题描述】:

我的两个变量都是 32 位无符号整数。不知道为什么这似乎不起作用:

// Arguments: output_file how_many_mb 

int main(int argc, char * argv[])
{
    uint32_t pattern, counter;
    int i, count;
    counter = 1;
    sscanf(&counter, "%x", &pattern); 
    FILE * outFile = fopen(argv[1],"wb");
    int times = atoi(argv[2]);
    count = 0;

    times = times*1048576; // Write out 4bytes at a time

    for (i = 0; i < times; i++) {
        fwrite(&pattern, 1, 1, outFile);
        counter<<=1;
        sscanf(&counter, "%x", &pattern);  
        count++;

        if (counter == 0) { 
            sscanf("00000000", "%x", &pattern);  
                if (count < 100)
                printf ("Reached the condition %0x \n", pattern);   
            counter = 1;    
        }
        if (count < 100)
            printf ("%x\n", pattern);
        }
        fclose (outFile);
    }

我实际上是在尝试将十六进制模式“写入”到文件中。我移位计数器,然后将其以十六进制形式写入模式。那就是给我带来问题的陈述。创建的文件里面基本上有垃圾。

谢谢。

【问题讨论】:

  • 我认为这里没有足够的信息来回答这个问题。出了什么问题?您预期的输入和输出是什么?
  • sscanf() 的第一个参数必须是指向 (const) char 的指针,即:它必须是字符串。
  • 有简单的方法吗?除了必须遍历所有案例和检查?谢谢。
  • 编译时没有任何警告是不可能的。阅读编译器告诉您的内容,并在阅读时阅读sscanf() 的文档。

标签: c++ c embedded hex


【解决方案1】:

您使用 sscanf 不正确(请参阅http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/

int32_t counter;
const char * pattern = "badf00d";
sscanf(pattern, "%x", &counter);

【讨论】:

    猜你喜欢
    • 2017-02-03
    • 2016-04-06
    • 1970-01-01
    • 2014-03-16
    • 2013-07-11
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    相关资源
    最近更新 更多