Hello how to convert String into int array. I need it for some enrypting. Just a String converted into 32 bit values.
I tried that but it did not work. Maybe converting String into BigInteger then this into raw String then into int array would work?
String s = "Alice";
int[] tab = s.getBytes();
如果要将String转换为int数组,请阅读Joel关于字符串编码的文章 ,它并不像您想象的那么明显。
I think something like this would work for ya: Found it here: http://pro-programmers.blogspot.com/2011/05/java-byte-array-into-int-array.html
public int[] toIntArray(byte[] barr) {
//Pad the size to multiple of 4
int size = (barr.length / 4) + ((barr.length % 4 == 0) ? 0 : 1);
ByteBuffer bb = ByteBuffer.allocate(size *4);
bb.put(barr);
//Java uses Big Endian. Network program uses Little Endian.
bb.order(ByteOrder.BIG_ENDIAN);
bb.rewind();
IntBuffer ib = bb.asIntBuffer();
int [] result = new int [size];
ib.get(result);
return result;
}
To call it:
String s = "Alice";
int[] tab = toIntArray(s.getBytes());
Try :
String s = "1234";
int[] intArray = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
intArray[i] = Character.digit(s.charAt(i), 10);
}
change to byte : byte[] tab = s.getBytes();
final String s = "54321";
final byte[] b = s.getBytes();
for (final byte element : b) {
System.out.print(element+" ");
}
Output :
53 52 51 50 49
Edit
(int)
cast is removed by Eclipse in System.out.print((int) element+" ");
Unless you want to cast int myInteger = (int) tab[n]
, you have to copy byte[]
in a new int[]
String s = "Alice";
byte[] derp = s.getBytes();
int[] tab = new int[derp.length];
for (int i=0; i< derp.length; i++)
tab[i] = (int)derp[i];
You can't convert a string to bytes without an encoding.
You need to use this already existing method:
public static final byte[] getBytesUtf8( String string )
{
if ( string == null )
{
return new byte[0];
}
try
{
return string.getBytes( "UTF-8" );
}
catch ( UnsupportedEncodingException uee )
{
return new byte[]
{};
}
}
}
Then change it to int array like this: byte[] bAlice
int[] iAlice = new int[bAlice.length];
for (int index = 0; index < bAlice.length; ++index) {
iAlice [index] = (int)bAlice[index];
}
The rules of widening primitive conversion does not apply to arrays of primitive types. For instance, it is valid to assign a byte to an integer in Java:
byte b = 10;
int i = b; //b is automatically promoted (widened) to int
However, primitive arrays do not behave the same way, and therefore, you cannot assume that an array of byte[]
is going to be automatically promoted to an array of int[]
.
However, you could force the promotion of every item in your byte array manually:
String text = "Alice";
byte[] source = text.getBytes();
int[] destiny = new int[source.length];
for(int i = 0; i < source.length; i++){
destiny[i] = source[i]; //automatically promotes byte to int.
}
For me, this would be the simplest approach.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.