Avatar of gla
gla

asked on 

Convert ASCII char to int problem

I have the attached java code

the database Field "CODE" type is character 2 bytes. I want to get the Ascii code of each of 2 bytes. But when I print out (System.out.println()) the Ascii Code (int) custID[0]),  (int) custID[1]) I take negative numbers.

for example:

0 = 104
1 = -43
c0 = h
c1 = ?

I thing the Ascii codes begin from 0 to 255. Any idea on this problem?


Thanks

George
Selectstmt = "SELECT CODE FROM SO08_LOG_ERGA";
        
        try
        {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(Selectstmt);
     
            int p = 1;
            while (rs.next()) {  
                byte[] custID = rs.getBytes("CODE");
                
                char ccust0 = (char) custID[0];
                char ccust1 = (char) custID[1];
                
                int icust0 = (int) custID[0];
                int icust1 = (int) custID[1];
 
                System.out.println("0 = " + (int) custID[0]);
                System.out.println("1 = " + (int) custID[1]);
                System.out.println("c0 = " + (char) custID[0]);
                System.out.println("c1 = " + (char) custID[1]);
            }
 
            file.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

Open in new window

Java

Avatar of undefined
Last Comment
Mick Barry

8/22/2022 - Mon