ASCII编码表 以及怎么获取ASCII码 #Java #Python #C++ #JavaScript

本文详细介绍了如何在Java、C++、Python和JavaScript中分别通过Integer.valueOf()、强类型转换和ord()函数获取字符的ASCII码,涵盖了基本的字符编码概念和实践示例。

其中最常用:

0 - 9 : 48~57

A - Z :65~90

a - z : 97~122


java中如何获取字符的ASCII码



java中使用Integer.valueOf(char c)方法可以直接获取一个字符的ASCII码,比如:

public class ASCIITest {
    public static void main(String[] args) {
        char a='a';
        char A='A';
        int a_ascii=Integer.valueOf(a);
        int A_ascii=Integer.valueOf(A);
        System.out.println("字符"+a+"的ASCII码为:"+a_ascii);
        System.out.println("字符"+A+"的ASCII码为:"+A_ascii);
        System.out.println(A-2);
        System.out.println(A_ascii-a_ascii);
    }
}

 C++中如何获取字符的ASCII码


c++和java类似
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    
    char ch1 = 'a';
    char ch2 = 'A';
    
    cout << int(ch1) << endl;    //将字符强转成整形数,也就是我们能看懂的十进制数
    cout << int(ch2) << endl;
    
    return 0;
}

python中如何获取字符的ASCII码 


# python program to print ASCII
# value of a given character
 
# Assigning character to a variable
char_var = 'A'
# printing ASCII code
print("ASCII value of " + char_var + " is = ", ord(char_var))
 
char_var = 'x'
# printing ASCII code
print("ASCII value of " + char_var + " is = ", ord(char_var))
 
char_var = '9'
# printing ASCII code
print("ASCII value of " + char_var + " is = ", ord(char_var))


JavaScript中如何获取字符的ASCII码 


var s = 'A';
console.log(s.charCodeAt(0));
// 65
console.log(String.fromCharCode(65));
// 'A'
'apple'.split('').forEach(function (c) {
  console.log(c + ': ' + c.charCodeAt(0));
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值