How to Convert NSInteger or NSString to a binary (string) value

后端 未结 2 955
野的像风
野的像风 2021-01-17 07:03

Anybody has some code in objective-c to convert a NSInteger or NSString to binary string?

example:

56 -> 111000

There are some code in stackoverflow

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-17 07:14

    Tooting my own horn a bit here...

    I've written a math framework called CHMath that deals with arbitrarily large integers. One of the things it does is allows the user to create a CHNumber from a string, and get its binary representation as a string. For example:

    CHNumber * t = [CHNumber numberWithString:@"56"];
    NSString * binaryT = [t binaryStringValue];
    NSLog(@"Binary value of %@: %@", t, binaryT);
    

    Logs:

    2009-12-15 10:36:10.595 otest-x86_64[21918:903] Binary value of 56: 0111000
    

    The framework is freely available on its Github repository.

提交回复
热议问题