码迷,mamicode.com
首页 > 编程语言 > 详细

论C/C++数据在内存中的二进制存放形式

时间:2014-06-15 09:44:48      阅读:414      评论:0      收藏:0      [点我收藏+]

标签:二进制   内存   c   c++   

// enter any type data to show Binary.c
// 输入任意类型数据转换成二进制并显示
// version: 2.0
// date: 2014.6.13
// BUG提交: yinjunwithu@outlook.com
// 缺陷: 请勿使用vc 6.0编译 因为不支持long long及unsigned long long类型

#define _CRT_SECURE_NO_WARNINGS // Microsoft Visual Studio 2013编译器请添加本行
#include <stdio.h>
#include <limits.h> /* CHAR_BIT所属h文件 CHAR_BIT = 8 */
#define MAX	11 // 最大选项数
#define EATLINE while (getchar() != '\n') continue // 应输入数字但实际输入字母则剔除错误输入
#define SHOW_ERROR(str)	printf("wrong %s number!\n", str) // 显示输入错误语句

int menu(void); 
void convert(const unsigned char * const ch, const 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;

	union
	{
		long double value;
		// 用unsigned类型是为了与mask类型一致
		unsigned char array[sizeof (long double)];
	} ld_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)
			{
				EATLINE;
				SHOW_ERROR("char");
				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 %u) : ", UCHAR_MAX);
			while (scanf("%llu", &llu_data.value) != 1)
			{
				EATLINE;
				SHOW_ERROR("unsigned char");
				printf("Please input a unsigned char number (0 to %u) : ", 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)
			{
				EATLINE;
				SHOW_ERROR("short");
				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 %u) : ", USHRT_MAX);
			while (scanf("%llu", &llu_data.value) != 1)
			{
				EATLINE;
				SHOW_ERROR("unsigned short");
				printf("Please input a unsigned short number (0 to %u) : ", 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)
			{
				EATLINE;
				SHOW_ERROR("int");
				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)
			{
				EATLINE;
				SHOW_ERROR("unsigned int");
				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)
			{
				EATLINE;
				SHOW_ERROR("long long");
				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)
			{
				EATLINE;
				SHOW_ERROR("unsigned long long");
				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)
			{
				EATLINE;
				SHOW_ERROR("float");
				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)
			{
				EATLINE;
				SHOW_ERROR("double");
				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;

		case 11: // long double
			printf("Please input a long double number : ");
			while (scanf("%lf", &ld_data.value) != 1)
			{
				EATLINE;
				SHOW_ERROR("long double");
				printf("Please input a long double number : ");
			}
			printf("\n%lf is in %#p\nBinary : ", ld_data.value, &ld_data.value);
			convert(ld_data.array, sizeof(long 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("11\tlong double\n");
	printf("Please input the number to select : ");
	while ((status = scanf("%d", &code)) != 1 || (code > MAX || code < 0))
	{
		if (status != 1)
			scanf("%*s");
		printf("input an integer from 0 to %d,please\n", MAX);
	}
	return code;
}

void convert(const unsigned char * const ch, const 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数据
// 只不过显示的时候都被截断了
// 自我感觉写得不好 应该还能优化


总结:左侧为内存高地址,右侧为低地址 数据首地址为最末字节的地址 见图

bubuko.com,布布扣





论C/C++数据在内存中的二进制存放形式,布布扣,bubuko.com

论C/C++数据在内存中的二进制存放形式

标签:二进制   内存   c   c++   

原文地址:http://blog.csdn.net/u010518429/article/details/30332237

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!