zoukankan      html  css  js  c++  java
  • java代码(ascii与字母互转)

    package test;

    /**

    * Java中将一个字符与对应Ascii码互转

    * 1 byte = 8bit 可以表示 0-127

    */

    public class GetCharAscii

    {

        /*0-9对应Ascii 48-57

        *A-Z 65-90

        *a-z 97-122

        *33126(94)是字符,其中第4857号为09十个阿拉伯数字

        */

        public static void main(String[] args)

        {

            // System.out.println(charToByteAscii('9'));

            // System.out.println(byteAsciiToChar(57));

            System.out.println(SumStrAscii("="));

            System.out.println(SumStrAscii(">"));

        }

        /**

        * 方法一:将char 强制转换为byte

        * @param ch

        * @return

        */

        public static byte charToByteAscii(char ch)

        {

            byte byteAscii = (byte)ch;

            return byteAscii;

        }

        /**

        * 方法二:将char直接转化为int,其值就是字符的ascii

        * @param ch

        * @return

        */

        public static byte charToByteAscii2(char ch)

        {

            byte byteAscii = (byte)ch;

            return byteAscii;

        }

        /**

        * 同理,ascii转换为char 直接int强制转换为char

        * @param ascii

        * @return

        */

        public static char byteAsciiToChar(int ascii)

        {

            char ch = (char)ascii;

            return ch;

        }

        /**

        * 求出字符串的ASCII值和

        * 注意,如果有中文的话,会把一个汉字用两个byte来表示,其值是负数

        */

        public static int SumStrAscii(String str)

        {

            byte[] bytestr = str.getBytes();

            int sum = 0;

            for(int i = 0; i < bytestr.length; i++)

            {

                sum += bytestr[i];

            }

            return sum;

        }

    }

  • 相关阅读:
    Visual C#核心编程之泛型
    Visual C#核心编程之枚举器
    标准的非托管资源的销毁模式
    Visual C#核心编程之LINQ
    Visual C#核心编程之数组和集合
    ACCPSQL第四章上机六
    Visual C#2008核心编程之类型
    一月一代码 3月 kmp 领悟代码
    [转] 技巧 如何统一设置 windows live writer 的 图片大小
    understanding the linux virtual memory management 图序
  • 原文地址:https://www.cnblogs.com/NiceTime/p/6758650.html
Copyright © 2011-2022 走看看