1

I have a somewhat long string (around 2.000 characters, but could be more) which I need to break into parts of up to 255 byte chunks. I realize I have to iterate over the whole string (saw it here), but it's not clear how.

How can I do this?

Juan Catalan
  • 2,299
  • 1
  • 17
  • 23
André Fratelli
  • 5,920
  • 7
  • 46
  • 87
  • A Swift string has Unicode, UTF-16, and UTF-8 representations. What do you mean by "byte chunks" in this context? – Martin R Jul 10 '14 at 15:15
  • 1
    I have an Objective-C answer, which can either by ported by you to Swift or used seamlessly within your code. Would you like me to post it? – duci9y Jul 10 '14 at 15:17
  • @MartinR, a byte is a byte, it does not depend on the representation used for the string. – André Fratelli Jul 10 '14 at 15:19
  • @duci9y, yes, please do! – André Fratelli Jul 10 '14 at 15:19
  • Are you just wanting to grab 255 characters? (1 byte = 1 character) – Oxcug Jul 10 '14 at 15:19
  • 2
    @theMonster: 1 byte != 1 character. – Martin R Jul 10 '14 at 15:20
  • @theMonster, no, like Martin R said, I want bytes – André Fratelli Jul 10 '14 at 15:21
  • @MartinR 4 bytes == 1 character in C and in C++ 1 character == 1 byte. http://stackoverflow.com/questions/2172943/size-of-character-a-in-c-c – Oxcug Jul 10 '14 at 15:22
  • @AndréFratelli: A (Swift) string has (Unicode) characters, not bytes. If you are looking for the UTF-8 representation (which is a sequence of bytes) then you cannot always obtain chunks of exactly 255 bytes, because 1 characters may be represented by 1 to 4 bytes. (That's the reason why I was asking). – Martin R Jul 10 '14 at 15:22
  • @theMonster: Your link is about the size of `char` in C/C++. I was talking about `String` and `Character` as used in the Swift programming language, compare https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-XID_367. – Martin R Jul 10 '14 at 15:25
  • @MartinR I know that, maybe I should rephrase the question to "up to 255 bytes", that's what I'm really looking for – André Fratelli Jul 10 '14 at 15:36

2 Answers2

3

IF you look at The Swift Programming Guide from page 128 you can see how to iterate over a string. The bytes that you get from the string do depend on the encoding that you ask the string to give to you. You probably want UTF-8 (an 8 bit encoding) which is described on page 130:

let dogString = "Dog!"

for codeUnit in dogString {
    print("\(codeUnit) ")
}”

“print("\n")
// 68 111 103 33 240 159 144 182”

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/gb/jEUH0.l

Note 1 code unit may not contain the whole of a character (as some characters are represented by multiple code units, in this case the 'dogface' has a 4 byte encoding, whereas the ascii characters require only one byte each).

James Snook
  • 4,063
  • 2
  • 18
  • 30
1
    NSString *str = [NSString stringWithContentsOfFile:[@"~/Desktop/bytes" stringByExpandingTildeInPath] encoding:NSUTF8StringEncoding error:nil];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

    NSMutableArray *chunks = [NSMutableArray array];

    NSUInteger readPointer = 0;
    NSUInteger distanceToEndOfData;

    while (readPointer + 255 < data.length) {
        distanceToEndOfData = data.length - readPointer;
        [chunks addObject:[data subdataWithRange:NSMakeRange(readPointer, 255)]];
        readPointer += 255;
    }

    distanceToEndOfData = data.length - readPointer;

    if (distanceToEndOfData > 0) [chunks addObject:[data subdataWithRange:NSMakeRange(readPointer, distanceToEndOfData)]];

    NSLog(@"%@", chunks);

Let me know if you want an explanation of the code.

duci9y
  • 4,128
  • 3
  • 26
  • 42