Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Transform Array<UShort> to Array <UByte>
- * Antonio Villanueva Segura
- */
- fun main() {
- var str="abcdefg" //Test string array
- var arrayUShort=toUShortArray(str) //String to UShort array
- printArrayUShort (arrayUShort)//Show UShort array
- var arrayUByte=toUByteArray(arrayUShort)//UShort array to UByte array
- printArray(arrayUByte)//Show UByte array
- }
- //String -> Char -> to UShort
- fun toUShortArray(s:String):Array <UShort>{
- var arrayUshort = Array<UShort> (s.length){0U} //Same size like string array
- for (i in s.indices){//loop through the string array
- //arrayUshort[i]=s[i].toShort().toUShort()
- arrayUshort[i]=s[i].code.toUShort()
- }
- return arrayUshort
- }
- //Transform UShort array -> UByte array
- fun toUByteArray (a:Array<UShort>):Array<UByte>{
- var arrayUbyte=Array<UByte> (a.size * 2){0U}//El doble
- for (i in a.indices ){
- arrayUbyte[i*2+0]=a[i].toInt().shr(8).toUByte()
- arrayUbyte[i*2+1]=a[i].toUByte()
- }
- return arrayUbyte
- }
- //Show UByte array
- fun printArray(a:Array<UByte>){
- println ("Type = ${(a[0]::class.simpleName)}" )
- for (e in a){
- println ("0x%02x".format (e.toInt()))
- }
- }
- //Show UByte array
- fun printArrayUShort(a:Array<UShort>){
- println ("Type = ${(a[0]::class.simpleName)}" )
- for (e in a){
- println ("0x%04x".format (e.toInt()))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement