2017-02-15 1 views
0

나는 Scanner 입력으로 문자열을 받아이를 2 진수의 ASCII 숫자로 변환하고 각 문자의 빈도를 출력하는 프로그램을 만들었습니다. 우리 모두가 각각의 ASCII 긴 7 비트의 길이이어야한다 알고 있듯이이자바에서 아스키의 누락 된 앞자리 0을 잡는 방법

import java.util.*; 

public class ASCII 
{ 
    public static void main (String [] args) 
    { 
     Scanner sc = new Scanner (System.in); 
     System.out.println("Enter your sentence: "); 
     String myString = sc.nextLine(); 
     int length = myString.length(); 
     int ascii; 
     char character; 
     int[] myarray = new int[256]; 
     for(int i =0; i < length; i++) 
     { 
      character = myString.charAt(i); 
      ascii = (int)character; // casting the character into an int 
      System.out.print(Integer.toBinaryString(ascii) + " "); 
      myarray[ascii]++; // counting the frequency of each letter in the string 
     } 

     System.out.println(); 

     for (int k = 0; k < myarray.length; k++) 
     { 
      if (myarray[k] > 0) // if the frequency is greater than 0 do this print line, without this programm would print all 255 ascii characters. 
      { 
       System.out.println("'"+(char)k + "'" + " appeared " + myarray[k]  + " times."); 
      } 
     } 
    } 
} 

을 그대로 여기 내 프로그램입니다하지만 내 프로그램은 그것이 앞의 0을 잡기되지 않는 경우는 항상 아닙니다. 내가 문자열을 전달한다면 예를 들어, "안녕하세요" 예상 출력은

"1101000 1100101 1101100 1101111 0100000 1110100 1101000 1100101 1110010 1110010 1100101" 

해야하지만 난 점점 오전 :

"1101000 1100101 1101100 1101100 1101111 100000 1110100 1101000 1100101 1110010 1100101" 

는 "100000"을 볼 수있는 단지입니다 6 비트 길고 내 ascii 값 중 일부는이 잘못된 선행 0 문제로 인해 잘못되었습니다.

나는이 문제를 해결하는 방법에 대해 확신하지 못하기 때문에 여기에 있습니다. 누군가가 이것이 어떻게 고정 될 수 있는지를 보여 주면 좋을 것입니다.

+0

사용할 때 무슨 일이 일어나고 : System.out.print (Integer.toString (ascii, 2) + ""); –

+0

예, 0-127의 번호가 지정된 128 개의 ASCII 코드 포인트가 있으며 0-127 값으로 인코딩됩니다. 그러나 Java에서 ASCII를 사용한다고 가정 할 때 프로그램에 몇 가지 주장이 있습니다. 자바, 자바 스크립트, 닷넷, XML, HTML, ... [유니 코드] (http://www.unicode.org/charts/nameslist/index.html)를 사용하십시오. 'charAt (i)'는 UTF-16 코드 단위를 반환하는데, 그 중 하나 또는 두 개는 A 또는 € 또는와 같은 유니 코드 코드 포인트를 인코딩합니다. C0 Controls 및 Basic Latin 블록과 같은 하위 집합 만 처리하려는 경우 프로그램에 가드 (if-throw 또는 유사)를 넣어야합니다. –

답변

-1
public static void main (String [] args) 
{ 
    String myString = "hello there"; 
    int length = myString.length(); 
    int ascii; 
    char character; 
    int[] myarray = new int[256]; 
    for(int i =0; i < length; i++) 
    { 
     character = myString.charAt(i); 
     ascii = character; // casting the character into an int 
     // not optimal, but works 
     System.out.println(String.format("%7s", Integer.toBinaryString(ascii)).replace(' ', '0')); 

     myarray[ascii]++; // counting the frequency of each letter in the string 

    } 
    System.out.println(); 

    for (int k = 0; k < myarray.length; k++) 
    { 
     if (myarray[k] > 0) // if the frequency is greater than 0 do this print line, without this programm would print all 255 ascii characters. 
     { 
      System.out.println("'"+(char)k + "'" + " appeared " + myarray[k]  + " times."); 
     } 
    } 
} 
+0

이 방법을 사용하면 실제로 7 대신 7 비트 당 8 비트를 인쇄하여 7을 인쇄 할 수 있습니까? 물론 –

+0

여기에 오타를 입력하십시오. 수정 해줘서 고마워. – kamehl23

0

당신의 int로 문자열을 변환 중지하고 직접 문자열을 비교하거나 문자열로 K를 변환해야합니다 중 하나는, 다음 문자로 변환, 선행 0의 필요 수량을 추가합니다.