如何在C ++中从Hexa十进制数中分割数字 [英] How to split a digits from Hexa decimal number in C++

查看:77
本文介绍了如何在C ++中从Hexa十进制数中分割数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







我有一个十六进制数为exp 0x156A,现在我想分开最低有效数字(4位)并且同样想分开每个数字​​。



有人有任何想法,怎么做C ++ ???

< br $>




谢谢,

Baakki

解决方案

假设您的十六进制数字是整数或类似的:

  int  GetDigit( int   value  int  digitNumber)
{
返回 value >>( 4 * digitNumber))& 0x000F;
}


屏蔽你想要的位,向右移位以使结果位于低位。



所以要从0x156A获得0x6,你首先将其屏蔽掉,它被0xF0覆盖。

那么,因为它是第二组的4位把它降低4位。



这样的东西可以帮到你;



  #include   <   iostream  >  

使用 命名空间标准;

int main(){
const int source = 0x156A;

const int first_4 =(source& 0xF)>> 0 ;
const int second_4 =(source& 0xF0)>> 4 ;
const int third_4 =(source& 0xF00)>> 8 ;
const int fourth_4 =(source& 0xF000)>> 12 ;

cout<<十六进制;
cout<< Source = 0x<<来源<< ENDL;
cout<< 前四位= 0x<< first_4<< ENDL;
cout<< 后四位= 0x<< second_4<< ENDL;
cout<< 第三个四位= 0x<< third_4<< ENDL;
cout<< 第四个四位= 0x<< fourth_4<< ENDL;

return 0 ;
}





希望这会有所帮助,

Fredrik


< blockquote>因为我无法相信没有其他人想出这个并且每个人都在轮班,面具和工会中放屁:



  char  least_significant_hex_digit( unsigned  number)
{
return number% 16 ;
}





首先计算后续数字除以16:



 std :: vector< char> split_into_hex_digits( unsigned  number)
{
std :: vector< char>数字;
for (; number; number / = 16
{
digits.push_back(number% 16 );
}
返回位数;
}< / char>< / char>





有了一个不错的优化器,它最终会像任何使用移位并且可能更快的东西,因为大多数处理器在进行除法时产生余数,这将除以除以模数。


Hi,


I have a hex number for exp 0x156A , now I want to split least significant digit(4bit) seperately and the same way want to split each digits seperately.

Do anybody have any idea, how to do in C++ ???



Thanks,
Baakki

解决方案

Assuming your hex number is in an integer or similar:

int GetDigit(int value, int digitNumber)
   {
   return (value >> (4 * digitNumber)) & 0x000F;
   }


Mask the bits out you want, right shift down so that the result is in the lower bits.

So to get the 0x6 from 0x156A you'd first mask it out, and it is covered by 0xF0.
Then, because it is the second group of 4 bits shift that down by 4 bits.

Something like this might help you;

#include <iostream>

using namespace std;

int main() {
	const int source = 0x156A;

	const int first_4 = (source & 0xF) >> 0;
	const int second_4 = (source & 0xF0) >> 4;
	const int third_4 = (source & 0xF00) >> 8;
	const int fourth_4 = (source & 0xF000) >> 12;

	cout << hex;
	cout << "Source           = 0x" << source << endl;
	cout << "First four bits  = 0x" << first_4 << endl;
	cout << "Second four bits = 0x" << second_4 << endl;
	cout << "Third four bits  = 0x" << third_4 << endl;
	cout << "Fourth four bits = 0x" << fourth_4 << endl;
	
    return 0;
}



Hope this helps,
Fredrik


As I can't believe no-one else's come up with this and everyone's been farting around with shifts, masks and unions:

char least_significant_hex_digit( unsigned number )
{
     return number % 16;
}



To work out subsequent digits divide by 16 first:

std::vector<char> split_into_hex_digits( unsigned number )
{
    std::vector<char> digits;
    for( ; number; number /= 16 )
    {
        digits.push_back( number % 16 );
    }
    return digits;
}</char></char>



With a decent optimiser it'll end up as fast as anything using shifts and potentially faster as most processors produce a remainder when doing a division which'll combine the divide and modulo.


这篇关于如何在C ++中从Hexa十进制数中分割数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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