如何转换,并打破了2字节整数到C 2不同字符? [英] How do I convert and break a 2 byte integer into 2 different chars in C?

查看:98
本文介绍了如何转换,并打破了2字节整数到C 2不同字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换一个unsigned int,并打入2个字符。例如:如果该整数1,其二进制重新presentation将是0000 0001我想在一个字符变量中的部分0000和另一个二元变量0001的一部分。如何在C ++中实现这一点?

I want to convert an unsigned int and break it into 2 chars. For example: If the integer is 1, its binary representation would be 0000 0001. I want the 0000 part in one char variable and the 0001 part in another binary variable. How do I achieve this in C?

推荐答案

如果你坚持,你有一个的sizeof(int)的== 2 则:

If you insist that you have a sizeof(int)==2 then:

  unsigned int x = (unsigned int)2; //or any other value it happens to be
  unsigned char high = (unsigned char)(x>>8);
  unsigned char low  = x & 0xff;


如果您有八位总(一个字节),你都分解成两个4位值:


If you have eight bits total (one byte) and you are breaking it into two 4-bit values:

  unsigned char x=2;// or whatever
  unsigned char high = (x>>4);
  unsigned char low = x & 0xf;

这篇关于如何转换,并打破了2字节整数到C 2不同字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆