-2

I have 2 NSData elements that have 2 register values from a sensor. HighData and LowData. In arduino world I did

Value = (HighData << 8) + LowData;

How should I implement this with Objective-C.

Currently I have HighData, LowData of NSData type holding a byte whose values are in decimals.

I want to get the integer value, please help.

Veeru
  • 23
  • 7
  • exactly the same way, since it's just C in both cases. – The Paramagnetic Croissant Aug 28 '15 at 18:31
  • @TheParamagneticCroissant No, that's not the way he should be doing it with `NSData`. – Sergey Kalinichenko Aug 28 '15 at 18:33
  • `HighData` and `LowData` are eight bits each, right? – Sergey Kalinichenko Aug 28 '15 at 18:34
  • @dasblinkenlight no, but that's not the "same way". but what he posted is nonsense anyway (it doesn't compile). he can just shift and add once he correctly got the values out of the data objects. – The Paramagnetic Croissant Aug 28 '15 at 18:35
  • 3
    The tem bitwise means doing an operation on a bit. I find it extraordinary that you are even imagining that you could perform a bitwise operation on a class. Do you know a class is and a bit and the difference between them? Do you know why you are even using an NSData? What are you trying to do exactly? What you have posted and are asking is complete nonsense, I am not saying this to be rude. – Gruntcakes Aug 28 '15 at 18:35
  • I am sorry for being very vague in my question. My xcode project deals with a sensor and I get data as NSData element when I read a register. It is 8 bit data. I used the same sensor in Arduino and I didn't put the complete source code, so it obviously won't compile. – Veeru Aug 28 '15 at 19:01
  • HighData, LowData in my xcode are defined as NSData, value was read from an object which is NSData. I am very very sorry for not putting it out correctly, I am new to Xcode. How to convert NSData to integer and then do bitwise OR for MSB and LSB in Objective C. Sorry if I offended anyone. – Veeru Aug 28 '15 at 19:12
  • try this for converting nsdata into int http://stackoverflow.com/questions/7332641/converting-nsdata-to-int – Teja Nandamuri Aug 28 '15 at 19:26
  • 2
    This seems like it could be answered, but not without making a bunch of assumptions about what exactly the situation is. Please [edit] to be more explicit about how the information is stored and what you want to do with it. `NSData HighData = 11;` is not real ObjC code. I suspect it just means "I have an `NSData` instance holding a byte whose decimal value is 11", but you really need to clarify. – jscs Aug 28 '15 at 19:39

1 Answers1

0

It sounds like you need something like this:

NSData * highData = <something>;
NSData * lowData = <something else>;

if (highData.length != 1 || lowData.length != 1) {
    NSLog(@"Something went wrong!");
    return;
}

uint8_t * highBytes = (uint8_t *)highData.bytes;
uint8_t * lowBytes = (uint8_t *)lowData.bytes;

unsigned value = (((unsigned)highBytes[0]) << 8) + ((unsigned)lowBytes[0]);
Ewan Mellor
  • 6,747
  • 1
  • 24
  • 39