这两天还在读x86保护模式的天书了,不过闲暇写了个二进制字符串转化为二进制数的幼稚程序。

 当然是方便自己查那些什么特权级,描述符,粒度位,段属性之类的信息了。

百度网盘下载地址:

https://pan.baidu.com/s/1_-IznMWL3z1CROziiD6mCw

#include <stdio.h>



#include "const.h"
//#include "global.h"
//#include "proc.h"
//#include "protect.h"
//#include "proto.h"

//#include "string.h"
#include "type.h"

unsigned int do_div(unsigned int *value, unsigned int base);
char *printbin(unsigned int value);
int str_len(char *str);
int str2bin(char *str);

int main(void) {
	printf("1 << 3 = 0x%x\n", 1 << 3);
	printf("%s\n", printbin(0xffffffff));
	printf("%s\n", printbin(0x9a));


	printf("%d\t 0x%x", str2bin("11111111"),  str2bin("11111111"));
	printf("%d\t 0x%x", str2bin("c111"),  str2bin("c111"));

}

unsigned int do_div(unsigned int *value, unsigned int base) {
	unsigned int temp;
	temp = *value % base;
	*value = *value / base;
	return temp;
}

char *printbin(unsigned int value) {
	char str[2] = "01";
	char tempa[128], tempb[128];
	char *temp1 = tempa, *temp2 = tempb;
	int count = 0;
	int count1 = 0;
	if(!value) {
		*temp2++ = '0';
	}
	while(value) {
		*temp1++ = str[do_div(&value, 2)];
		count++;	
		if(count % 4 == 0) {
			*temp1++ = ',';
			count1++;
		}
	}
	if (*(temp1 - 1) == ',') {
			temp1--;
			count1--;
	}
	count += count1;
	while(count) {
		*temp2++ = *--temp1;
		count--;
	}
	*temp2++ = 'b';
	*temp2 = '\0';
	temp2 = tempb;
	return temp2;
}

int str_len(char *str) {
	int len = 0;
	while(*str) {
		str++;
		len++;
	}
	return len;	
}

int str2bin(char *str) {
	int i = 0, len, result = 0;
	len = str_len(str);
	char temp[128], *temp1 = temp, c;
	for (i = len - 1; i >= 0; i--) {
		*temp1++ = str[i];
		if((str[i] < '0') || (str[i] > '1')) {
			printf("ERROR String Format!, %c\n!!!!!!!!!!!!!!!!!\n", str[i]);
			return -1;
		} 		
	}
	*temp1 = '\0';
	c = temp[0] - '0';
	if(len == 1) {
		return (int)c;
	}
	for (i = 1; i < len; i++) {
		result += (temp[i] - '0') * (2 << (i - 1));
	}
	result += c;
	return result;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39410618

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值