how to convert a byte array values in to integer?

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

how to convert a byte array values in to integer?

Postby itskrishnaraj » Mon Oct 06, 2008 1:21 pm

in my code i jus using

Code: Select all
int k=move[0]&0xFF


its working in some cases like ifthe byte value is below 250 or smthing like dat ...but it fails sometimes there is another method

Code: Select all
public static int readIntFromByteArray(byte[] byteArray, int index){
      return
      ((byteArray[index++]&0xFF)<<24) |
      ((byteArray[index++]&0xFF)<<16) |
      ((byteArray[index++]&0xFF)<<8)  |
      ( byteArray[index]&0xFF);      
   }

but its not returning any value i guess

help with any solution
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: how to convert a byte array values in to integer?

Postby DevelopmentTeam » Tue Oct 07, 2008 4:54 am

Code: Select all
   
public static int readIntFromByteArray(byte[] byteArray, int index){
          return (int)byteArray[index];     
       }


What is your problem in using typecasting. I think this should work if I understand your post correctly
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India


Return to J2ME Games

Who is online

Users browsing this forum: No registered users and 1 guest

cron