将十进制转换为二进制(字符的位图)

我有一个关于从angular色获取位图的问题,例如A.

我已经在网上search,但没有直接的帮助。 我find了这个页面,在那里描述了我的计划。

从这个网站引用:

例如字符char =“A”位=“227873781661662”,其转换为二进制的“0000 0000 0000 0000 1100 1111 0011 1111 1111 1111 1100 1111 0011 1111 1101 1110”。

他们如何从227873781661662到00000000 0000 0000 1100 1111 0011 1111 1111 1111 1100 1111 0011 1111 1101 1110?

int num = 227873781661662; int n = log(num)/log(2)+1;      //Figure out the maximum power of 2 needed NSString *result = @"";       //Empty string for (int j=n; j>=0; j--)       //Iterate down through the powers of 2 { if (pow(2,j) >= num)       //If the number is greater than current power of 2 { num -= pow(2,j);               //Subtract the power of 2 result = [result stringByAppendingString:@"1"]; //Add a 1 to result string } else result = [result stringByAppendingString:@"0"]; //Otherwise add a 0 if (num == 0) break;                //If we're at 0, stop } NSLog(@"num = %i",num); 

这有什么问题? 感谢帮助

将数字从小数转换为二进制:

 long long num = 938409238409283409; int n = log(num)/log(2)+1; //Figure out the maximum power of 2 needed NSString *result = @""; //Start with empty string for (int j=n; j>=0; j--) //Iterate down through the powers of 2 { long long curPOW = powl(2,j); if (curPOW <= num) //If the number is greater than current power of 2 { num -= curPOW; //Subtract the power of 2 result = [result stringByAppendingString:@"1"]; //Add a 1 to result string } else result = [result stringByAppendingString:@"0"]; //Otherwise add a 0 } NSLog(@"%@", result); //Result is now binary representation of num 

用上面的例子num输出是: 在这里输入图像说明

它们向您显示一个64位二进制数字的十进制表示。 他们的代码将数字解释为一个颠倒的8x6位matrix,最初的16位部分被丢弃。

下面将重新分组以说明发生了什么。 我六点摸索,把星号标记为1 ,用空格标记0表示下面的图像:

 0000 0000 0000 0000 -- Thrown away bits image ------ ------ 110011 ** ** 110011 ** ** 111111 ****** 111111 ****** 110011 ** ** 110011 ** ** 111111 ****** 011110 **** 

在Windows上,您可以使用计算器应用程序将二进制转换为十进制和返回。 select[View / Programmer],然后select“Bin”单选button。

这里是你如何可以将目标C中的数字转换为二进制:

 long long num = 227873781661662L; NSMutableString *res = [NSMutableString string]; while (res.length != 64) { [res insertString: [NSString stringWithFormat:@"%d", num%2] atIndex:0]; num >>= 1; } 

227873781661662是十进制的,显然1和0是二进制的。 要将十进制转换为二进制,或者将数字分成2的幂 (即2 ^ 0 = 1,2 ^ 1 = 2,2 ^ 2 = 4),这对于一个大数字来说可能是长的,或者只是使用计算器工具