ASCII Hexadecimal to ASCII Decimal with NSData
ASCII Hexadecimal to ASCII Decimal with NSData
- Subject: ASCII Hexadecimal to ASCII Decimal with NSData
- From: "Simon Setterstrom" <email@hidden>
- Date: Fri, 9 Nov 2007 18:22:22 -0600
I am trying to read a proprietary file format in and parse it. I've read
up (soaking up as much as a newbie can) on NSString and NSData and their
methods allowing you to initialize them to the contents of a file. About the
encoding of the file, some values are ASCII, but all are 8 bits guaranteed
(some have the largest bit flipped to signify state change).
Examples of what I've tried respectively:
NSString *path = @"/Users/simon/Desktop/PAGE _001.DHW";
NSString *myFile = [[NSString alloc]
initWithContentsOfFile:path
encoding:NSASCIIStringEncoding
error:NULL];
and
NSData *myData = [[NSData alloc] initWithContentsOfFile:path
options:NSASCIIStringEncoding error:NULL];
I have read that I am better off working with the straight hexadecimal
ASCII NSData provides me and I would be fine with that, if I knew how to
convert that into decimal ASCII as I would be able to do everything I need
then, but I do not know how.
The reason I (probably foolishly) am toying with NSString is that there is
a Perl program that does what I need to with a similar approach (reads a
character and uses unpack("C",$aChar)); I thought if I could figure out how
to change the encoding / reformat the character to display in binary
(possibly putting the character into NSNumber and using stringValue), I
would have my pretty ASCII decimal number and be set.
NSNumber attempt:
NSString *output = [[NSString alloc] initWithString:@""];
NSString *chunk = [file substringWithRange:NSMakeRange(loopC,1)];
NSNumber *num = [[NSNumber alloc] initWithUnsignedChar: chunk];
output = [output stringByAppendingString: [num stringValue]];
Should I steer myself back towards using NSData and try to parse each byte
(getBytes :length ??) and convert the hexadecimal ASCII into decimal ASCII
with some method or formatter?
Or is there a way with NSString to change the format of a character that
does not fit into a predefined encoding? Subsequently I tried chunk =
[NSString stringWithFormat:@"%c", chunk]; with no luck.
I hope I have been clear with my thoughts; please be patient with me--I
really am trying not to be burdensome. I know that many of these concepts
and libraries do not make much sense to me yet so I am hoping for your
guidance.
Any feedback or direction you could give me would be amazing. Thank you for
donating some of your time to my problem.
Simon
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden