AntonioVillanueva

Transform Array<UShort> to Array <UByte> KOTLIN

Nov 30th, 2021
800
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Transform  Array<UShort> to Array <UByte>
  3.  * Antonio Villanueva Segura
  4.  */
  5.  
  6. fun main() {
  7.     var ushortArray  = arrayOf<UShort> ( 0x1234U , 0x5678U , 0x9ABCU , 0xDEF0U )
  8.     println ( printUByteArray (toArrayUByte(ushortArray)) )        
  9. }
  10.  
  11. //Transform Array<UShort> TO  Array<UByte>
  12. fun toArrayUByte(a:Array<UShort>):Array<UByte>{
  13.    
  14.     var b=Array<UByte> (a.size *2){0U} //The UByte array is twice the UShort array
  15.     for (i in a.indices ){        
  16.         b[i*2]=a[i].toInt().shr(8).toUByte() //H Byte
  17.         b[i*2 + 1]=a[i].toUByte() //L Byte                
  18.     }
  19.    
  20.     return b //Returns Array<UByte>
  21. }
  22.  
  23.  
  24. //Print function to watch UByte Array
  25. fun printUByteArray(a:Array<UByte>){
  26.     for (elem  in a ){
  27.         println (elem.toString(16)) //Conversion HEX 0x
  28.     }
  29. }
RAW Paste Data