1

I want to create a Byte Array like this one;

   Byte UUID[] = {0xEB, 0xEF, 0xD0, 0x83, 0x70, 0xA2, 0x47, 0xC8, 0x98, 0x37, 0xE7, 0xB5, 0x63, 0x4D, 0xF5, 0x24};

But the problem here I am facing is, I need to fill all the elements in the above array programatically from a NSMutableArray that holds the values as below;

(
    0xEB,
    0xEF,
    0xD0,
    0x83,
    0x70,
    0xA2,
    0x47,
    0xC8,
    0x98,
    0x37,
    0xE7,
    0xB5,
    0x63,
    0x4D,
    0xF5,
    0x24
)

I have tried with the integer values of each index but it is showing '/0' in the Byte Array. If anyone have any information regarding this please share.

Thanks

8
  • Where do you get this array of numbers? Commented Apr 21, 2014 at 8:43
  • Share your current code to give some context Commented Apr 21, 2014 at 8:46
  • i am creating this array from a string. Now i want to place each value of this array into the byte array. Commented Apr 21, 2014 at 8:46
  • Suppose, This is my byte array Byte UUID[16] = {}; Commented Apr 21, 2014 at 8:48
  • Now i want to add the below sixteen values into it, from this array ( 0xEB, 0xEF, 0xD0, 0x83, 0x70, 0xA2, 0x47, 0xC8, 0x98, 0x37, 0xE7, 0xB5, 0x63, 0x4D, 0xF5, 0x24 ) Commented Apr 21, 2014 at 8:50

1 Answer 1

1

Assuming that you have an array of strings "0xEB", "0xEF", ..., the following should work:

NSArray *array = @[@"0xEB", @"0xEF", @"0xD0", @"0x83", @"0x70", @"0xA2", @"0x47", @"0xC8", @"0x98", @"0x37", @"0xE7", @"0xB5", @"0x63", @"0x4D", @"0xF5", @"0x24"];
Byte UUID[16];
for (int i = 0; i < 16; i++) {
    UUID[i] = strtoul([array[i] UTF8String], NULL, 16);
}

This works even if the strings do not have the "0x" prefix:

NSArray *array = @[@"EB", @"EF", ...]

because strtoul(string, ..., 16) reads a string with or without "0x" prefix in base 16, and converts it to an integer.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer, but i am getting this; (Byte [16]) UUID = { [0] = '\xeb' [1] = '\xef' [2] = '\xd0' [3] = '\x83' [4] = 'p' [5] = '\xa2' [6] = 'G' [7] = '\xc8' [8] = '\x98' [9] = '7' [10] = '\xe7' [11] = '\xb5' [12] = 'c' [13] = 'M' [14] = '\xf5' [15] = '$' }
@MohitManhas: Yes, and that is correct. Byte is the same as unsigned char, and that is how the debugger prints the values. You get exactly the same result as with Byte UUID[] = {0xEB, 0xEF, 0xD0, 0x83, ...};. For example '\xeb' is a char with the code 0xEB, and '7' is the character with the code 0x37.
@MohitManhas: You are welcome! - Please note that you can "accept" the answer if it helped by clicking on the check mark. That marks the problem as closed and gives some reputation points to you and to the author of the answer. (I am mentioning this because you have never accepted an answer so far.)
i know that, i will accept it once the problem is resolved at my end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.