如何将ASCII码(0-255)转换为关联字符的字符串?

时间:2011-10-08 00:26:36

标签: java ascii

我的int int范围为0-255,我想创建一个String(长度为1),以便该单个字符的ASCII值是指定的整数。

在Java中有一种简单的方法吗?

示例:

65  -> "A"
102 -> "f"

11 个答案:

答案 0 :(得分:218)

答案 1 :(得分:53)

System.out.println((char)65); 会打印“A”

答案 2 :(得分:28)

String.valueOf ( Character.toChars(int) )

假设整数是,如你所说,在0到255之间,你将从Character.toChars获得一个带有单个字符的数组,当传递给{{1}时,它将成为单字符字符串}。

使用String.valueOf优于涉及从Character.toCharsint(即char)的广告投放的方法,原因有多种,包括(char) iCharacter.toChars如果你没有正确验证整数,那么抛出一个IllegalArgumentException,而演员会吞下错误(按照narrowing primitive conversions specification),可能会产生一个非你想要的输出。

答案 3 :(得分:8)

int number = 65;
char c = (char)number;

这是一个简单的解决方案

答案 4 :(得分:4)

new String(new char[] { 65 }))

您将得到一个长度为1的字符串,其单个字符具有(ASCII)代码65.在Java字符中是数字数据类型。

答案 5 :(得分:1)

可以像这样从a到z迭代

int asciiForLowerA = 97;
int asciiForLowerZ = 122;
for(int asciiCode = asciiForLowerA; asciiCode <= asciiForLowerZ; asciiCode++){
    search(sCurrentLine, searchKey + Character.toString ((char) asciiCode));
}

答案 6 :(得分:0)

更简单的方法:

将转换整数输入到字符,让int n为整数, 然后:

Char c=(char)n;
System.out.print(c)//char c will store the converted value.

答案 7 :(得分:0)

@implementation ViewController

  -(void) viewDidLoad {
    [super viewDidLoad];

    [self.categoriesBtn setTitle: @ "+ Categories" forState: UIControlStateNormal ];
    self.data = [[NSArray alloc] initWithObjects: @ "Value1", @ "Value2", @ "Value3", @ "Value4", @ "Value5", nil ];
    self.categoriesTable.delegate = self;
    self.categoriesTable.dataSource = self;
    self.categoriesTable.hidden = YES;

  }

  -(NSInteger) tableView: (UITableView * ) tableView numberOfRowsInSection: (NSInteger) section {

    return [self.data count];

  }

  -(UITableViewCell * ) tableView: (UITableView * ) tableView cellForRowAtIndexPath: (NSIndexPath * ) indexPath {

    static NSString * simpleTableIdentifier = @ "SimpleTableItem";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: simpleTableIdentifier];
    if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: simpleTableIdentifier];
    }
    cell.textLabel.text = [self.data objectAtIndex: indexPath.row];

    return cell;
  }

  -(IBAction) categoriesButton: (id) sender {
    if (self.categoriesTable.hidden == YES) {
      self.categoriesTable.hidden = NO;
    } else
      self.categoriesTable.hidden = YES;
  }


  -(void) tableView: (UITableView * ) tableView didSelectRowAtIndexPath: (NSIndexPath * ) indexPath {

    UITableViewCell * cell = [self.categoriesTable cellForRowAtIndexPath: indexPath];
    [self.categoriesBtn setTitle: cell.textLabel.text forState: UIControlStateNormal];
    self.categoriesTable.hidden = YES;

  }

  -(void) didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  }

@end

答案 8 :(得分:0)

为什么不将其简化为返回ascii字符的方法?

public char toAscii (int input) {
    return (char)input;
}

答案 9 :(得分:-1)

这是一个例子,它表明通过将int转换为char,可以确定ASCII码的相应字符。

public class sample6
{
    public static void main(String... asf)
    {

        for(int i =0; i<256; i++)
        {
            System.out.println( i + ". " + (char)i);
        }
    }
}

答案 10 :(得分:-1)

上面的答案只能解决问题。继承人你的回答:

Integer.decode(Character.toString(char c));