ASCII Hexadecimal to ASCII Decimal with NSData

  • 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
  • Anything with the high bit set is no longer ASCII.
    You could try loading the file as MacRoman or Latin1, any simple
    character set will do, but with ASCII you will get a failure (NULL
    result) as soon as a byte value above 127 is found.
    I am not sure that you are going to get much mileage out of NSString;
    you might just go straight to an NSData blob in memory and then work
    with the data directly.

    On Nov 9, 2007, at 4:22 PM, Simon Setterstrom wrote:

    >
    > 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
  • > 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.

    I think you may be confused about C? A byte is a byte is a byte, its value
    might get displayed as hex or as decimal, but it's just a binary number.

    Could you be more explicit about what you're trying to do? Exactly what
    format is the file in, and what exactly do you need to get out of the file?

    --
    Scott Ribe
    <scott_ribe...>
    http://www.killerbytes.com/
    (303) 722-0567 voice
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today