使用for循环的int到char数组?

时间:2017-04-25 16:04:03

标签: c

我有一个

char data[2]

也是一个整数

int b = 65535;

65535是十六进制0xffff,可以按照以下方式进行推送

data[0] = 0xff

data[1] = 0xff

我有一个for循环如下:

for(; b > 0; b--) {

}

for循环无法更改!我想使用一个计数器,它从0增加到0xffff,数据查看每次迭代,如下所示:

0x0000  <----- first iteration 
0x0001  <----- second iteration 
0x0002  <----- third iteration 
0x0003  <----- fourth iteration 
...... 
0x00ff  <----- 256th iteration 
0x0100  <----- ....
0x0101 
...... 
0xffff

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

只需使用你的b计数器,然后用0xFFFF xor。另请注意关于对数组使用unsigned char over char的注释。

较小的例子:

unsigned short counter = b ^ 0xFFFF;
data[0] = counter & 0xFF;
data[1] = (counter>>8) & 0xFF;

使用

存储
int TimeTracking(void) { // Function with the purpose of counting how many 
    // times it has been invoked and return it
    static unsigned int call_count = -1;
    call_count++;
    return call_count;
}

int PrinterFunction (void) {
    int LocalCallCount = TimeTracking();
    char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int randomvar = 0;
    fileObject = fopen("Default.txt", "a");

    char MasterArray[1440][10];


    char AircraftRandomLetter1[10]; // Store random letter
    char AircraftRandomLetter2[10]; // Store second random letter


    randomvar = rand() % 26; // Gen random num for gen random letter
    AircraftRandomLetter1[LocalCallCount] = alphabet[randomvar]; // Access 
    // Violation here???
    randomvar = rand() % 26; // Gen random num for gen random letter
    AircraftRandomLetter2[LocalCallCount] = alphabet[randomvar];


    strcat(AircraftRandomLetter1[LocalCallCount], 
    AircraftRandomLetter2[LocalCallCount]);


    MasterArray[LocalCallCount] = AircraftRandomLetter1[LocalCallCount];  


    fputs(AircraftRegisterRequestingTakeOffIdentifer[LocalCallCount],
    fileObject);
    // Print result to file

    fputs("\n", fileObject);
    fclose(fileObject);
    return 0;
}