C MLT-3的二进制数

C MLT-3的二进制数,c,networking,C,Networking,我需要写一个C程序,它在输入端有一个二进制数,在输出端有相同的MLT-3代码(电压+,0,-)。电压仅在“1”处变化,在“0”处不变: 10010111 +++00-0+ 如何“滚动”这些值?代码指示的基本流程 #include <stdio.h> int main(void){ int direction = 1;//1: upto, -1: downto int state = 0; char *seq = "10010111"; while(

我需要写一个C程序,它在输入端有一个二进制数,在输出端有相同的MLT-3代码(电压+,0,-)。电压仅在“1”处变化,在“0”处不变:

10010111
+++00-0+

如何“滚动”这些值?

代码指示的基本流程

#include <stdio.h>

int main(void){
    int direction = 1;//1: upto, -1: downto
    int state = 0;
    char *seq = "10010111";
    while(*seq){
        if(*seq == '1'){
            state += direction;
            if(state == 1 || state == -1)
                direction = -direction;//reversal of direction
        }
        switch(state){//putchar("-0+"[state+1]);
        case 1: putchar('+');break;
        case 0: putchar('0');break;
        case -1: putchar('-');break;
        }
        ++seq;
    }
    return 0;
}
#包括
内部主(空){
int direction=1;//1:upto,-1:downto
int state=0;
char*seq=“10010111”;
while(*seq){
如果(*序号=='1'){
状态+=方向;
如果(状态==1 | |状态==-1)
方向=-方向;//方向反转
}
开关(状态){//putchar(“-0+”[state+1]);
案例1:putchar(+);break;
案例0:putchar('0');break;
案例1:putchar('-');break;
}
++序号;
}
返回0;
}
#include <stdio.h>

int main()
{
    char states[4] = { '+', '0', '-', '0' };
    int index = 3;
    int b = 0x97;         // OP example
    int i;
    for (i=0; i<8; i++) {
        if (b & 0x80)
            index = (index + 1) % 4;
        printf ("%c", states[index]);
        b <<= 1;
    }
    printf("\n");
    return 0;
}
+++00-0+