Unsigned data types

I have been using java for many years and never really needed this (which is probably why it's not in java by default), but I am porting a C++ library to java and the C++ code uses quite a bit of WORD (16 bit unsigned) and DWORDs (32 bit unsigned) and a great amout of shifting.
This is kind of a lame question, but I can't get it to work and I think it is due to the uncontrolable sign bit - does anyone have any resources that can point me in the right direction? Everything on the web just talks about java primitive data type - which I know already - but there is little about using unsigned types or how to port them. Links, books, whatever.
The project is to get a windows library to work on linux (any everything else) - I have ported it into a gcc C++ and will do JNI if it gets down to it, but I would like to get a native java class going. -- I can't post the code because it's NDAed :(
Any info, links, books, tips, anything is highly welcome
Thank you much,
Rob

You don't understand my concern (or I misunderstand
you). A char solution for the 16 bit unsigned type
(word) is great, but if you're gonna write methods to
shift a 32 bit dword as a long (ignoring the sign
bit), why not use that for the 16 bit version as well?I agree, promoting from 16->32 bits or 32-->64 bits sounds like a temporary solution. For "unsigned int"
I'd just use int and shift with >>> and handle comparisons with either promotion to long or a case analysis
on sign, which ever works better.
Let me add, as an old C/C++ programmer I'm glad >99% of the time that Java doesn't have the
plethora of unsigned types of C/C++ (not to method the ambiguity of 'char' in those languages!).
And I don't miss those languages dubious promotion rules. Every C book has the puzzler:
unsigned u = 1;
if (u < -1) {
   //always true!

Similar Messages

  • Unsigned data types in Java

    I know this is beating an old dead horse, but I find it frustrating that Java does not support unsigned types. I frequently work with binary file formats that store data in unsigned types. In Java there is no easy way to work with these files. For example, if the file has unsigned longs it is a problem. Also, if the file has unsigned ints, a very common occurrence, I have to "upcast" those numbers into longs. This is expensive from a time and space point of view since data files from satellites can be hundreds of megabytes or gigabytes in size and I have to double them in size just to capture the final bit. It is also inefficient to process these files because now I am using longs (64-bits) on systems that are usually optimized for 32-bits which means processing code takes a big performance hit.
    Also, there is simple confusion. For example, if I provide data from a file to a user and they see "long" where they know they data is an int they start asking questions and I have to start explaining why it is a long instead of an int. Scientists don't like data to be "translated" for integrity reasons so I have make these long explanations how upconverting to a long is not really a translation, etc. It just creates a lot of confusion and problems.
    Also, look at from a developer point of view. The developer has a spec in front of him listing the data types and is now reading the corresponding java class where the data gets loaded--all the data types are different because of the upconverting--it creates confusion.
    Is there any hope of having unsigned types or we all condemned to a lifetime of upconverting?

    JohnChamberlain wrote:
    I thought it was obvious that you need to upconvert unsigned values because otherwise the number will be wrong if the sign bit is set.No. A 32-bit scalar with the high bit set is still correct (bit-for-bit). You only get problems if you perform arithmetic, right-shift or widening-conversion on it, or make a decimal String representation.
    For example, lets say the file has an unsigned int (uint32) value of 0xF1234567 which in decimal is 4,045,620,583. This is a positive number. If you load this value into a Java int value it will be interpreted as the negative number -249,346,713.For a given value of "interpreted"; Integer.toHexString(0xF1234567) produces the expected result.
    Further example: if you load a file containing uint32 values into Java ints and then average all the values you might come out with a negative number. Obviously this is impossible because the file contains only positive numbers so the average of those numbers cannot be negative.Obviously if you need to do this on a number of large 32-bit scalars you need the sum scalar to be 64-bit which means you can mask your way out of any issues when you perform the widening conversion.
       int[] x = { 0xF1234567, 0xF1234565, 0xF1234557 };
       long sum = 0L;
       for(int i=0;i<x.length;++i) { sum += 0xffffffffL & x; }
    int average = (int)(sum/x.length); // average is now "correct" (bit-for-bit)
    Maybe it wasn't clear that I need to not only load the values but use them.You did fail to mention that.
    The files I load get passed on to other applications where the values get used and obviously that includes doing calculations on them so the values have to be correct. I cannot load a bunch of UInt32s into Java ints and say "pretend they are positive numbers".I get the impression you are not really interested in finding solution.
    It can be releasing to have a whinge but don't let it distract you from thinking outside the box.
    I think part of the problem here is that Java programmers do not have to work with binary files or binary protocols so they do not appreciate that doing this has serious problems in Java.I have done binary protocols. Not having unsigned scalars was never a problem.
    For example, take unsigned longs. If you are working with a binary source that has these you have no alternative except to use the BigInteger package and convert every value into an object, an incredibly painful and CPU-intensive operation if you dealing with files with gigabytes of data.You might want to consider alternatives, like
    unsigned right-shifting ('>>>') the incoming values for 64-bit scalar fields where dividing by 2 would be acceptable,
    etc.

  • JAVA primitive data types - Signed/ unsigned etc.

    1. Are the following data types signed or unsigned in JAVA?
    byte, short, int, long, float, double
    2. I have tried using "Integer" in place of "int" and the program runs perfectly. Why is this so when "Integer" is not a reserved word in JAVA?
    Thanks.

    And to answer your first question, byte, short, int, long, float and double are all signed.
    As a part of the core of the language, Sun developed a series of 'collections' classes; Vector, ArrayList, etc. All of these classes, in the days before generics, were able to store instances of the Object class or anything that derived from it. Therefore, to store an int, a float or a double value into one of these collections classes, it was necessary to wrap it up in an instance of one of those classes that derived from Number. These are the wrapper classes that the previous poster alluded to. Typically, the wrapper class has a similar name to the primitive data type but with a capital latter; int - Integer, double - Double, etc.

  • Unsigned byte: Java byte data type (0-255)?

    I need an "unsigned" byte type.
    How can I implement a one byte data type with values from 0-255 in Java?

    No there isn't an unigned byte, but you don't need
    one. You just separate storage representation from
    calculation representation. When you store the
    "unsigned byte" you use a signed byte. When you make
    calculations on the "unsigned byte" you use an
    integer. For this you need two conversion functions,
    public static int toInt(byte b) {
    return b + 128;
    public static byte toByte(int i) {
    return (byte)(i - 128);
    }These amazing functions must be tested off course.
    public void testUnsigned(int unsgn) {
    byte bA[] = new byte[1];
    int i = unsgn;          // 0 to 255
    bA[0] = toByte(i);    // stored as -128 to 127
    int j = toInt(bA[0]); // 0 to 255
    if (i==j)
    System.out.println("Yes it works!");
    Sorry, but your code doesn't work. I also tried the byte & 0xFF and I get strange results too. 0xFF transfer to 129, which (I think, but maybe I'm wrong) should transfer to 255 (0xFF == 1111 1111, no?). After some tests, I found that "ounos" solution is the best fitted (byte < 0 ? b+256 : b)
    If you only do a (byte + 128), you'll get, for example:
    0000 0000 (should be 0) == 128
    I think that only 1 case is enough to proove that this code doesn't work... :-)

  • How can I calling LabView DLL within LabView and pass similar Data Types?

    I am trying to use an Instrument Driver, which is created in LabView6.1 as a DLL. At this point I have only LabView to test this DLL. I was wondering, is there easy way to find out what sort of Parameter or Data Type I should be using.
    How can I pass the following data with in LabView:
    LVRefnum as Type?
    LVBoolean as Type?
    TD1 (a structure) as Type?
    It is funny to see that I am able to create a DLL in labview but having trouble calling it within LabView. I thought, it would be easier to test the DLL within the same environment.
    Basically, I am more worried about the VISA calls that are used in the driver to communicate with instrument. Because, there is no link to �VISA32.dll� in
    the header file, is that handled by the LV Run-time engine? I guess more details are needed on using the LabView DLL within LabView from National Instrument Technical Support.
    Attachments:
    RL5000.h ‏1 KB

    A LVRefNum seems to be an unsigned long data type (32bit). You can cast it
    in LV then use that as a parameter to call the DLL. (an Occurrence type
    seems to be a Ulong32)
    When you created the DLL what was the resulting type for the LVRefNum?
    Happy Holidays
    "Enrique" wrote in message
    news:[email protected]..
    > I see...
    >
    > After doing some research, it seems to me that there is no easy way to
    > find out the type of data, other than looking at the header file and
    > have documents like Using External Code in LabVIEW handy. The
    > following information is from that document:
    >
    > LVBoolean is an 8-bit integer. 1 if TRUE, 0 if FALSE.
    >
    > LabVIEW specifies file refnums using t
    he LVRefNum data type, the
    > exact structure of which is private to the file manager. To pass
    > references to open files into or out of a CIN, convert file refnums to
    > file descriptors, and convert file descriptors to file refnums using
    > the functions described in Chapter 6, Function Descriptions.
    >
    > I know you are creating a dll in LabVIEW, but I am pretty sure the
    > information applies as well and is useful. For your dll this can be
    > interpreted that, rather than passing a LVRefnum, try passing the file
    > descriptor.
    >
    > From the header file, is can be deduced that TD1 is a cluster in
    > LabVIEW.
    >
    > You are right in saying that "more details are needed on using the
    > LabView DLL within LabView from National Instrument Technical
    > Support.".
    >
    > Enrique

  • Selecting #of bits for data types in Java

    hi,
    i would like to know if we can constrain compiler on allocating memory to basic data types, such as int, float, short etc. i would like to read an exactly four byte integer into an int, 2 byte short into a short etc. i need to read these parameters exactly of their specified number of bytes - not more or less - as if i read anything more/less, i will be reading a part of other parameter.
    i know that C allows us to do this by letting us to specify number of bits we like to allocate for each data type (for eg. unsigned int:16; means i need an unsigned int of exactly 16bits (2 bytes long) -no more and no less).
    Is there anything similar i can do in Java?
    any suggestion is greately appreciated.
    Thanks,

    All primitive types in Java are well-defined. In contrast to C/C++, an int in Java is allways 32 bits, using one's complement, a char is allways 16 bits etc.
    Java does not give you direct acces to physical memory - that would compromise the basic design of Java.
    You can read individual bytes from files, and you can do all the integer arithmetic in Java as you can in C/C++. To convert eg. fout bytes to one int, you could do the following:
    byte b1= (some byte value),
         b2,
         b3,
         b4;
    int i= (((b1 << 8) | b2 << 8) | b3 << 8) | b4;
    b1 being the most significant byte, b4 being the least significant.
    Did this answer you questions?

  • Call a method with complex data type from a DLL file

    Hi,
    I have a win32 API with a dll file, and I am trying to call some methods from it in the labview. To do this, I used the import library wizard, and everything is working as expected. The only problem which I have is with a method with complex data type as return type (a vector). According to this link, import library wizard can not import methods with complex data type.
    The name of this method is this:   const std::vector< BlackfinInterfaces::Count > Counts ()
    where Count is a structure defined as below:
    struct Count
       Count() : countTime(0) {}
       std::vector<unsigned long> countLines;
       time_t countTime;
    It seems that I should manually use the Call Library Function Node. How can I configure parameters for the above method?

    You cannot configure Call Library Function Node to call this function.  LabVIEW has no way to pass a C++ class such as vector to a DLL.

  • ERROR OGG-01148 programming error, data type not supported for column

    I am getting following error when I put null in insert statement
    2011-03-31 18:30:45 ERROR OGG-01148 programming error, data type not supported for column TXID in table advoss.tblaudittrail.
    I am replicating MySQL 5.5.9 to Oracle 11g rel2 via goldengate 11

    I am able to diagnose what is cuasing the problem
    unsigned flag was the culprit of this error
    I am able to insert null after removing unsigned flag.
    thank you very much for your kind support

  • Custom Data Types

    Hi! I am just starting to build Java and I am having this problem.It's so much .doc about Java but I still can't find what I need. My problem:
    I should define(in a class or interface or something else) a custom data type like : uint16, or uint32, which means that instead of writing : int j; , I should be able to write : uint16 j; which means an unsigned integer(positive) on 16 bits(eg. short). And this not only in one file, but in any file I want to, and to be able to transport this through TCP. Thank you a lot if you can help me.

    Ok, you could use some API that provides uint16, uint32,... classes or you could write your own. But this is really not something you would ever do in practical use except perhaps for interoperability with other languages. In that case you will not be able to use +,-,*,/,==,!=,>,<, and so forth, due to the lack of operator overloading in Java. It is not supported in Java by good reasons.
    In my opinion here somebody did bad work in constructing an exercise for your training. :-)
    regards
    Sven

  • Use of type libraries and custom data types in TestStand 3.1

    Consider the following example in CVI 7.0:
    #include <cvidef.h>
    typedef struct
        int                         t_err;
        unsigned int                err_flags;
        int                         err_code;
        char                        err_msg[1024];
    } TEST_ERROR;
    int DLLEXPORT DLLSTDCALL   NfctConnect  (int iPortType, int iComPort, int iBaudRate, TEST_ERROR * tstError);
    I want to call this function from TestStand 3.1 using the CVI Standard Adapter.
    I created a function panel from the header file then I generated a DLL that contains the type library as an embedded resource. Using OLE/COM object viewer I can confirm that the definitions are correct. In TestStand however, all basic types are recognized but the structure is rejected as not being a TestStand type.
    However, a C structure is supposed to be assimilated as a container in TestStand. Do I have to manually create a custom data type in TestStand to match my C structure or TestStand can automatically create the container based on the information in the type library (because this information is available it won't be difficult for TestStand to do this automatically)?

    Hi Spoli,
    At the current time the only adapter that gives you the option to
    automatically create a container for a given parameter is the LabVIEW
    adapter. In your case you will need to manually create the container
    that matches your C struct. I apologize for the inconvenience.
    One thing you can do to see this feature in future versions is make a Product Suggestion on our website. Our R&D engineers are always looking for good feedback.
    Have a good one Spoli,
    Dan Weiland
    Applications Engineer
    National Instruments
    Dan Weiland

  • Date() function data type?

    What is the data type of the Date() function return value? I tried real, integer, unsigned integer, it does not match. The function reference in TestStand Help does not describe what is the return value data type. Please help.
    Thanks

    Hi Jwardojo,
    The return type is string
    Hoe this helps !
    Regards,
    Mathieu Steiner, Test System Engineer, Safran Engineering
    CLD, ISTQB

  • I am supppose to wire up a cluster to a callng library function thru the use of the "adapt to type" configuration.But the program lie in the data types in the struct.

    i have call library function that contain two arguement,two inputs i mean.The first input is simply an int which can be easily matched by labview data type.The second input is declared as a structure which i match it thru a cluster by using the adapt to type means.
    However,inside the structure there are four variables,two of which is PULONG and the other two is ULONG.Inside my cluster i can match the ULONG by a 32bit unsign integer labview data type but how can i match the PULONG with a labview data type?

    There is a great KnowledgeBase entry that discusses using the "adapt to type" option in a Call Library Function, and specifically using clusters.
    This KB, titled "I'm Having Trouble Accessing the Members of a Cluster Passed to a Shared Library Such as a DLL" is available at:
    http://digital.ni.com/public.nsf/3efedde4322fef19862567740067f3cc/bc2a65c0fe2868a1862566e80067c4e4?OpenDocument
    I found this article by going to www.ni.com, clicking on the KnowledgeBase link, and searching for "call library cluster."

  • Should Java introduce new primitive data types??

    In Java SE 5.0, many character- related methods (especially in the class Character) handle code points by using int type, i.e. return (code point) type is int, or receive (code point) int as parameter. This leads several problems. First, the variable used for storing returned result should be carefully stated, otherwise confusion may arise. Second, the parameters of method should be carefully ordered, otherwise conflict of method signature may meet.
    By those reasons, I suggest that Java should introduce new primitive data types for handling characters, they are:
    1. wchar
    this type states the variable stores 32-bit UNSIGNED integer, and it could be used to store either the code point, or the UTF-16 code of a character.
    2. ascii
    this type states the variable stores 8-bit UNSIGNED integer, and it could be used to store the code point of a elemental (ascii) character.

    short char, no. Nononono.
    No.
    wchar, I think I'd pref to see Java version 2 come
    out, and that be wchar by default, but that is not
    going to happen.
    I don't see a great need for it at this time.http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4504839
    Though I do not think that Java developer would like to change too much program code for using proposed (unsigned) primitive data types, I still believe that Java developer will eventually introduce these types, because it is not efficient to pretend signed integer to unsigned integer, and the success of a programming language is determined by how it can meet the programmers' need.

  • LV data types for C++ DLL

    I have created a DLL in VC++ that someone in my organization needs to call from LabView. The declarations for my exported functions are like this example:
    extern "C" __declspec(dllexport)int DLL_Funct(int portnum, char str_data[33], unsigned char csn[4]);
    The function above passes an integer and a null terminated 32 char string and gets back a 4 byte uchar value.
    The LV programmer is having trouble getting my DLL functions to work. I suspect it has to do with different data types or byte alignment or something like that.
    I have zero experience with LV. Can someone please tell me what the equivalent function declaration should be in LV and whatever else I should tell my associate that might help make his code work?
    Thanks.
    Dave
    DRC

    Do you know what the LabVIEW programmer is doing? The "str_data" string parameter should be of Type "string" in the parameters list for the Call Library Function Node setup. The "csn" can be passed as an array of unsigned 8-bit integers.

  • Profibus data type converting to floating point

    Hi, 
    Is there an efficient way to convert  the incoming data to the floating point in the NI cRIO Profibus system? 
    1) The system is : NI cRIO 9068 Controller with Comsoft profibus slave module. The lavview we are using is 2013. 
    2) We are using profibus slave example and are able to see a array of unsigned 8 bit data. Please see the Output data shown in the attachement.
    3) On the other side of profibus system, thrid party profibus master are converting floating points and tranmit the converted data to NI profibus slave. 
    Attachments:
    Screenshoot.png ‏7 KB

    If it's just a matter of converting data types once you have the data in LabVIEW, you can always manually scale and convert the data using the arithmetic functions and the "To Double Precision Float" or "To Single Precision Float" functions. You just have to know what floating point value the unsigned byte integer corresponds to. Is that what you're asking?
    If you're asking for a way to this inherently with the Profibus functions, I'm afraid I can't be of much help...
    Ryan K.

Maybe you are looking for

  • PB no longer recognizes VGA connection to projector

    I use my PowerBook with two different external displays. It works fine with the DVI connection to an external desktop monitor. It used to also work fine connecting to a classroom A/V system with a projector (using the DVI-VGA adaptor), but it has mos

  • Hi In ALE ,IDOC  HOW  CONTROL DATA IS TRANSFERRED

    Hi Experts,                       Hi In ALE ,IDOC  HOW  CONTROL DATA IS TRANSFERRED . Thanks & Regards Bhaskar Rao.M

  • CTRL + C, CTRL + V - copy paste

    Hi, I created one menu ... in that for copy & paste i given short as CTRL+V , CTRL + C. But the problem is when i do CTRL+C CTRL+V systems defaults copy paste executing how can i sove this. Thanks in advance JOe

  • WCCP redirect not working on Cat 3560

    We have a 3560 running 12.2(37)SE1, IP services image. Through debug, we can see WCCP communication betweeen the 3560 and our content engine (for web caching). However, web traffic isn't being redirected to the CE at all. Instead, it goes straight ou

  • Photostream not importing correct photos

    When I highlight and click to import photos from my phone to iPhoto from the Photostream, a set of flag icons start importing.  What's happening and how do I stop this?