Y at-il un moyen pour Java de convertir les int en symboles ascii?Java - Change int en ascii
Répondre
Voulez-vous convertir int
s à char
s ?:
int yourInt = 33;
char ch = (char) yourInt;
System.out.println(yourInt);
System.out.println(ch);
// Output:
// 33
// !
Ou voulez-vous convertir int
s à String
s?
int yourInt = 33;
String str = String.valueOf(yourInt);
Ou qu'est-ce que vous voulez dire?
Si vous convertissez d'abord l'int en un caractère, vous aurez votre code ascii.
Par exemple:
int iAsciiValue = 9; // Currently just the number 9, but we want Tab character
// Put the tab character into a string
String strAsciiTab = Character.toString((char) iAsciiValue);
En fait, dans la dernière réponse chaîne strAsciiTab = Character.toString ((char) iAsciiValue); la partie essentielle est (char) iAsciiValue qui fait le travail (Character.toString inutile)
Signification la première réponse était correcte en fait char ch = (char) yourInt;
si yourint = 49 (ou 0x31), ch sera « 1 »
Il y a plusieurs façons de convertir un int en ASCII (en fonction de vos besoins) mais voici un moyen de convertir chaque octet entier à un caractère ASCII:
private static String toASCII(int value) {
int length = 4;
StringBuilder builder = new StringBuilder(length);
for (int i = length - 1; i >= 0; i--) {
builder.append((char) ((value >> (8 * i)) & 0xFF));
}
return builder.toString();
}
par exemple, le texte ASCII pour "TEST" peut être représentée par le tableau d'octets:
byte[] test = new byte[] { (byte) 0x54, (byte) 0x45, (byte) 0x53, (byte) 0x54 };
Ensuite, vous pouvez faire ce qui suit:
int value = ByteBuffer.wrap(test).getInt(); // 1413829460
System.out.println(toASCII(value)); // outputs "TEST"
... donc cela convertit essentiellement les 4 octets dans un entier de 32 bits en 4 caractères ASCII séparés (un caractère par octet).
En Java, vous voulez vraiment utiliser Integer.toString pour convertir un entier en sa valeur de chaîne correspondante. Si vous faites affaire avec seulement les chiffres 0-9, vous pouvez utiliser quelque chose comme ceci:
private static final char[] DIGITS =
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
private static char getDigit(int digitValue) {
assertInRange(digitValue, 0, 9);
return DIGITS[digitValue];
}
Ou, ce qui revient:
private static int ASCII_ZERO = 0x30;
private static char getDigit(int digitValue) {
assertInRange(digitValue, 0, 9);
return ((char) (digitValue + ASCII_ZERO));
}
Vous pouvez convertir un nombre en ASCII en java. exemple convertissant un nombre 1 (base est 10) en ASCII.
char k = Character.forDigit(1, 10);
System.out.println("Character: " + k);
System.out.println("Character: " + ((int) k));
sortie:
Character: 1
Character: 49
- 1. conversion d'un ascii en int
- 2. Binaire Int tableau à ASCII équivalent en Java
- 3. Microsoft.VisualBasic.FileIO.TextFieldParser change ± Ascii 241 à? Ascii 63
- 4. comment convertir ascii en unsigned int
- 5. convertir binaire en ascii, chaîne de rotation en ascii - java
- 6. Dessin ASCII art en utilisant java
- 7. taille du tableau int change
- 8. Valeurs ASCII dans JAVA
- 9. Ascii à la fonction Int dans Groovy
- 10. texte Ascii java
- 11. flux ASCII de filtre en Java
- 12. java convertir int en court
- 13. Conversion int [] en octets [] Java
- 14. Convertir int en double Java
- 15. Chaîne à int en Java
- 16. Java convert String [] en int []
- 17. convertir de int à float en C change la valeur
- 18. Conversion de byte en int en java
- 19. Conversion de String en int en Java
- 20. Débutants Java Question (int, float)
- 21. Sérialisation Java. Champ change valeur
- 22. Comment obtenir la valeur int ASCII d'un caractère dans Cocoa?
- 23. Java int concurrency ++ int équivalent à AtomicInteger.incrementAndGet()?
- 24. Aide avec NSString de int à NSString de caractères ASCII?
- 25. Convertir une chaîne de caractères hexadécimaux en ASCII en Java
- 26. Java - convertir "this [int x, int y]"
- 27. Convertir chaîne de chiffres ASCII à int dans MIPS/Assembleur
- 28. Java: Change String [] [] Dynamiquement
- 29. Java - Change BufferedImage File
- 30. Conversion de chaîne ASCII en ASCII