Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6874629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:13:07+00:00 2026-05-27T04:13:07+00:00

How do I convert binary data to hex value in obj-c? Example: 1111 =

  • 0

How do I convert binary data to hex value in obj-c?
Example:

1111 = F, 
1110 = E, 
0001 = 1, 
0011 = 3.

I have a NSString of 10010101010011110110110011010111, and i want to convert it to hex value.
Currently I’m doing in a manual way. Which is,

-(NSString*)convertToHex:(NSString*)hexString

{
NSMutableString *convertingString = [[NSMutableString alloc] init];

for (int x = 0; x < ([hexString length]/4); x++) {

    int a = 0;
    int b = 0; 
    int c = 0;
    int d = 0;

    NSString *A = [NSString stringWithFormat:@"%c", [hexString characterAtIndex:(x)]];
    NSString *B = [NSString stringWithFormat:@"%c", [hexString characterAtIndex:(x*4+1)]];
    NSString *C = [NSString stringWithFormat:@"%c", [hexString characterAtIndex:(x*4+2)]];
    NSString *D = [NSString stringWithFormat:@"%c", [hexString characterAtIndex:(x*4+3)]];


    if ([A isEqualToString:@"1"]) { a = 8;}

    if ([B isEqualToString:@"1"]) { b = 4;}

    if ([C isEqualToString:@"1"]) { c = 2;}

    if ([D isEqualToString:@"1"]) { d = 1;}

    int total = a + b + c + d;

    if (total < 10) { [convertingString appendFormat:@"%i",total]; }
    else if (total == 10) { [convertingString appendString:@"A"]; }
    else if (total == 11) { [convertingString appendString:@"B"]; }
    else if (total == 12) { [convertingString appendString:@"C"]; }
    else if (total == 13) { [convertingString appendString:@"D"]; }
    else if (total == 14) { [convertingString appendString:@"E"]; }  
    else if (total == 15) { [convertingString appendString:@"F"]; } 

}

NSString *convertedHexString = convertingString;
return [convertedHexString autorelease];
[convertingString release];

}

Anyone have better suggestion? This is taking too long.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:13:08+00:00Added an answer on May 27, 2026 at 4:13 am

    I have never been much of a C hacker myself, but a problem like this is perfect for C, so here is my modest proposal – coded as test code to run on the Mac, but you should be able to copy the relevant bits out to use under iOS:

    #import <Foundation/Foundation.h>
    
    int main(int argc, char *argv[]) {
        NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
    
        NSString *str = @"10010101010011110110110011010111";
    
        char* cstr = [str cStringUsingEncoding: NSASCIIStringEncoding];
    
        NSUInteger len = strlen(cstr);
    
        char* lastChar = cstr + len - 1;
        NSUInteger curVal = 1;
    
        NSUInteger result = 0;
    
        while (lastChar >= cstr) {
    
            if (*lastChar == '1')
            {
                result += curVal;
            }
            /*
            else 
            {
                // Optionally add checks for correct characters here
            }
            */
    
            lastChar--;
            curVal <<= 1;
        }
    
        NSString *resultStr = [NSString stringWithFormat: @"%x", result];
    
        NSLog(@"Result: %@", resultStr);
    
        [p release];
    }
    

    It seems to work, but I am sure that there is still room for improvement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a binary that I want to convert to standard MIME-compliant base64 string.
How can I convert/export binary data (for example binary files like excel- or word-documents)
Does anybody have sample code to convert from binary data to string in c#?
How can I convert image to binary data ..??? I want to send that
I want to convert binary data to hexadecimal, just that, no fancy formatting and
I want to convert a binary number into a float number. Here's an example
I've been told this : Computers convert binary data into the hexadecimal (hex) number
If I have some binary data D And I convert it to string S.
can anyone show how to correctly convert binary represented data into double value in
How can I convert the binary string $x_bin=0001001100101 to its numeric value $x_num=613 in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.