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
i am trying to wrire a word and change it to capital letters using client-server. im...
i am trying to wrire a word and change it to capital letters using client-server. im not able to write any words when i run the file in netbeans or command promot. is it something with my code? /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package assignment3retake; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter;...
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)...
Q:Could you fill in the blank。 class Employee{ // field private String name; private (1) _float__...
Q:Could you fill in the blank。 class Employee{ // field private String name; private (1) _float__ salary; // constructor (2) ________________________________________ // method public (3) __String____ getName( ){ return name; } public float getSalary( ){ return Salary; } public (4) ________ compare( (5) _________________ { (6) ____________________________________ } }// class Employee public class EmployeeUI{ public static void main(String[ ] args){ Employee e1 = new Employee(“William”); Employee e2 = new Employee(“Rchard”); if(Employee.compare(e1, e2)) System.out.println(“same employee”); else System.out.println(“different employee”); } }//class EmployeeUI
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...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
1.If you have defined a class,  SavingsAccount, with a public  static method,  getNumberOfAccounts, and created a  SavingsAccount object referenced by...
1.If you have defined a class,  SavingsAccount, with a public  static method,  getNumberOfAccounts, and created a  SavingsAccount object referenced by the variable  account20, which of the following will call the  getNumberOfAccounts method? a. account20.getNumberOfAccounts(); b. SavingsAccount.account20.getNumberOfAccounts(); c. SavingsAccount.getNumberOfAccounts(); d. getNumberOfAccounts(); e. a & c f. a & b 2.In the following class, which variables can the method printStats use? (Mark all that apply.) public class Item { public static int amount = 0; private int quantity; private String name; public Item(String inName, int inQty) { name...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6    at assignment2.Client.readStudents(Client.java:64)    at assignment2.Client.main(Client.java:253) void readStudents() { //...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6    at assignment2.Client.readStudents(Client.java:64)    at assignment2.Client.main(Client.java:253) void readStudents() { // Scanner class object declare Scanner readStudentFile = null; // try block begin /*System.out.println("Working Directory = " + System.getProperty("user.dir")); // for debugging purposes only*/ try { // open file readStudentFile = new Scanner(new File("student.txt")); // loop until end of file while (readStudentFile.hasNextLine()) { String stu = readStudentFile.nextLine(); String[] eachStu; eachStu = stu.split(" "); String firstName = null, middleName = null, lastName = null; long id...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs in Chapter 1 of your textbook are not complete. They are used for illustration purpose only. The implementation of Listing 1-1 on page 39 is explained in Chapter 2. And, in order to see the result of using it, we will need the following set of files: i. BagInteface.java – the specification only. ii. ArrayBag.java – the implementation of BagInerface.java. iii. ArrayBagDemo.java – a...
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks....
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each...