十进制到二进制转换方法的Objective-C [英] Decimal to Binary conversion method Objective-C

查看:114
本文介绍了十进制到二进制转换方法的Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想使一个小数在Objective-C的二进制数转换器,但一直不成功的......我有下面的方法,到目前为止这是从Java的尝试翻译了类似的方法。任何帮助,使这种方法工作是非常AP preciated。

  +(的NSString *)DecToBinary:(INT)decInt
{
    INT结果为0;
    INT乘数;
    INT基准= 2;
    而(decInt大于0)
    {
        INT R = decInt%2;
        decInt = decInt /基地;
        结果=结果+ R *乘数;
        乘数=乘数* 10;
    }
返回[的NSString stringWithFormat:@%D,导致]
 

解决方案

我会用位移位到达整数的每一位

  X = X>> 1;
 

由一个移动的比特向左,小数13是重新presente以位为1101,所以它移位到右边匝将创建110 - > 6

  X'放大器; 1
 

是位掩码x,其中1

  1101
&放大器; 0001
------
= 0001
 

综合这些线路将遍历形成最低到最高位,我们可以添加此位为整数格式化为一个字符串。

有关unsigned int类型也可能是这一点。

 #进口<基金会/ Foundation.h>

@interface的BinaryFormatter:NSObject的
+(的NSString *)decToBinary:(NSUInteger)decInt;
@结束

@implementation的BinaryFormatter

+(的NSString *)decToBinary:(NSUInteger)decInt
{
    的NSString *字符串= @;
    NSUInteger X = decInt;

    而(X 0){
        字符串= [[NSString的stringWithFormat:@%禄,X放大器; 1] stringByAppendingString:字符串];
        X = X>> 1;
    }
    返回的字符串;
}
@结束

INT主(INT ARGC,为const char * argv的[])
{
    @autoreleasepool {
        的NSString * binaryRe presentation = [BinaryFormatter的decToBinary:13]
        的NSLog(@%@,binaryRe presentation);
    }
    返回0;
}
 

这code将返回 1101 ,13个二进制重新presentation。


较短的形式与DO-同时, X>> = 1 是 X = X&GT的缩写形式;> 1

  +(的NSString *)decToBinary:(NSUInteger)decInt
{
    的NSString *字符串= @;
    NSUInteger X = decInt;
    做 {
        字符串= [[NSString的stringWithFormat:@%禄,X放大器; 1] stringByAppendingString:字符串];
    }而(X GT;> = 1);
    返回的字符串;
}
 

Hi I am trying to make a Decimal to binary number converter in Objective-C but have been unsucessful... I have the following method so far which is an attempted translation from Java for a similar method. Any help to make this method work is much appreciated.

 +(NSString *) DecToBinary: (int) decInt
{
    int result = 0;
    int multiplier;
    int base = 2;
    while(decInt > 0)
    {
        int r = decInt % 2;
        decInt = decInt / base;
        result = result + r * multiplier;
        multiplier = multiplier * 10;
    }
return [NSString stringWithFormat:@"%d",result];

解决方案

I would use bit shifting to reach each bit of the integer

x = x >> 1;

moves the bits by one to the left, the decimal 13 is represente in bits as 1101, so shifting it to the right turns creates 110 -> 6.

x&1

is the bit masking x with 1

  1101
& 0001
------
= 0001

Combined these lines will iterate form the lowest to highest bit and we can add this bit as formatted integer to a string.

For unsigned int it could be this.

#import <Foundation/Foundation.h>

@interface BinaryFormatter : NSObject
+(NSString *) decToBinary: (NSUInteger) decInt;
@end

@implementation BinaryFormatter

+(NSString *)decToBinary:(NSUInteger)decInt
{
    NSString *string = @"" ;
    NSUInteger x = decInt;

    while (x>0) {
        string = [[NSString stringWithFormat: @"%lu", x&1] stringByAppendingString:string];
        x = x >> 1;
    }
    return string;
}
@end

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSString *binaryRepresentation = [BinaryFormatter decToBinary:13];
        NSLog(@"%@", binaryRepresentation);
    }
    return 0;
}

this code will return 1101, the binary representation of 13.


Shorter form with do-while, x >>= 1 is the short form of x = x >> 1:

+(NSString *)decToBinary:(NSUInteger)decInt
{
    NSString *string = @"" ;
    NSUInteger x = decInt ;
    do {
        string = [[NSString stringWithFormat: @"%lu", x&1] stringByAppendingString:string];
    } while (x >>= 1);
    return string;
}

这篇关于十进制到二进制转换方法的Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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