Question

1. you will just write your name in bytes - once in UTF8 bytes, once in...

1. you will just write your name in bytes - once in UTF8 bytes, once in UTF16 bytes. Name :Andrew

Submit:
1. Your .java file

2. A screenshot showing your code ran and gave the right output.

----

/**
* In this program you will write your first name in bytes in 2 different encodings.
* Then convert the byte array to a String and print it out.
*
* TODO in lecture: show students how I google to find the bytes for my name in both encodings.
*
* @author melvyn
*
*/

import java.io.UnsupportedEncodingException;

public class SampleSubmission {
   private static String createMyNameFromUTF8Bytes() throws UnsupportedEncodingException {
       byte[] utf8Bytes = {
               (byte) 0x30 // "0"
       };
       return new String(utf8Bytes, "UTF-8");
   }
  
   private static String createMyNameFromUTF16Bytes() throws UnsupportedEncodingException {
       // Note: heres a website that shows utf16 binary for different bytes.
       // https://asecuritysite.com/coding/asc2?val=0%2C255
       byte[] utf16Bytes = {
               (byte) 0x00, (byte) 0x30 // "0"
       };
       return new String(utf16Bytes, "UTF-16");
   }
  
   public static void main(String[] args) {
       String s8 = null;
       String s16 = null;
      
       try {
           s8 = createMyNameFromUTF8Bytes();
       } catch (UnsupportedEncodingException e) {
           System.err.println(e.getMessage());
       }
      
       try {
           s16 = createMyNameFromUTF16Bytes();
       } catch (UnsupportedEncodingException e) {
           System.err.println(e.getMessage());
       }
      
       System.out.println("String created with utf8 bytes: " + s8);
       System.out.println("String created with utf16 bytes: " + s16);
       System.out.println("Are the strings equal? " + s8.equals(s16));
   }
  
}

Homework Answers

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Java Program: You will be inserting values into a generic tree, then printing the values inorder,...
Java Program: You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
1. Paste the code below into your Java IDE and make sure it runs correctly. 2....
1. Paste the code below into your Java IDE and make sure it runs correctly. 2. Create a program that receives numeric data from the user and then adds it to a queue. 3. Create a program that receives text based data and then adds it to a queue. 4. Create a program that automatically populates a queue on start up with 5 pieces of data and then displays each entry in the queue after receiving a command from the...
You will be traversing through an integer tree to print the data. Given main(), write the...
You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 -...
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text...
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text editor of your choice). This program will “explode” a String into an array of characters (char[]), and then print out the array. The String comes from the first command-line argument. Example output with the command-line argument foo is below: f o o --------------------------------------------------------------------- public class StringExplode { // TODO - write your code below this comment. // You will need to write a method...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age. Ex: If the input is: Lee 18 Lua...
For this part, you will write a PostfixCalculator class that has methods for processing each possible...
For this part, you will write a PostfixCalculator class that has methods for processing each possible input. You will write a Tester class that reads a line of input from the user, with each symbol separated by a space, and prints out the numeric value of the top of the stack. If the user specifies an incomplete expression, print out the top of the stack and print out a message saying that the stack contains more than one item. If...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...