Benches Uncategorized

5

How to convert string into bytearray

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

I try


+ (void) stringToBytes:(UInt8 **)bytes string:(NSString *)string{
int length = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];

NSRange range;
range.location = 0;
range.length = length;

[string getBytes:*bytes maxLength:length usedLength:nil encoding:NSUTF8StringEncoding options:NSStringEncodingConversionAllowLossy range:range remainingRange:nil];
}

I work, but
sizeof(bytes) return 4. Why it does not return
length ?

I try to use
malloc():


...
*bytes = malloc(length);
[string getBytes:*bytes maxLength:length usedLength:nil encoding:NSUTF8StringEncoding options:NSStringEncodingConversionAllowLossy range:range remainingRange:nil];

But
sizeof(bytes) return 4.

Message was edited by: abuharsky

5 replies

Sign in or join Beachball Help to reply.

  1. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Bytes is a just pointer. You will have to use the "usedLength" parameter to get the size because the result is not null terminated.

  2. 0

    abuharsky

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Does C contain function for automatic calculation size or I need do it personally?

    When I can use "sizeof" to calculate amount of data in bytes, or it only for strings(because they ended by '')?

  3. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    abuharsky wrote:

    Does C contain function for automatic calculation size

    Yes. The is what sizeof() is. But it only works on POD (Plain Old Data). It will tell you the size of short, int, pointer, any struct, but it won't look into a structure and tell you how much memory is used in a particular buffer.

    or I need do it personally?

    There is nothing you can do in this case.

    When I can use "sizeof" to calculate amount of data in bytes, or it only for strings(because they ended by '')?

    If this were a string (which it isn't) you could use strlen() to get its size. The NSString getBytes:... message does not return a string. It fills a previously allocated buffer. How much data it puts there is returned in "usedLength". That is the only way for you to tell how many bytes were written into your buffer.

  4. 0

    abuharsky

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Maybe add
    usedLength:


    + (void) stringToBytes:(UInt8 **)bytes string:(NSString *)string usedLength:(int *)len {

    ?

  5. 0

    abuharsky

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I try:


    UInt8 array[10];
    UInt8 *pointerToArray = array;

    NSLog(@"sizeof(array) = %d, sizeof(pointerToArray) = %d", sizeof(array), sizeof(pointerToArray));

    and get


    sizeof(array) = 10, sizeof(pointerToArray) = 4

    I see the solution to get true size of array in bytes in the correct init of arrays:


    UInt8 array[length];