Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Byte to String conversion

    I am not able to call toString() on a byte value. How to make the program to display the characters instead of printing the byte values?

    Java Code:
    class SubStringCons {
    	public static void main(String args[]) {
    		byte ascii[] = {65, 66, 67, 68, 69, 70 };
    		String s1 = new String(ascii);
    		System.out.println(s1);
    		for(int i=0;i<ascii.length;i++)
    		{
    			System.out.println(ascii[i]);
    		}
    	}
    }
    Also, in the for loop how is it that the length method does not require the brackets()?

  2. #2
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Byte to String conversion

    cast ascii to char in for loop ...

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Byte to String conversion

    you cannot call methods from primitive types and also "length" is field thats why you call it without parentheses ...
    Last edited by allaudin; 08-26-2013 at 07:53 AM.

Similar Threads

  1. Byte [] to String
    By taptaptill in forum New To Java
    Replies: 18
    Last Post: 12-20-2012, 09:03 AM
  2. byte to string conversion creates problem in streams
    By sakshi.13agg in forum Networking
    Replies: 1
    Last Post: 12-05-2012, 10:11 PM
  3. String to byte conversion
    By YellowSun in forum New To Java
    Replies: 1
    Last Post: 02-02-2012, 08:19 PM
  4. Replies: 2
    Last Post: 11-21-2011, 12:08 AM
  5. image to byte code file conversion
    By nupurashi in forum Java Tip
    Replies: 0
    Last Post: 01-28-2009, 10:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •