程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 論C/C++數據在內存中的二進制存放形式

論C/C++數據在內存中的二進制存放形式

編輯:C++入門知識

// enter any type data to show Binary.c
// 輸入任意類型數據轉換成二進制並顯示
// version: 1.0
// date: 2014.6.12
// BUG提交: [email protected]
// 缺陷: 請勿使用vc 6.0編譯 因為不支持long long及unsigned long long類型

// #define _CRT_SECURE_NO_WARNINGS // Microsoft Visual Studio 2013編譯器請添加本行
#include 
#include  /* CHAR_BIT所屬h文件 CHAR_BIT = 8 */
#define MAX 10 // 最大選項數

int menu(void);
void convert(const unsigned char * const ch, char size);

int main(void)
{
	union
	{
		long long value;
		// 用unsigned類型是為了與mask類型一致
		unsigned char array[sizeof (long long)]; 
	}ll_data;
	union
	{
		unsigned long long value;
		// 用unsigned類型是為了與mask類型一致
		unsigned char array[sizeof (unsigned long long)];
	}llu_data;
	union
	{
		float value;
		// 用unsigned類型是為了與mask類型一致
		unsigned char array[sizeof (float)];
	} f_data;
	union
	{
		double value;
		// 用unsigned類型是為了與mask類型一致
		unsigned char array[sizeof (double)];
	} d_data;

	int select; // 選項

	// while ((select = menu()) != 0)
	while (select = menu())
	{
		switch (select)
		{
		case 1: // char
			printf("Please input a char number (%d to %d) : ", CHAR_MIN, CHAR_MAX);
			while (scanf("%lld", &ll_data.value) != 1)
			{
				while (getchar() != '\n')
					continue;
				printf("wrong char number!\n");
				printf("Please input a char number (%d to %d) : ", CHAR_MIN, CHAR_MAX);
			}
			printf("\n%lld is in %#p\nBinary : ", ll_data.value, &ll_data.value);
			convert(ll_data.array, sizeof(char));
			break;

		case 2:	// unsigned char
			printf("Please input a unsigned char number (0 to %d) : ", UCHAR_MAX);
			while (scanf("%llu", &llu_data.value) != 1)
			{
				while (getchar() != '\n')
					continue;
				printf("wrong unsigned char number!\n");
				printf("Please input a unsigned char number (0 to %d) : ", UCHAR_MAX);
			}
			printf("\n%llu is in %#p\nBinary : ", llu_data.value, &llu_data.value);
			convert(llu_data.array, sizeof(unsigned char));
			break;

		case 3: // short
			printf("Please input a short number (%d to %d) : ", SHRT_MIN, SHRT_MAX);
			while (scanf("%lld", &ll_data.value) != 1)
			{
				while (getchar() != '\n')
					continue;
				printf("wrong short number!\n");
				printf("Please input a short number (%d to %d) : ", SHRT_MIN, SHRT_MAX);
			}
			printf("\n%lld is in %#p\nBinary : ", ll_data.value, &ll_data.value);
			convert(ll_data.array, sizeof(short));
			break;

		case 4:	// unsigned short
			printf("Please input a unsigned short number (0 to %d) : ", USHRT_MAX);
			while (scanf("%llu", &llu_data.value) != 1)
			{
				while (getchar() != '\n')
					continue;
				printf("wrong unsigned short number!\n");
				printf("Please input a unsigned short number (0 to %d) : ", USHRT_MAX);
			}
			printf("\n%llu is in %#p\nBinary : ", llu_data.value, &llu_data.value);
			convert(llu_data.array, sizeof(unsigned short));
			break;

		case 5: // int
			printf("Please input a int number (%d to %d) : ", INT_MIN, INT_MAX);
			while (scanf("%lld", &ll_data.value) != 1)
			{
				while (getchar() != '\n')
					continue;
				printf("wrong int number!\n");
				printf("Please input a int number : (%d to %d) : ", INT_MIN, INT_MAX);
			}
			printf("\n%lld is in %#p\nBinary : ", ll_data.value, &ll_data.value);
			convert(ll_data.array, sizeof(int));
			break;

		case 6: // unsigned int
			printf("Please input a unsigned int number (0 to %u) : ", UINT_MAX);
			while (scanf("%llu", &llu_data.value) != 1)
			{
				while (getchar() != '\n')
					continue;
				printf("wrong unsigned int number!\n");
				printf("Please input a unsigned int number (0 to %u) : ", UINT_MAX);
			}
			printf("\n%llu is in %#p\nBinary : ", llu_data.value, &llu_data.value);
			convert(llu_data.array, sizeof(unsigned int));
			break;

		case 7: // long long
			printf("Please input a long long number (%lld to %lld) : ",LLONG_MIN, LLONG_MAX);
			while (scanf("%lld", &ll_data.value) != 1)
			{
				while (getchar() != '\n')
					continue;
				printf("wrong long long number!\n");
				printf("Please input a long long number (%lld to %lld) : ", LLONG_MIN, LLONG_MAX);
			}
			printf("\n%lld is in %#p\nBinary : ", ll_data.value, &ll_data.value);
			convert(ll_data.array, sizeof(long long));
			break;

		case 8: // unsigned long long
			printf("Please input a unsigned long long number (0 to %llu) : ", ULLONG_MAX);
			while (scanf("%llu", &llu_data.value) != 1)
			{
				while (getchar() != '\n')
					continue;
				printf("wrong unsigned long long number!\n");
				printf("Please input a unsigned long long number (0 to %llu) : ", ULLONG_MAX);
			}
			printf("\n%llu is in %#p\nBinary : ", llu_data.value, &llu_data.value);
			convert(llu_data.array, sizeof(unsigned long long));
			break;

		case 9: // float
			printf("Please input a float number : ");
			while (scanf("%f", &f_data.value) != 1)
			{
				while (getchar() != '\n')
					continue;
				printf("wrong float number!\n");
				printf("Please input a float number : ");
			}
			printf("\n%f is in %#p\nBinary : ", f_data.value, &f_data.value);
			convert(f_data.array, sizeof(float));
			break;

		case 10: // double
			printf("Please input a double number : ");
			while (scanf("%lf", &d_data.value) != 1)
			{
				while (getchar() != '\n')
					continue;
				printf("wrong double number!\n");
				printf("Please input a double number : ");
			}
			printf("\n%lf is in %#p\nBinary : ", d_data.value, &d_data.value);
			convert(d_data.array, sizeof(double));
			break;

		default:
			break;
		}
	}
	printf("Bye.\n");
	getch();
	return 0;
}

int menu(void)
{
	int code, status;
	printf("0\texit\n");
	printf("1\tchar\n");
	printf("2\tunsigned char\n");
	printf("3\tshort\n");
	printf("4\tunsigned short\n");
	printf("5\tint\n");
	printf("6\tunsigned int\n");
	printf("7\tlong long\n");
	printf("8\tunsigned long long\n");
	printf("9\tfloat\n");
	printf("10\tdouble\n");
	printf("Please enter the number to select : ");
	while ((status = scanf("%d", &code)) != 1 || (code > MAX || code < 0))
	{
		if (status != 1)
			scanf("%*s");
		printf("enter an integer from 0 to %d,please\n", MAX);
	}
	return code;
}

void convert(const unsigned char * const ch, char size)
{
	char i, j;
	unsigned char mask; // 掩碼用unsigned類型是為了無符號數右移以0填充
	for (i = size - 1; i >= 0; --i)
	{
		mask = 0x80; // 1000 0000B
		for (j = 0; j < CHAR_BIT; ++j)
		{
			(ch[i] & mask) == mask ? putchar('1') : putchar('0');
			// mask = mask >> 1;
			mask >>= 1; // 右移1位
			// 每輸出8位2進制就空一格
			if (j % CHAR_BIT == 7)
				putchar(' ');
		}
	}

	printf("\n\n");

	for (i = 0; i < size; ++i)
	{
		printf("%#p : \t", ch + i);
		mask = 0x80; // 1000 0000B
		for (j = 0; j < CHAR_BIT; ++j)
		{
			(ch[i] & mask) == mask ? putchar('1') : putchar('0');
			// mask = mask >> 1;
			mask >>= 1; // 右移1位
			// 每輸出8位2進制就空一格
			if (j % CHAR_BIT == 7)
				putchar(' ');
		}
		putchar('\n');
	}
	putchar('\n');
}
// 其實輸入的都是long long或unsigned long long數據
// 只不過顯示的時候都被截斷了
// 自我感覺寫得不好 應該還能優化

總結:左側為內存高地址,右側為低地址 數據首地址為最末字節的地址 見圖

\

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved