Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Function to transform Array<UShort 16bits > into Array <UByte 8bits>
- * with generic show function Array ..showArray (array:Array<*>)
- * Antonio Villanueva Segura
- */
- fun main() {
- //I declare an example Array of unsigned short, Array<UShort>
- var ushortArray=arrayOf<UShort>(0x0102U,0x0304U,0x0506U,0x0708U,0x090AU)
- var ubyteArray =toUByteArray(ushortArray) //Conversion UShortArray to UByteArray
- }
- /*Function to show an array in hexadecimal */
- fun showArray(array: Array<*>){
- println ("${array[0]!!::class.simpleName}")//Analysis of the type of variable
- for (indice in array.indices){//Go through the Array Indices
- print ("["+indice+"]=")
- println ( "0x%x".format (((array[indice] ).toString()).toInt()))
- }
- }
- /* Function to transform Array<UShort> in Array<UByte> ..Receive an UShortArray retuns UByteArray*/
- fun toUByteArray(ushortArray:Array<UShort>):Array<UByte>{
- //Initialize a UByte(8) Array to the size of UShort(16) Array * 2
- var ubyteArray=Array<UByte>(ushortArray.size * 2){0U}
- var index=0
- for (elem in ushortArray){//Walk through the UShort array
- //recovers elem 16bits for write two bytes in Array<UByte>
- ubyteArray[index++]= (elem.toInt().shr(8)).toUByte()
- ubyteArray[index++]= elem.toUByte()
- }
- showArray(ushortArray) //Debug show Array<UShort>
- showArray(ubyteArray) //Debug show Array<UByte>
- return ubyteArray
- }
Add Comment
Please, Sign In to add comment