#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
// ฟังก์ชันแปลง Binary ไปเป็น Gray Code
unsigned char binaryToGray(unsigned char num) {
return num ^ (num >> 1);
}
// ฟังก์ชันแปลง Binary ไปเป็น Signed Decimal (2's complement)
int binaryToSigned(char *binary) {
int result = 0;
if (binary[0] == '1') { // msb = 1 คือเลขลบ
for (int i = 0; i < 8; i++) {
result = result * 2 + (binary[i] == '0' ? 1 : 0);
}
result = -(result + 1);
} else {
for (int i = 0; i < 8; i++) {
result = result * 2 + (binary[i] - '0');
}
}
return result;
}
// ฟังก์ชันแปลง Binary ไป Fixed Point [4.4]
float binaryToFixedPoint(char *binary) {
float result = 0.0;
int int_part = 0, frac_part = 0;
// คำนวณส่วนจำนวนเต็ม (4 บิตแรก)
for (int i = 0; i < 4; i++) {
int_part = int_part * 2 + (binary[i] - '0');
}
// คำนวณส่วนทศนิยม (4 บิตสุดท้าย)
for (int i = 4; i < 8; i++) {
frac_part = frac_part * 2 + (binary[i] - '0');
}
result = int_part + (frac_part / 16.0); // 2^4 = 16
return result;
}
int main() {
char binary[9]; // เก็บเลข 8 บิต + null terminator
printf("Enter a binary code [8-bit]: ");
// แปลง binary เป็น unsigned decimal
unsigned char num
= (unsigned char)strtol(binary
, NULL
, 2);
printf("unsigned decimal : %u\n", num
); printf("hexadecimal : %X\n", num
);
// แปลง binary เป็น signed decimal
int signedNum = binaryToSigned(binary);
printf("signed decimal (2's complement): %d\n", signedNum
);
// แปลง binary เป็น Gray Code
unsigned char grayCode = binaryToGray(num);
printf("gray code: %u [%08b]\n", grayCode
, grayCode
);
// แปลง binary เป็น Fixed Point [4.4]
float fixedPoint = binaryToFixedPoint(binary);
printf("fixed point [4.4]: %.6f\n", fixedPoint
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPG1hdGguaD4KI2luY2x1ZGUgPHN0ZGludC5oPgoKLy8g4Lif4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vLyDguITguLPguJnguKfguJPguKrguYjguKfguJnguIjguLPguJnguKfguJnguYDguJXguYfguKEgKDQg4Lia4Li04LiV4LmB4Lij4LiBKQogICAgZm9yIChpbnQgaSA9IDA7IGkgPCA0OyBpKyspIHsKICAgICAgICBpbnRfcGFydCA9IGludF9wYXJ0ICogMiArIChiaW5hcnlbaV0gLSAnMCcpOwogICAgfQoKICAgIC8vIOC4hOC4s+C4meC4p+C4k+C4quC5iOC4p+C4meC4l+C4qOC4meC4tOC4ouC4oSAoNCDguJrguLTguJXguKrguLjguJTguJfguYnguLLguKIpCiAgICBmb3IgKGludCBpID0gNDsgaSA8IDg7IGkrKykgewogICAgICAgIGZyYWNfcGFydCA9IGZyYWNfcGFydCAqIDIgKyAoYmluYXJ5W2ldIC0gJzAnKTsKICAgIH0KCiAgICByZXN1bHQgPSBpbnRfcGFydCArIChmcmFjX3BhcnQgLyAxNi4wKTsgLy8gMl40ID0gMTYKICAgIHJldHVybiByZXN1bHQ7Cn0KCmludCBtYWluKCkgewogICAgY2hhciBiaW5hcnlbOV07IC8vIOC5gOC4geC5h+C4muC5gOC4peC4giA4IOC4muC4tOC4lSArIG51bGwgdGVybWluYXRvcgogICAgcHJpbnRmKCJFbnRlciBhIGJpbmFyeSBjb2RlIFs4LWJpdF06ICIpOwogICAgc2NhbmYoIiU4cyIsIGJpbmFyeSk7CgogICAgLy8g4LmB4Lib4Lil4LiHIGJpbmFyeSDguYDguJvguYfguJkgdW5zaWduZWQgZGVjaW1hbAogICAgdW5zaWduZWQgY2hhciBudW0gPSAodW5zaWduZWQgY2hhcilzdHJ0b2woYmluYXJ5LCBOVUxMLCAyKTsKCiAgICBwcmludGYoInVuc2lnbmVkIGRlY2ltYWwgOiAldVxuIiwgbnVtKTsKICAgIHByaW50Zigib2N0YWwgICAgICAgICAgICA6ICVvXG4iLCBudW0pOwogICAgcHJpbnRmKCJoZXhhZGVjaW1hbCAgICAgIDogJVhcbiIsIG51bSk7CgogICAgLy8g4LmB4L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