The Artima Developer Community
Sponsored Link

Java Answers Forum
convert an int into a byte array & vice versa

4 replies on 1 page. Most recent reply: Mar 24, 2004 3:06 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Courtenay

Posts: 23
Nickname: courtz
Registered: Apr, 2003

convert an int into a byte array & vice versa Posted: Mar 23, 2004 4:29 AM
Reply to this message Reply
Advertisement
Can anyone help? i need a method to convert an int into a byte array and vice versa? thanks!


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: convert an int into a byte array & vice versa Posted: Mar 23, 2004 8:58 AM
Reply to this message Reply
byte array?

I will do it like this

String strn = 15+""; //Convert number to String
 
byte [] bArr = strn.getBytes();//convert String to byte array
System.out.println("byteArray::"+bArr);
//now say if u have byteArray and want to conver it to int
 
//Make string object from array
String newStr = new String(bArr);
//convert it to int
int convertedInt = Integer.parseInt(newStr);
System.out.println("convertedInt::"+convertedInt);

Courtenay

Posts: 23
Nickname: courtz
Registered: Apr, 2003

Re: convert an int into a byte array & vice versa Posted: Mar 23, 2004 10:58 AM
Reply to this message Reply
this isnt right - i need an int to be converted to a byte array, not a string

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: convert an int into a byte array & vice versa Posted: Mar 23, 2004 8:52 PM
Reply to this message Reply
that is what i m saying first convert int to String and then convert that String to byte Array

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: convert an int into a byte array & vice versa Posted: Mar 24, 2004 3:06 AM
Reply to this message Reply
A good guide to to to convert from one java data type to another can be found at http://mindprod.com/converter.html.

For going int -> String, the following alternatives are suggested:
// to String g from int i
/* best for readabilty */ g = Integer.toString(i);
/* best for maintainablity */ g = String.valueOf(i);
/* or */ g = Integer.toString(i, 7 /* radix */);
/* or */ g = Integer.toBinaryString(i);
/* or */ g = Integer.toOctalString(i);
/* or */ g = Integer.toHexString(i);
/* or kludgy and possibly slow */ g = "" + i;
Tacking .getBytes() as suggested above onto the end of any of the alternatives above will give you the desired char array. Thus:
// to byte[] ba from int i
ba = Integer.toString(i).getBytes();
ba = String.valueOf(i).getBytes();
ba = Integer.toString(i, 7).getBytes();
ba = Integer.toBinaryString(i).getBytes();
ba = Integer.toOctalString(i).getBytes();
ba = Integer.toHexString(i).getBytes();
ba = ("" + i).getBytes();
(I think there should be enough information above to help work out the (slightly more complex) reverse conversion for yourself.)

Vince.

Flat View: This topic has 4 replies on 1 page
Topic: How to "setArray" into the PL/SQL stored procedure? Previous Topic   Next Topic Topic: Random Numbers

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use