Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 4:9fefb9f18f08
- Parent:
- 3:1883ea70d247
- Child:
- 5:3849b0d9fcf8
diff -r 1883ea70d247 -r 9fefb9f18f08 main.cpp
--- a/main.cpp Wed Dec 23 06:51:02 2020 +0000
+++ b/main.cpp Thu Dec 24 18:04:01 2020 +0000
@@ -6,25 +6,38 @@
int main(void)
{
/* 変数の宣言 */
+ float num = 154.243; //calsatから受け取った10進数のデータ
+ float n = num * 2 / 19.68; //変換式
+ int Integer_N = (int)n; //
+ int Fractional_N = (n - (int)n)*32768;
int i;
- int decimal = 14;
- int binary[7]={0};
+ int intbin[7]={0};
+ int decbin[14]={0};
+
- /* 10進数の入力
- printf("10進数 = ");
- scanf("%d", &decimal);*/
-
- /* 10進数→2進数の変換 */
- for(i=0;decimal>0;i++){
- binary[i] = decimal % 2;
- decimal = decimal / 2;
+ /* 小数部 10進数→2進数の変換 */
+ int intdec = Fractional_N * 100;
+ for(i=0;intdec>0;i++){
+ decbin[i] = Fractional_N % 2;
+ Fractional_N = Fractional_N / 2;
+ }
+ /*整数部 10進数→2進数の変換 */
+ for(i=0;Integer_N >0;i++){
+ intbin[i] = Integer_N % 2;
+ Integer_N = Integer_N / 2;
}
/* 2進数の出力 */
- printf(" 2進数 = ");
+ printf(" 整数部の2進数 = ");
int k = 8;
while( k>0 ){
- printf("%d", binary[--k]);
+ printf("%d", intbin[--k]);
+ }
+ printf("\n");
+ printf(" 小数部の2進数 = ");
+ int l = 15;
+ while( l>0 ){
+ printf("%d", decbin[--l]);
}
printf("\n");