通过sscan f写一个十六进制模式

时间:2012-03-26 18:59:36

标签: c++ c embedded hex

我的两个变量都是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);
    }

我实际上是在尝试将十六进制模式“写入”文件。我移位计数器,然后将其写为十六进制模式。这是导致我出现问题的陈述。创建的文件里面基本上都有垃圾。

感谢。

1 个答案:

答案 0 :(得分:4)

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

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