par intérim J'ai actuellement la fonction simpleJava - Bit Manipulation étrange
public static void convert() {
for (int i = 0; i < byteArray.length; i++) {
byteArray[i] = (byte) (byteArray[i] & ~(1 << 0));
String s = ("0000000" + Integer.toBinaryString(0xFF & byteArray[i])).replaceAll(".*(.{8})$", "$1");
System.out.println(s);
}
}
par étapes qui un tableau d'octets et modifie chacun des bits. Ce produit les éléments suivants:
11110110
11000010
11111010
Là où ça devient étrange est cependant quand j'ajoute simple instruction if (cela ne change rien) autour des lignes dans la fonction, la sortie change complètement. Par exemple, si je change la fonction ci-dessus pour
public static void convert() {
for (int i = 0; i < byteArray.length; i++) {
if (5 > 3) {
byteArray[i] = (byte) (byteArray[i] & ~(1 << 0));
String s = ("0000000" + Integer.toBinaryString(0xFF & byteArray[i])).replaceAll(".*(.{8})$", "$1");
System.out.println(s);
}
}
}
il produit les éléments suivants à la place:
10100110
00011000
00010100
Je suis vraiment confus pour expliquer pourquoi ce qui se passe. Merci.
La classe entière juste pour la référence:
public class LIB {
static byte[] byteArray;
static ArrayList<String> bitArray = new ArrayList<String>();
static String messageToDecode = "001010110";
static char[] mtdChar = messageToDecode.toCharArray();
static ArrayList<String> FinalBitArray = new ArrayList<String>();
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
imageToBits();
convert();
}
public static void imageToBits() throws IOException {
//get image bytes
byteArray = Files.readAllBytes(new File("/Users/2020shatgiskessell/Desktop/url.jpg").toPath());
for (int i = 0; i < byteArray.length; i++) {
String s = ("0000000" + Integer.toBinaryString(0xFF & byteArray[i])).replaceAll(".*(.{8})$", "$1");
bitArray.add(s);
}
}
public static void convert() {
for (int i = 0; i < byteArray.length; i++) {
if (5 > 3) {
byteArray[i] = (byte) (byteArray[i] & ~(1 << 0));
String s = ("0000000" + Integer.toBinaryString(0xFF & byteArray[i])).replaceAll(".*(.{8})$", "$1");
System.out.println(s);
}
}
}
Vous nous avez demandé de déboguer votre code pour vous, et vous n'avez même pas fourni tout le code. Mettez-le dans un débogueur et déterminez quelle déclaration ne fait pas ce que vous attendez. Ensuite, supprimez ce post, sauf si vous avez besoin d'aide pour comprendre cette déclaration. – arcy
@arcy J'ai fourni tout le code à l'exception de la fonction principale, qui appelle simplement les fonctions. Je ne suis pas sûr de savoir pourquoi cette erreur se produit, c'est pourquoi je suis en train d'atteindre la communauté de débordement de pile. Le débogueur ne donne aucune information sur la raison pour laquelle cela se produit. –
'my_string.substring (0, my_string.length() - 2) +" 1 ";' est une façon de le faire, probablement pas le meilleur. –