求C语言高手编写Code!This segment of code takes a value(IN) and converts it into a set of characters,in which there are 3 characters which represent the value as a 3 digit hex number.char OUT[6] //initializes an array with 6 spaces,each space h

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/09 14:12:38
求C语言高手编写Code!This segment of code takes a value(IN) and converts it into a set of characters,in which there are 3 characters which represent the value as a 3 digit hex number.char OUT[6] //initializes an array with 6 spaces,each space h

求C语言高手编写Code!This segment of code takes a value(IN) and converts it into a set of characters,in which there are 3 characters which represent the value as a 3 digit hex number.char OUT[6] //initializes an array with 6 spaces,each space h
求C语言高手编写Code!
This segment of code takes a value(IN) and converts it into a set of characters,in which there are 3 characters which represent the value as a 3 digit hex number.
char OUT[6] //initializes an array with 6 spaces,each space holds one character
cMSB = (0xF00 &IN) >> 8;
cMIDB = (0x0F0 & IN) >> 4;
cLSB = (0x00F & IN);
OUT[0] = '0';
OUT[1] = 'x';
//OUT[0] and OUT[1] are the characters:0x,which is the notation we use to
//represent a hex number
OUT[2] = (cMSB > 9) 'A' + (cMSB - 10) :'0' + cMSB;
OUT[3] = (cMIDB > 9) 'A' + (cMIDB - 10) :'0' + cMIDB;
OUT[4] = (cLSB > 9) 'A' + (cLSB - 10) :'0' + cLSB;
OUT[5] = '\n'; //This triggers a new line
求高手用C语言把这个语言在编写一遍~感激不尽啊~
我是新手~没有高的悬赏值麻烦大家帮帮啦

求C语言高手编写Code!This segment of code takes a value(IN) and converts it into a set of characters,in which there are 3 characters which represent the value as a 3 digit hex number.char OUT[6] //initializes an array with 6 spaces,each space h
#include <stdio.h>

char *Convert(int IN, char OUT[]) {
int cMSB = (0xF00 & IN) >> 8;
int cMIDB = (0x0F0 & IN) >> 4;
int cLSB = (0x00F & IN);

OUT[0] = '0';
OUT[1] = 'X';
OUT[2] = (cMSB > 9) ? 'A' + (cMSB - 10) : '0' + cMSB;
OUT[3] = (cMIDB > 9) ? 'A' + (cMIDB - 10) : '0' + cMIDB;
OUT[4] = (cLSB > 9) ? 'A' + (cLSB - 10) : '0' + cLSB;
OUT[5] = '\n'; //This triggers a new line
return OUT;
}

int main() {
char res[6];
int a[] = {160,225,275,209,188,665,221,986};
int i,j,n = sizeof(a)/sizeof(a[0]);
for(i = 0; i < n; ++i) {
printf("0X%03X(%d) : ",a[i],a[i]);
Convert(a[i],res);
for(j = 0; res[j] != '\n'; ++j)
putchar(res[j]);
putchar('\n');
}
return 0;
}