Last active
November 1, 2015 12:58
-
-
Save KentaYamada/a2287c1c4bf88ded0a08 to your computer and use it in GitHub Desktop.
10進数から2進数、16進数に変換して表示するプログラム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------------------------------------------- | |
//10進数→2進数 & 16進数変換プログラム(C言語版) | |
//エミュレータで通信信号送る時に計算めんどくさいから作ってみた。 | |
//ナンセンスな部分があるのはご愛嬌 | |
//------------------------------------------------------------------- | |
#include <stdio.h> | |
void to_binary(int decimal) | |
{ | |
int buf[32] = {0}; | |
int i = 0; | |
while(0 < decimal) | |
{ | |
buf[i] = decimal % 2; | |
decimal /= 2; | |
i++; | |
} | |
while(0 < i) | |
{ | |
printf("%1d", buf[--i]); | |
} | |
putchar('\n'); | |
} | |
int main(void) | |
{ | |
printf("10進数→2進数・16進数変換プログラム\n"); | |
while(1) | |
{ | |
int decimal; | |
printf("10進数: "); | |
scanf("%d", &decimal); | |
to_binary(decimal); | |
printf("16進数: %x\n", decimal); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace DecToBinAndHex | |
{ | |
/// <summary> | |
/// こっちじゃないと仕事で使えないと言われそう… | |
/// </summary> | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("10進数→2進数 & 16進数変換プログラム"); | |
while (true) | |
{ | |
Console.Write("10進数: "); | |
int i = -1; | |
var v = Console.ReadLine(); | |
if (!int.TryParse(v, out i)) | |
{ | |
Console.WriteLine("入力値に誤りがあります。"); | |
break; | |
} | |
i = int.Parse(v); | |
Console.WriteLine("2進数: {0}", Convert.ToString(i, 2)); | |
Console.WriteLine("16進数: {0}", Convert.ToString(i, 16)); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*-coding: utf-8 -*- | |
if __name__ == '__main__': | |
print("10進数 → 2進数、16進数変換スクリプト") | |
while(True): | |
print("Please input number:", end="") | |
val = input() | |
if not val.isdigit(): | |
print("Input error:vale is not a number. exit application.") | |
exit(1) | |
num = int(val) | |
print("Binary: {0:b}".format(num)) | |
print("Hex: {0:x}".format(num)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment