Converting integers from array to integer variables
-
I have an array of numbers say array[0]=-10, and array[1]=23. Furthermore, lets say that they represent a most significant bit, and a least significant bit, so in reality the number is -1023. How do I combine both elements of the array into a signed integer that I can store?? Regards Mike Zolna
-
I have an array of numbers say array[0]=-10, and array[1]=23. Furthermore, lets say that they represent a most significant bit, and a least significant bit, so in reality the number is -1023. How do I combine both elements of the array into a signed integer that I can store?? Regards Mike Zolna
Using your simple example,
int number = array[0]*100 + array[1];
--Mike-- http://home.inreach.com/mdunn/ Time is an illusion; lunchtime doubly so. -
Using your simple example,
int number = array[0]*100 + array[1];
--Mike-- http://home.inreach.com/mdunn/ Time is an illusion; lunchtime doubly so.Thanks, but what if the number is 102 and not 1023, that wont work. I dont know that magnitude of the numbers until they are joined together Mike
-
Thanks, but what if the number is 102 and not 1023, that wont work. I dont know that magnitude of the numbers until they are joined together Mike
I don't know what your program is like, so it's hard to say. But if you don't know what the final result should look like, then you won't be able to calculate the final result. ;) You'll need some sort of separate indicator to tell you how the result is split up among the array elements. --Mike-- http://home.inreach.com/mdunn/ Time is an illusion; lunchtime doubly so.
-
I have an array of numbers say array[0]=-10, and array[1]=23. Furthermore, lets say that they represent a most significant bit, and a least significant bit, so in reality the number is -1023. How do I combine both elements of the array into a signed integer that I can store?? Regards Mike Zolna
If your numbers are in range of -signed char- it`s very easy to combine these numbers to an integer: int result = ( array[0] << 8 ) + array[1]; This command shifts the number stored in array[0] 8 Bit up (left) and adds the number stored in array[1]; Hope that helps Greatings Mario /// -------------------- www.klangwerker.de rocknix@lycos.de --------------------
-
I have an array of numbers say array[0]=-10, and array[1]=23. Furthermore, lets say that they represent a most significant bit, and a least significant bit, so in reality the number is -1023. How do I combine both elements of the array into a signed integer that I can store?? Regards Mike Zolna
If your numbers are in range of -signed char- it`s very easy to combine these numbers to an integer: int result = ( array[0] << 8 ) + array[1]; This command shifts the number stored in array[0] 8 Bit up (left) and adds the number stored in array[1]. Shifting a byte by 8 is equal to a multiplikation with 0xff - but shifting is much faster I think. Hope that helps Greatings Mario /// -------------------- www.klangwerker.de rocknix@lycos.de --------------------
-
Thanks, but what if the number is 102 and not 1023, that wont work. I dont know that magnitude of the numbers until they are joined together Mike
Well, this is not efficient, but shoudl serve as a starting point for you: long lResult = 0; int nMult; for (int x = 0; x< nNum; x++) { nMult = 1; while (nMult <= nArray[x]) nMult *= 10; lResult *= nMult; lResult += nArray[x]; } Note though that if your array has any significant size then you will overflow the integer you're trying ot store it into.
-
I don't know what your program is like, so it's hard to say. But if you don't know what the final result should look like, then you won't be able to calculate the final result. ;) You'll need some sort of separate indicator to tell you how the result is split up among the array elements. --Mike-- http://home.inreach.com/mdunn/ Time is an illusion; lunchtime doubly so.
Actually, The elements of the array are from 0-255, or 00 to FF in hex. I need to combine both bytes and turn them into a signed character for processing. Regards Mike