English 中文(简体)
Java - 变化
原标题:Java - Change int to ascii
  • 时间:2011-03-16 17:10:37
  •  标签:
  • java
  • int

是否有办法将斜体转换成像标志?

问题回答

您是否希望将<代码>ints改为chars?

int yourInt = 33;
char ch = (char) yourInt;
System.out.println(yourInt);
System.out.println(ch);
// Output:
// 33
// !

还是想将<条码>ints改为String?

int yourInt = 33;
String str = String.valueOf(yourInt);

或者你指的是什么?

If you first convert the int to a char, you will have your ascii code.

例如:

    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);

有许多办法(视您的需要)转换为ASCII(ASCII),但这里是一种将每一分类的星体转换为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();
}

例如,ASCII“TEST”文本可作为星体表示:

byte[] test = new byte[] { (byte) 0x54, (byte) 0x45, (byte) 0x53, (byte) 0x54 };

然后,你可以做以下工作:

int value = ByteBuffer.wrap(test).getInt(); // 1413829460
System.out.println(toASCII(value)); // outputs "TEST"

......这样基本上将四位按32轨的 in合转化为4种不同的ASCII特性(一种特性是每 by)。

你们可以在 j将一些数字转换到ASCII。 例如,将第1号(基数为10)改为ASCII。

char k = Character.forDigit(1, 10);
System.out.println("Character: " + k);
System.out.println("Character: " + ((int) k));

产出:

Character: 1
Character: 49

In fact in the last answer String strAsciiTab = Character.toString((char) iAsciiValue); the essential part is (char)iAsciiValue which is doing the job (Character.toString useless)

Meaning the first answer was correct actually char ch = (char) yourInt;

如果在您的打字中(或0x31),时间为1

最简单的方法是使用类型:

public char toChar(int c) {
    return (char)c;
}

The most simple way is to get integer and just use the casting operator Ex

int num = 33;
System.out.println((char) num);    //Outputs 33

//if you want to find the integer value of character instead.
//Just do the reverse

char ch =  % ;
System.out.println((int) ch);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签