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 8870113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:38:38+00:00 2026-06-14T17:38:38+00:00

With reference to the following question: Convert NSData into HEX NSString I have solved

  • 0

With reference to the following question: Convert NSData into HEX NSString

I have solved the problem using the solution provided by Erik Aigner which is:

NSData *data = ...;
NSUInteger capacity = [data length] * 2;
NSMutableString *stringBuffer = [NSMutableString stringWithCapacity:capacity];
const unsigned char *dataBuffer = [data bytes];
NSInteger i;
for (i=0; i<[data length]; ++i) {
  [stringBuffer appendFormat:@"%02X", (NSUInteger)dataBuffer[i]];
}

However, there is one small problem in that if there are extra zeros at the back, the string value would be different. For eg. if the hexa data is of a string @”3700000000000000″, when converted using a scanner to integer:

unsigned result = 0;
NSScanner *scanner = [NSScanner scannerWithString:stringBuffer];
[scanner scanHexInt:&result];
NSLog(@"INTEGER: %u",result);

The result would be 4294967295, which is incorrect. Shouldn’t it be 55 as only the hexa 37 is taken?

So how do I get rid of the zeros?

EDIT: (In response to CRD)

Hi, thanks for clarifying my doubts. So what you’re doing is to actually read the 64-bit integer directly from a byte pointer right? However I have another question. How do you actually cast NSData to a byte pointer?

To make it easier for you to understand, I’ll explain what I did originally.

First, I displayed the data of the file I have (data is in hexadecimal)

NSData *file = [NSData dataWithContentsOfFile:@"file path here"];
NSLog(@"Patch File: %@",file);

Output:

enter image description here

Next, I read and offset the first 8 bytes of the file and converted them into a string.

// 0-8 bytes
[file seekToFileOffset:0];
NSData *b = [file readDataOfLength:8];
NSUInteger capacity = [b length] * 2;
NSMutableString *stringBuffer = [NSMutableString stringWithCapacity:capacity];
const unsigned char *dataBuffer = [b bytes];
NSInteger i;
for (i=0; i<[b length]; ++i) {
    [stringBuffer appendFormat:@"%02X", (NSUInteger)dataBuffer[i]];
}
NSLog(@"0-8 bytes HEXADECIMAL: %@",stringBuffer);

As you can see, 0x3700000000000000 is the next 8 bytes. The only changes I would have to make to access the next 8 bytes would be to change the value of SeekFileToOffset to 8, so as to access the next 8 bytes of data.

All in all, the solution you gave me is useful, however it would not be practical to enter the hexadecimal values manually. If formatting the bytes as a string and then parsing them is not the way to do it, then how do I access the first 8 bytes of the data directly and cast them into a byte pointer?

  • 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-06-14T17:38:39+00:00Added an answer on June 14, 2026 at 5:38 pm

    You appear to be trying to convert 8 bytes stored in an NSData into an 64-bit integer (there are 8 bytes in 0x3700000000000000).

    If this is your goal then doing it by formatting the bytes as a string and then parsing them is not the way to do it. You need to find out what endian format (i.e. least or most significant byte first) your data is in and then use the appropriate endian conversion functions.

    BTW the reason why you’re getting 4294967295 (0xFFFFFFFF) is because you are asking the NSScanner to read a 32-bit integer and your number (0x3700000000000000) overflows.

    Response to comment

    As the x86 is little-endian you can read a little-endian 64-bit integer direct from a byte pointer by just casting:

    Byte bytes[] = { 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    NSLog(@"%lld", *(int64_t *)bytes);
    

    If you need to read big-endian data then you can use an endian conversion function such as Endian64_Swap – which just flips what you have, so read then flip.

    Note that the x86 does not require data access to be aligned, but will perform better if it is.

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

Sidebar

Related Questions

i have following question. I tried assignment by value and by reference and as
After some searching, I got the following solution : reference . CLLocationCoordinate2D* new_coordinate =
I have a question regarding the passing of a map by reference. Let's consider
I found the following question Is Java "pass-by-reference" or "pass-by-value"? . I read almost
In reference to this question ( Server returned HTTP response code: 400 using Spring-Security
In reference to answer 1 of the following question , the answer states that
I recently looked at the following question: How to disable text selection highlighting using
In reference to the following question: iPhone: How do I detect when an app
Greeting everyone, may I ask your help for following question? I'm using following code
I am using Microsoft's XAML/HTML converter to convert HTML from a database into a

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.