如何将字符串转换为位,然后转换为整数数组 - java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11367611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert string into bits and then into int array - java
提问by Yoda
How to convert string into bits(not bytes) or array of bits in Java(i will do some operations later) and how to convert into array of ints(every 32 bits turn into the int and then put it into the array? I have never done this kind of conversion in Java.
如何在 Java 中将字符串转换为位(不是字节)或位数组(我稍后会做一些操作)以及如何转换为整数数组(每 32 位转换为整数,然后将其放入数组?我有从未在 Java 中进行过这种转换。
String->array of bits->(some operations I'll handle them)->array of ints
回答by Louis Wasserman
ByteBuffer bytes = ByteBuffer.wrap(string.getBytes(charset));
// you must specify a charset
IntBuffer ints = bytes.asIntBuffer();
int numInts = ints.remaining();
int[] result = new int[numInts];
ints.get(result);
回答by Yoda
THIS IS THE ANSWER
这就是答案
String s = "foo";
byte[] bytes = s.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes)
{
int val = b;
for (int i = 0; i < 8; i++)
{
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
// binary.append(' ');
}
System.out.println("'" + s + "' to binary: " + binary);
回答by Matzi
回答by Barranka
Well, maybe you can skip the String to bits conversion and convert directly to an array of ints (if what you want is the UNICODE value of each character), using s.toCharArray()
where s
is a String
variable.
好吧,也许您可以跳过字符串到位的转换并直接转换为整数数组(如果您想要的是每个字符的 UNICODE 值),使用s.toCharArray()
wheres
是一个String
变量。
回答by codeDEXTER
This will convert "abc" to byte and then the code will print "abc" in respective ASCII code (ie. 97 98 99).
这会将“abc”转换为字节,然后代码将以相应的 ASCII 代码(即 97 98 99)打印“abc”。
byte a[]=new byte[160];
String s="abc";
a=s.getBytes();
for(int i=0;i<s.length();i++)
{
System.out.print(a[i]+" ");
}
回答by MikhailSP
May be so (I have no compiler in my current computer and don't test if it work, but it can help you a bit):
可能是这样(我当前的计算机中没有编译器,不测试它是否有效,但它可以帮助您):
String st="this is a string";
byte[] bytes=st.getBytes();
List<Integer> ints=new ArrayList<Integer>();
ints.addAll(bytes);
If compiler fails in
如果编译器失败
ints.addAll(bytes);
you can replace it with
你可以用
for (int i=0;i<bytes.length();i++){
ints.add(bytes[i]);
}
and if you want to get exactly array:
如果你想得到准确的数组:
ints.toArray();
回答by Ramanqul Buzaubak
Note that string is a sequence of chars, and in Java each char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive). In order to get char integer value do this:
请注意,字符串是一个字符序列,在 Java 中,每个字符数据类型都是一个 16 位的 Unicode 字符。它的最小值为 '\u0000'(或 0),最大值为 '\uffff'(或 65,535)。为了获得 char 整数值,请执行以下操作:
String str="test";
String tmp="";
int result[]=new int[str.length()/2+str.length()%2];
int count=0;
for(char c:str.toCharArray()) {
tmp+=Integer.toBinaryString((int)c);
if(tmp.length()==14) {
result[count++]=Integer.valueOf(tmp,2);
//System.out.println(tmp+":"+result[count-1]);
tmp="";
}
}
for(int i:result) {
System.out.print(i+" ");
}