---
title: "java ascii"
author: "eryk"
date: "2012-09-17T14:59:37Z"
tags: []
categories: ["编程语言"]
slug: "java-ascii"
draft: false
lastmod: "2012-09-17T14:59:37Z"
id: 1679794
url: "https://eryk.iteye.com/blog/1679794"
---

```
import org.junit.Test;

public class byte2ascii {
	public static char convertChar1(byte b) {
		return (char) b;
	}

	public static char convertChar2(byte b) {
		return (char) ((0xff & b));
	}

	public static char convertChar3(byte b) {
		return (char) ((0 | b));
	}

	@Test
	public void test() {
		byte b = 65;
		System.out.println(convertChar1(b));
		System.out.println(convertChar2(b));
		System.out.println(convertChar3(b));
	}
	
	// 打印ascii码
	@Test
	public void printCode(){
	      for(byte b=0;b>=0 && b<128;b++){
	          System.out.print("十进制数： "+ b + "  |  ");
	          System.out.print("八进制数： "+ Integer.toOctalString(b) + "  |  ");
	          System.out.print("十六进制数： "+ Integer.toHexString(b) + "  |  ");
	          System.out.print("ASCII字符： "+(char)b);
	          System.out.println();  
	       }
	}
}
```