数据转换总结

1、从0F的字符得到对应的数字,例如字符‘F’,得到数字15

int GetIntFromChar(char c)

{

int n;

if(c>='0'&&c<='9')          // if(c>=0x30&&c<=0x39)

n = c - '0';  // n = c - 0x30;

else if(c>='A'&&c<='F')  // else if(c>=0x41&&c<=0x5A)

n = c - 'A' + 10;  // n = c - 0x41 + 10;

else if(c>='a'&&c<='f')  // else if(c>=0x61&&c<=0x7A)

n = c - 'a' + 10;    // n = c - 0x61 + 10;

return n;

}

笨方法:

int GetIntFromChar(char c)

{

if(c == '0')return 0;

else if(c == '1')return 1;

else if(c == '2')return 2;

else if(c == '3')return 3;

else if(c == '4')return 4;

else if(c == '5')return 5;

else if(c == '6')return 6;

else if(c == '7')return 7;

else if(c == '8')return 8;

else if(c == '9')return 9;

else if(c == 'a' || c == 'A')return 10;

else if(c == 'b' || c == 'B')return 11;

else if(c == 'c' || c == 'C')return 12;

else if(c == 'd' || c == 'D')return 13;

else if(c == 'e' || c == 'E')return 14;

else if(c == 'f' || c == 'F')return 15;

}

 

2、从015的数字,例如对应的字符,例如数字15得到字符‘F’。

char GetCharFromInt(int n)

{

char c;

if(n>=0&&n<=9) // if(n>=0&&n<=9)

c = '0' + n; // c = 0x30 + n;

if(n>=10&&n<=15) // if(n>=10&&n<=15)

c = 'A'+ n -10; // c = 0x41 + n -10;

return c;

}

笨方法:

char GetCharFromInt(int n)

{

if(c == 0)return '0';

else if(c == 1)return 1;

else if(c == 2)return 2;

else if(c == 3)return 3;

else if(c == 4)return 4;

else if(c == 5)return 5;

else if(c == 6)return 6;

else if(c == 7)return 7;

else if(c == 8)return 8;

else if(c == 9)return 9;

else if(c == 10)return 'A';

else if(c == 11)return 'B';

else if(c == 12)return 'C';

else if(c == 13)return 'D';

else if(c == 14)return 'E';

else if(c == 15)return 'F';

}

 

注:

字符’0’到’9’对应的ASCII码是0x30~0x39(十进制为48~57)

26个大写英文字母的ASCII码是0x41~0x5A(十进制为65~90)

26个小写英文字母的ASCII码是0x61~0x7A(十进制为97~122)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值