将十进制转换为二进制(字符的位图) [英] Convert Decimal to Binary (Bitmap of a character)

查看:133
本文介绍了将十进制转换为二进制(字符的位图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于从字符(例如A)中获取位图的问题.

I have a question about getting a Bitmap from a character, for example A.

我已经在互联网上搜索,但没有直接帮助.我找到了页面,我的计划是描述.

I was already searching in the internet but no direct help. I found this page, where my plans are described.

此网站的报价:

例如字符char ="A" bits ="227873781661662"转换为二进制格式的"0000 0000 0000 0000 1100 1111 0011 1111 1111 1111 1100 1111 0011 1111 1101 1110".

e.g. character char="A" bits="227873781661662" which translates to "0000 0000 0000 0000 1100 1111 0011 1111 1111 1111 1100 1111 0011 1111 1101 1110" in binary.

它们如何从227873781661662变为0000 0000 0000 0000 1100 1111 0011 1111 1111 1111 1100 1111 0011 1111 1101 1110?

How do they get from 227873781661662 to 0000 0000 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);

这有什么问题?感谢您的帮助

What is wrong with this? Thanks for help

推荐答案

要将数字从十进制转换为二进制:

To convert a number from decimal to binary:

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中,输出为:

With the example num above output is:

这篇关于将十进制转换为二进制(字符的位图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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