在 java 中将 int 转换为 char

下面是一个代码片段,

int a = 1;
char b = (char) a;
System.out.println(b);

但我得到的是空的输出。

int a = '1';
char b = (char) a;
System.out.println(b);

我将得到1作为输出。

有人能解释一下吗?如果我想像第一个代码片段那样把 int 转换成 char,我应该怎么做?

766246 次浏览
int a = 1;
char b = (char) a;
System.out.println(b);

将用 Unicode字符1打印出字符(起始标题字符,这是不可打印的; 请参阅此表: C0控件和基本拉丁文,与 ASCII 相同)

int a = '1';
char b = (char) a;
System.out.println(b);

将打印出 Unicode字符为49的字符(其中一个字符对应于’1’)

如果您想转换一个数字(0-9) ,您可以添加48到它和强制转换,或类似 Character.forDigit(a, 10);

如果你想转换一个看起来像 Unicode字符的 int,你可以使用 Character.toChars(48)作为例子。

在 java 中,字符 为 int。第一个代码片段打印出默认字符编码方案(可能是 Unicode)中与值1对应的字符。Unicode字符 U + 0001是一个非打印字符,这就是为什么你看不到任何输出。

如果要打印出字符“1”,可以在所使用的编码方案中查找“1”的值。在 Unicode 中,这是49(与 ASCII 相同)。但这只对0-9位有效。

您最好使用 String 而不是 char,并使用 Java 内置的 toString()方法:

int a = 1;
String b = toString(a);
System.out.println(b);

这将工作,无论您的系统编码是,并将工作的多位数字的数字。

int a = 1;
char b = (char) a;
System.out.println(b);

你好,我也经历过同样的问题,但我所做的是下面的代码。

int a = 1
char b = Integer.toString(a).charAt(0);
System.out.println(b);

通过这种方式,您可以得到作为 char 类型的十进制值。我对索引0使用 charAt () ,因为进入该 String 的唯一值是‘ a’,正如您所知,进入该 String 的‘ a’的位置从0开始。

对不起,如果我的英语没有很好地解释,希望它能帮助你。

我的答案类似于 jh314的答案,但我会解释得更深一点。

在这种情况下,你应该做的是:

int a = 1;
char b = (char)(a + '0');
System.out.println(b);

在这里,我们使用’0’,因为字符实际上是由 ASCII 值表示的。“0”是一个字符,由值48表示。

我们输入 (a + '0'),为了把这些加起来,Java 将’0’转换为它的 ASCII 值,即48和 a 是1,因此总和是49。然后我们做的是:

(char)(49)

我们把 int转换成 char。等效于49的 ASCII 是“1”。这种方法可以将任意数字转换为字符,比使用 .toString()方法然后用 .charAt()方法减去数字更聪明、更好。

看起来你正在寻找 Character.forDigit的方法:

final int RADIX = 10;
int i = 4;
char ch = Character.forDigit(i, RADIX);
System.out.println(ch); // Prints '4'

还有一种方法可以将 char 转换回 int:

int i2 = Character.digit(ch, RADIX);
System.out.println(i2); // Prints '4'

请注意,通过改变 RADIX,您还可以支持十六进制(基数16)和任何基数高达36(或 Character.MAX_RADIX,因为它也被称为)。

这里没有人回答真正的“问题”: 您正在正确地将 int 转换为 char; 在 ASCII 表中,十进制值01是“开始标题”,这是一个非打印字符。尝试查找一个 ASCII 表,并在33和7E 之间转换一个 int 值; 这将为您提供可查看的字符。

如果你想把一个字符转换成相应的整数,你可以这样做:

int a = (int) 'a';
char b = (char) a;
System.out.println(b);

之所以会发生这种情况,是因为在 ASCII 中有一些项目无法正常打印。

例如,数字97到122是与小写字母 a 到 z 对应的整数。

每当您输入 cast int to char 时,它都会返回该 int 的 ascii 值(为了更好地理解,请查看 ascii 表)

    int a=68;
char b=(char)a;


System.out.println(b);//it will return ascii value of 68


//output- D

如果您想根据 ascii 代码打印 ascii 字符,并且不想超出这个范围(比如 unicode 字符) ,那么您可以将变量定义为一个字节,然后使用(char)转换。例如:

public static void main(String[] args) {
byte b = 65;
for (byte i=b; i<=b+25; i++) {
System.out.print((char)i + ", ");
}

顺便说一下,字母“ A”的 ASCII 代码是65

如果我们讨论的是类类型而不是原语,那么需要做到以下几点:

Integer someInt;
Character someChar;


someChar = (char)Integer.parseInt(String.valueOf(someInt));
public class String_Store_In_Array
{
public static void main(String[] args)
{
System.out.println(" Q.37 Can you store string in array of integers. Try it.");


String str="I am Akash";
int arr[]=new int[str.length()];
char chArr[]=str.toCharArray();
char  ch;
for(int i=0;i<str.length();i++)
{


arr[i]=chArr[i];
}
System.out.println("\nI have stored it in array by using ASCII value");
for(int i=0;i<arr.length;i++)
{


System.out.print(" "+arr[i]);
}
System.out.println("\nI have stored it in array by using ASCII value to original content");
for(int i=0;i<arr.length;i++)
{
ch=(char)arr[i];


System.out.print(" "+ch);
}
}
}

确保整数值是字母/字符的 ASCII 值。

如果没有,那就做吧。

for e.g. if int i=1

然后加上64,这样它就变成了65 = ASCII 值‘ A’ 那就用

char x = (char)i;


print x


// 'A' will be printed
int a = 1;
char b = (char) (a + 48);

在 ASCII 中,每个字符都有自己的编号。字符’0’表示小数点后48,’1’表示49,以此类推。所以如果

char b = '2';
int a = b = 50;

看看下面的程序完全转换的概念

class typetest{
public static void main(String args[]){
byte a=1,b=2;
char c=1,d='b';
short e=3,f=4;
int g=5,h=6;
float i;
double k=10.34,l=12.45;
System.out.println("value of char variable c="+c);
// if we assign an integer value in char cariable it's possible as above
// but it's not possible to assign int value from an int variable in char variable
// (d=g assignment gives error as incompatible type conversion)
g=b;
System.out.println("char to int conversion is possible");
k=g;
System.out.println("int to double conversion is possible");
i=h;
System.out.println("int to float is possible and value of i = "+i);
l=i;
System.out.println("float to double is possible");
}
}

希望至少能帮上忙

有一种方法可以将 int转换为 char,甚至不需要使用 ASCII 值。

例如:

int i = 2;
char ch = Integer.toString(i).charAt(0);
System.out.println(ch);

说明:

首先将整数转换为字符串,然后使用字符串函数 charAt()从字符串中提取字符。由于整数只有一个单位数字,因此将索引 0给予 charAt()函数。

首先,将 int(或其他类型)转换为 String,

int a = 1;
String value = String.valueOf(a);

然后,将该 String转换为 char

char newValue = value.charAt(0);

您可以通过这种方式避免空输出..。

System.out.println(newValue);

你可能希望它被打印为’1’或’a’。

如果你想输入“1”,那么:

int a = 1;
char b = (char)(a + '0');
System.out.println(b);

如果你想要“ a”作为输入,那么:

int a = 1;
char b = (char)(a-1 + 'a');
System.out.println(b);

Java 将 ascii 值转换为 char:)

我的解决方案是将小写字母(a-z)转换为(0-25) ,反之亦然。

我的答案是对于一个特定的用例,它不是通用的解决方案,如果你想把字符的频率存储到一个大小为26的整数数组中,而不是使用 Hashmap < Property,Integer > ,我的解决方案将会对你有所帮助

用于将0到25转换为 a-z

Char ch = (char)(0 + ‘ a’) ;//输出‘ a’//输入0(作为整数)

Char ch = (char)(25 + ‘ a’) ;//输出‘ z’//input 25(作为整数)

将 a 转换为 z 转换为0-25

Int freq = ‘ a’-‘ a’//output 0//input‘ a’

Int freq = ‘ b’-‘ a’//output 1//input‘ b’

Int freq = ‘ c’-‘ a’//output 2//input‘ c’

Int freq = ‘ z’-‘ a’//output 25//input‘ z’

同样,这种方法将帮助您获得字符的频率以及字符

public class Main
{
public static void main(String[] args) {
String s="rajatfddfdf";
int freq[]= new int[26];
for(int i=0;i<s.length();i++){
char characterAtIndex=s.charAt(i);
freq[characterAtIndex-'a']+=1;
}
for(int i=0;i<26;i++){
System.out.println((char)('a'+i)+" frequency="+freq[i]);
}
}
}

通过使用上述代码,我们可以得到的频率以及字符使用整数数组的大小26。如果不想包含频率为0的字符,我们可以编写 if-else 逻辑。