Results 1 to 16 of 16

Thread: byte to int

  1. #1
    arieana is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default byte to int

    Hi,
    i have problem about how to convert byte to int.
    i don't know how to do anymore, because the value of x is 49, that is represent the ascii code.
    Here the output:
    First character: 1
    Number 49 : null

    what i want is:
    First character: 1
    Number 1 : a
    Here is my code. Someone plz, help me.

    Java Code:
    RandomAccessFile raf = new RandomAccessFile(file, "rw");
            byte ch = raf.readByte();
            System.out.println("First character: " + (char)ch);
            raf.seek(0);
              
            int x = (int)ch;
            numbers[1]="a";
            numbers[2]="b";
            numbers[3]="c";
            numbers[4]="d";
            numbers[5]="e";
            numbers[6]="f";  
         
            if(x >=0 && x<=6)
        	  System.out.println("Number "+x+" : "+ numbers[x]);
    TQ.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    Integer.parseInt(String.valueOf(char))

  3. #3
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by arieana View Post
    Here the output:
    First character: 1
    Number 49 : null

    what i want is:
    First character: 1
    Number 1 : a
    Here is my code. Someone plz, help me.

    Java Code:
    RandomAccessFile raf = new RandomAccessFile(file, "rw");
            byte ch = raf.readByte();
            System.out.println("First character: " + (char)ch);
            raf.seek(0);
              
            int x = (int)ch;
            numbers[1]="a";
            numbers[2]="b";
            numbers[3]="c";
            numbers[4]="d";
            numbers[5]="e";
            numbers[6]="f";  
         
            if(x >=0 && x<=6)
        	  System.out.println("Number "+x+" : "+ numbers[x]);
    TQ.

    IS the line
    Number 49 : null printing ???? coz the if(x >=0 && x<=6) loop does not satisfy the condition...


    and by the way wat are you trying to achieve by int x = (int)ch;????

    and how are u planning to use x??


    happy coding

    warm regards
    Vinod M
    Last edited by Vinod Mukundan; 12-02-2010 at 10:14 AM.
    _______________________________________________
    give me beans .........

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    The OP wants the character "1" to be an integer 1 and the cast, of course, is returning the ascii value of "1" which is 49, see my post.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by arieana View Post
    Hi,
    i have problem about how to convert byte to int.
    i don't know how to do anymore, because the value of x is 49, that is represent the ascii code.
    Here the output:
    First character: 1
    Number 49 : null

    what i want is:
    First character: 1
    Number 1 : a
    Here is my code. Someone plz, help me.

    Java Code:
    RandomAccessFile raf = new RandomAccessFile(file, "rw");
            byte ch = raf.readByte();
            System.out.println("First character: " + (char)ch);
            raf.seek(0);
              
            int x = (int)ch;
            numbers[1]="a";
            numbers[2]="b";
            numbers[3]="c";
            numbers[4]="d";
            numbers[5]="e";
            numbers[6]="f";  
         
            if(x >=0 && x<=6)
        	  System.out.println("Number "+x+" : "+ numbers[x]);
    TQ.
    How big is your array 'numbers'? If x == 49 (you typed a '1') note that the if condition near the end of your code fails.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    The OP wants the character "1" to be an integer 1 and the cast, of course, is returning the ascii value of "1" which is 49, see my post.
    Ya I got it .... I thought he wanted it from the array ...... well still the print

    Number 49 : null

    wont come ..

    yeah of-course your solution should do the trick ..:)
    _______________________________________________
    give me beans .........

  7. #7
    arieana is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    sorry2 my mistake..
    actually output :
    First character: 1

    that output came out when i replace if(x >=0 && x<=49)
    First character: 1
    Number 49 : null

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Okay? I kinda figured that, already. So, does it work now?

  9. #9
    arieana is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    i just want the value of x is 1 not 49.

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Okay? And did you read the first post?

  11. #11
    arieana is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    yup.

    is it like this?
    int x = Integer.parseInt(String.valueOf(ch));

    when i print value of x, it still =49

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Post the actual code in that area because as long "ch" is a char (or a byte) and not an int it definately will result in the int value 1 for the character/string "1", which means you are doing something else wrong (maybe casting to an int or some such).

  13. #13
    arieana is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    ok, here my code:

    Java Code:
    import java.io.*;
    public class FileInput2{
    	public static void main(String args[]) throws IOException{
    	
    		String numbers []=new String[100];
    	
    			
    		try{
    		
    	 File file = new File("D:\\test.txt");
    	 
            RandomAccessFile raf = new RandomAccessFile(file, "rw");
            byte ch = raf.readByte();
            System.out.println("First character: " + (char)ch);
            raf.seek(0);
            int x = Integer.parseInt(String.valueOf(ch));
            System.out.println("x:"+x);
          
            numbers[1]="a";
            numbers[2]="b";
            numbers[3]="c";
            numbers[4]="d";
            numbers[5]="e";
            numbers[6]="f";  
         
            if(x >=0 && x<=6)
        	  System.out.println("Number "+x+" : "+ numbers[x]);
           
      		} 
    		catch(Exception err){
    			System.out.println("Error Caught" + err);
    		}
    	}
    }

  14. #14
    arieana is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    and the output:
    First character: 1
    x:49

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Ah, sorry, "byte" will have the same value as "int", make sure to cast ch as char.

  16. #16
    arieana is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Read a bitmap byte by byte
    By cbr400aero in forum Java 2D
    Replies: 2
    Last Post: 10-18-2010, 11:42 PM
  2. byte[]
    By bobo67 in forum New To Java
    Replies: 10
    Last Post: 09-11-2010, 02:06 PM
  3. Replies: 1
    Last Post: 04-21-2010, 02:28 AM
  4. Byte Ranges
    By alessandro_ in forum Networking
    Replies: 2
    Last Post: 02-14-2010, 11:48 AM
  5. int to byte
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:22 PM

Posting Permissions

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