How to copy integer into a character array?
1 posts in topic
Flat View  Flat View

Posted By:   Abhishek_Tandon
Posted On:   Thursday, January 31, 2002 09:44 PM


I have a string(hexadecimal) from which I am reading 4 characters at a time and convert them into integer to perform some XOR operation with some integer value.The result is a 10 digit integer. Now I want to store this integer back into the character array and that too in 4 indices only.

In C/C++ by using memcpy we can achieved this but what about JAVA.How can we do this in JAVA.

Please help me out.


Re: How to copy integer into a character array?

Posted By:   Krzysztof_Raciniewski  
Posted On:   Friday, February 1, 2002 01:16 AM

You can use ByteArrayOutputStream. The code below shows how to copy an Integer into arrays of bytes:

int myInt = 1234567890;
ByteArrayOutputStream bros = new ByteArrayOutputStream ();
ObjectOutputStream ous = new ObjectOutputStream (bros);
ous.writeInt (myInt);
byte[] bytes = bros.toByteArray ();
System.out.println ("Integer " + myInt + " as four bytes:");
for (int i = 0; i < bytes.length; i++) {
System.out.println ("byte " + i + " " + bytes [i]);
About | Sitemap | Contact