Working with C++ Data in Java Presented by: Jessica Winblad This Presentation © 2008 Jessica Winblad.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Other Types Of Data rVariables of type short use half a word (16 bits) to represent a value. l everything.
Advertisements

Chapter 3 Message in a Bottle Programming Loops in C.
1 Floating Point Representation and Arithmetic (see Patterson Chapter 4)
Literals Why does this produce an error? float x = 2.5;
CH10 Instruction Sets: Characteristics and Functions
Morgan Kaufmann Publishers Arithmetic for Computers
Binary Number Systems.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
IP address Universally accepted addressing method is required so that all hosts can communicate with each other TCP/IP based network is assigned with unique.
Binary Lesson 4 Hexadecimal and Binary Practice. Counting to 15 Base Base Base 16 Base Base Base 16 Two Ten (Hex) Two Ten (Hex)
14-1 Bard, Gerstlauer, Valvano, Yerraballi EE 319K Introduction to Embedded Systems Lecture 14: Gaming Engines, Coding Style, Floating Point.
Introduction to Programming G51PRG University of Nottingham Revision 1
DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
Lecture 4 Sending and Receiving Messages Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.

8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
Portability CPSC 315 – Programming Studio Spring 2008 Material from The Practice of Programming, by Pike and Kernighan.
What is an instruction set?
Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter.
CENG 311 Machine Representation/Numbers
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
Data Representation - Part I. Representing Numbers Choosing an appropriate representation is a critical decision a computer designer has to make The chosen.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
Java Introduction to JNI Prepared by Humaira Siddiqui.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Roopa.T PESIT, Bangalore. Source and Credits Dalvik VM, Dan Bornstein Google IO 2008 The Dalvik virtual machine Architecture by David Ehringer.
Lecture #5 Introduction to C++
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Big Endian vs. Little Endian Storage of Numeric Data Noah Mendelsohn Tufts University Web:
Chapters 2 & 3. .NET Software development model that allows applications created in disparate programming languages to communicate Universal data access.
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSE 351 Number Representation & Operators Section 2 October 8, 2015.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Data Types Always data types will decide which type of information we are storing into variables In C programming language we are having 3 types of basic.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Lecture 4: Representing Negative Numbers CS 2011 Spring 2016, Dr. Rozier.
CompSci From bits to bytes to ints  At some level everything is stored as either a zero or a one  A bit is a binary digit a byte is a binary.
CSE 220 – C Programming Bitwise Operators.
Object Writing in files
ECE Application Programming
Microprocessor Systems Design I
Internet and Java Foundations, Programming and Practice
Microprocessor Systems Design I
CPSC 315 – Programming Studio Spring 2012
Java package classes Java package classes.
Portability CPSC 315 – Programming Studio
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Chapter 9 Instruction Sets: Characteristics and Functions
Focus of the Course Object-Oriented Software Development
Comp Org & Assembly Lang
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
Presentation transcript:

Working with C++ Data in Java Presented by: Jessica Winblad This Presentation © 2008 Jessica Winblad

Outline Working with C++ DLLs Working with C++ DLLs Java Native Interface (JNI) Java Native Interface (JNI) I/O with Files & Streams I/O with Files & Streams Byte/Bit Order Byte/Bit Order Data Type translation Data Type translation

Real World Example Powering test-equipment for 12.5 seconds Powering test-equipment for 12.5 seconds

Project Requirements Send voltage out a parallel port for a specified time in seconds, to a half-second accuracy Send voltage out a parallel port for a specified time in seconds, to a half-second accuracy Run on a Windows 2000 or XP machine Run on a Windows 2000 or XP machine Integrate with a test-suite already written in Java Integrate with a test-suite already written in Java

Engineering Challenge With Windows 98 this would have been easy With Windows 98 this would have been easy Windows 2000 provides less transparent access to the machines hardware Windows 2000 provides less transparent access to the machines hardware Only hardware device drivers can directly access the hardware Only hardware device drivers can directly access the hardware You cant write device drivers in Java You cant write device drivers in Java

Solution Use a device driver DLL that allows direct programming of the parallel port Use a device driver DLL that allows direct programming of the parallel port Use JNI to access the device driver Use JNI to access the device driver

JNI (Java Native Interface) class ioPort { public native void Out32( short PortAddress, short data); static { System.loadLibrary("jnpout32pkg");} } … ioPort pp = new ioPort(); pp.Out32(0x378, currentVal); class ioPort { public native void Out32( short PortAddress, short data); static { System.loadLibrary("jnpout32pkg");} } … ioPort pp = new ioPort(); pp.Out32(0x378, currentVal);

Another Real World Example

Byte and Bit Order Matter Big Endian (Network Byte Order) Big Endian (Network Byte Order) Eg: Motorolla 68k processor Eg: Motorolla 68k processor Little Endian Little Endian Eg: x86 PC Eg: x86 PC May need to test for endianness May need to test for endianness Java has classes to help with correcting byte order (eg: java.nio.ByteOrder ) Java has classes to help with correcting byte order (eg: java.nio.ByteOrder ) A source value of 0xFFFE = Could be read as: etc.

Converting Data Types Applies to both streams and files Applies to both streams and files If you have a double in C++ should you use a readDouble() method of your java stream/file reader to read it? (No) If you have a double in C++ should you use a readDouble() method of your java stream/file reader to read it? (No) Java and C++ do not always have the same names for equivalent primitive types. Java and C++ do not always have the same names for equivalent primitive types. Some types dont map nicely. Some types dont map nicely.

Size 1 byte2 bytes4 bytes8 bytes16 bytes 8 bits16 bits32 bits64 bits128 bits C++char/bool float(long) double Javabooleanchar floatdouble Size 1 byte 2 bytes 4 bytes 8 bytes 16 bytes 8 bits 16 bits 32 bits 64 bits 128 bits C++byteshortint/long long long __int128 JavabyteshortintlongBigInteger Internal Sizing of Data Types * C++ sizes are OS/compiler dependent (Win32 shown)

Signed/Unsigned Types Java ensures consistency by always using signed types Java ensures consistency by always using signed types C++ supports both signed & unsigned types C++ supports both signed & unsigned types Unsigned Byte Signed Byte Unsigned Short Signed Short Size 1 byte 2 bytes Value Range 0 to to to 65, ,768 to 32,767

Principle of Conversion To read in unsigned values from C++ the resulting type in Java needs to be larger To read in unsigned values from C++ the resulting type in Java needs to be larger Also, some extra conversion needs to be done to fix incorrect sign extension. Also, some extra conversion needs to be done to fix incorrect sign extension.

Naïve Unsigned Conversion short value = (short) in.readByte(); short value = (short) in.readByte(); Question: If a short can hold from 0 to 65,535 why doesnt this work for values ? Question: If a short can hold from 0 to 65,535 why doesnt this work for values ? Answer: Sign Extension applied when casting Answer: Sign Extension applied when casting

How Does Sign Extension Work? unsigned byte: 129 = 0x81 = unsigned byte: 129 = 0x81 = The sign bit is extended: The sign bit is extended: In twos compliment, if the sign bit = 1, the number is presumed negative. In twos compliment, if the sign bit = 1, the number is presumed negative.

Twos Compliment Raw Bits Signed Unsigned Raw Bits Signed Unsigned = = = = = = = = = = = = = = = =

Solution: Bit Masking byte b = in.readByte(); // reads as signed short bitmask = (short) 0xff; short value = (short)(b & bitmask); xxx xxxx negative xxx xxxx negative & xFF xxx xxxx positive xxx xxxx positive

Be Careful with Unsigned Ints long a = (long)(in.readInt() & 0xffffffff); long a = (long)(in.readInt() & 0xffffffff); doesnt work! doesnt work! Reason: 0xffffffff is a negative value. Reason: 0xffffffff is a negative value. Solution: Solution: long a = (long)(in.readInt() & 0xffffffffL); long a = (long)(in.readInt() & 0xffffffffL);

Dealing with Decimals Going from a C++ double to a Java float is easy because both are 8-byte IEEE 754 values. Going from a C++ double to a Java float is easy because both are 8-byte IEEE 754 values. Going from a C++ float to Java is harder because Java does not have a 4-byte float type Going from a C++ float to Java is harder because Java does not have a 4-byte float type But Java gives tools to make the conversion easy But Java gives tools to make the conversion easy int a = in.readInt(); float b = Float.intBitsToFloat(a); int a = in.readInt(); float b = Float.intBitsToFloat(a); out.writeInt(Float.floatBitsToInt(floatValue)); out.writeInt(Float.floatBitsToInt(floatValue));

Other Pitfalls & Issues If your C++ code used bit-fields, you will have to do bit masking and shifting to read out the individual fields If your C++ code used bit-fields, you will have to do bit masking and shifting to read out the individual fields Or use java.util.BitSet Or use java.util.BitSet Reading in text (Strings) – encoding matters Reading in text (Strings) – encoding matters With plain English it may not, but if you have international characters in your text, it will matter With plain English it may not, but if you have international characters in your text, it will matter InputStreamReader(InputStream in, String enc); InputStreamReader(InputStream in, String enc);

Questions?