Huawei Computer Testing Questions (convert digital string to binary-java), Huawei-java

Source: Internet
Author: User

Huawei Computer Testing Questions (convert digital string to binary-java), Huawei-java

PS: full score for this question. See

/** Question: convert a numeric string to a binary string

* Description: enter a string of Integers to convert each integer to a binary number. If the last and third bits are "0", the output value is "0". If the last and third bits are "1 ", output "1 ".

Question category: bitwise operation

Difficulty: elementary

Score: 60

Run Time Limit: 10 Sec

Memory limit: 128 MByte

Phase: apply for an exam

Input: a string of integers with a length less than 1024. The integers are separated by spaces.

Output: an integer string of 1/0 separated by Spaces

Sample input: 240 0

Sample output: 0 0

Answer:

*/

 

 1 import java.util.Scanner; 2  3 public class Main { 4  5     public static void main(String[] args) { 6          7         String bitStr = ""; 8          9         Scanner cin = new Scanner(System.in);10         String str = cin.nextLine();11         12         bitStr = getBitString(str);13         14         System.out.println(bitStr);15         cin.close();16         17     }18 19     private static String getBitString(String str) {20 21         int len = 0;22         int bit = 0;23         int num = 0;24         StringBuilder sb = new StringBuilder();25         26         String[] strArray = str.split(" ");27         len = strArray.length;28         29         if(len >= 1024)30         {31             return "";32         }33         34         for(int i = 0; i < len; i++)35         {36             num = Integer.parseInt(strArray[i]);37             num /= 4;38             bit = num % 2;39 40             sb.append(bit+" ");41         }42         43         return sb.toString().substring(0, sb.length()-1);44     }45     46 }

 


How to convert a string to a binary number in java

Java. lang. Integer: the API package contains the hexadecimal conversion function.
Public static String toBinaryString (int I)
Public static String toHexString (int I)
Public static String toOctalString (int I)
All three functions can convert decimal integers into two, one, six, and eight decimal integers.
However, the converted result is in the string format.
 
How to convert a binary number into a corresponding string in java

Public static String getSecret (byte [] B) {// obtain the ciphertext
String string = "";
Int c = B. length/6;
Int last = B. length % 6;
Int point = 0;
Int temp = 0;
For (int I = 0; I <c; I ++ ){
For (int j = 6; j> 0; j --){
Int temp1 = Integer. valueOf (B [point])-48;
Int z = 0;
While (z <J-1 ){
Temp1 = temp1 * 2;
Z ++;
}
Point ++;
Temp = temp + temp1;
}
String string2 = String. valueOf (char) temp );
String = string + string2;
Temp = 0;
}
For (; last> 0; last --){
Int z = 0;
Int temp1 = Integer. valueOf (B [point])-48;
While (z <last-1 ){
Temp1 = temp1 * 2;
Z ++;
}
Point ++;
Temp = temp + temp1;
}
String string2 = String. valueOf (char) temp );
String = string + string2;
Return string;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.