求解C语言问题 ?
输入一个4字节的变量示例
Please Enter 4-byte Value:
0x12345678
输出:1
2
3
4
5
6
7
8
#include <stdio.h> int main (void) { int a; int i=0,b[8]; printf("Enter 32-bit value:"); scanf("%x",&a); //check input if(a>0xffffffff||a<0xffffff) return 0; //convert do { b[i]=a&0xf; a>>=4; i++; }while(a!=0); //output for(i--;i>=0;i--) printf("%x\n",b[i]); return 0; }
#include <stdio.h> int main(void) { int x; printf("Please enter 4-byte value:\n"); scanf("%x", &x); while(x) { printf("%x\n", (x & 0xF0000000) >> 28); x <<= 4; } return 0; }上面没验证,下面写个验证的:
#include <stdio.h> int main(void) { int x; printf("Please enter 4-byte value:\n"); scanf("%x", &x); if((x & 0xF) == 0) { printf("Unsupported operation!\n"); return 1; } while(x) { printf("%x\n", (x & 0xF0000000) >> 28); x <<= 4; } return 0; }