Справочник по C#

    Исходники по языку программирования CSharp

    moyadcode15

    Как преобразовать символ в ASCII в Java

    /
    /
    /
    51 Views
    moyadcode8
    moyadcode14

    В Java преобразовать символ в ASCII довольно просто, он просто конвертирует символ в int.

    Преобразовать символ в ASCII

    
    int ascii = (int) character;
    

    Конвертировать ASCII в символ

    
    char character = (char)ascii;
    

    Пример на Java

    
    package com.csharpcoderr.common;
    /**
    * Character Utility class
    * @author mkyong
    *
    */
    public class CharUtils
    {
    /**
    * Convert the characters to ASCII value
    * @param character character
    * @return ASCII value
    */
    public static int CharToASCII(final char character){
    return (int)character;
    }
    
    /**
    * Convert the ASCII value to character
    * @param ascii ascii value
    * @return character value
    */
    public static char ASCIIToChar(final int ascii){
    return (char)ascii;
    }
    }
    

    Модульный тест

    
    package com.csharpcoderr.common;
    import org.testng.Assert;
    import org.testng.annotations.*;
    /**
    * Character Utils Testing
    * @author mkyong
    *
    */
    public class CharUtilsTest {
    
    @DataProvider
    public Object[][] ValidDataProvider() {
    return new Object[][]{
    { 'A', 65 },{ 'a', 97 },
    { 'B', 66 },{ 'b', 98 },
    { 'C', 67 },{ 'c', 99 },
    { 'D', 68 },{ 'd', 100 },
    { 'Z', 90 },{ 'z', 122 },
    { '1', 49 },{ '9', 57 },
    
    };
    }
    
    @Test(dataProvider = "ValidDataProvider")
    public void CharToASCIITest(final char character, final int ascii) {
    int result = CharUtils.CharToASCII(character);
    Assert.assertEquals(result, ascii);
    }
    
    @Test(dataProvider = "ValidDataProvider")
    public void ASCIIToCharTest(final char character, final int ascii) {
    char result = CharUtils.ASCIIToChar(ascii);
    Assert.assertEquals(result, character);
    }
    }
    

    Результат модульного теста

    
    PASSED: CharToASCIITest(A, 65)
    PASSED: CharToASCIITest(a, 97)
    PASSED: CharToASCIITest(B, 66)
    PASSED: CharToASCIITest(b, 98)
    PASSED: CharToASCIITest(C, 67)
    PASSED: CharToASCIITest(c, 99)
    PASSED: CharToASCIITest(D, 68)
    PASSED: CharToASCIITest(d, 100)
    PASSED: CharToASCIITest(Z, 90)
    PASSED: CharToASCIITest(z, 122)
    PASSED: CharToASCIITest(1, 49)
    PASSED: CharToASCIITest(9, 57)
    PASSED: ASCIIToCharTest(A, 65)
    PASSED: ASCIIToCharTest(a, 97)
    PASSED: ASCIIToCharTest(B, 66)
    PASSED: ASCIIToCharTest(b, 98)
    PASSED: ASCIIToCharTest(C, 67)
    PASSED: ASCIIToCharTest(c, 99)
    PASSED: ASCIIToCharTest(D, 68)
    PASSED: ASCIIToCharTest(d, 100)
    PASSED: ASCIIToCharTest(Z, 90)
    PASSED: ASCIIToCharTest(z, 122)
    PASSED: ASCIIToCharTest(1, 49)
    PASSED: ASCIIToCharTest(9, 57)
    
    ===============================================
    com.csharpcoderr.common.CharUtilsTest
    Tests run: 24, Failures: 0, Skips: 0
    ===============================================
    
    
    ===============================================
    mkyong
    Total tests run: 24, Failures: 0, Skips: 0
    ===============================================
    

    Ссылка

    http://www.asciitable.com/

    Джава

    moyadcode14
    Как преобразовать символ в ASCII в Java

    0.00 (0%) 0 votes

    moyadcode13
    • Facebook
    • Twitter
    • Google+
    • Linkedin
    • Pinterest
    moyadcode10
    moyadcode11
    moyadcode9