In java the char data type is 16 bit's = 2 bytes. In C the char data type is 8 bit's = 1 byte.
My problem is I am converting some C library structures into java classes. And I need a java data type that is equivalent to C char data type.
C char data type is equal to an unsigned byte. However java only has signed byte not unsigned byte.
I am wondering since java byte size is between -128 to 127. And I am getting error's/warning's when I do this in java
byte b = 0xff ; //out of range and when I cast possible lose of data Sure I can do this (byte)0xff But what would this give me? Another words does the cast lose any thing?
I need a data type that has size 0 to 255 like unsigned bytes = 8 bit char do.
The reason is I am coding a cd/dvd burning application and need to write the volume descriptors out to a file. Most of the volume descriptors are bytes. But I am unsure about if the signed or unsigned issue will play a role in making a correct iso file. My C library had used alot of char to represent the field's in the descriptor.
Like char c = 0xff ; could I just use byte c = (byte)0xff for java? I am not sure even if the range is an issue. I would think that if the byte 0xff is written out to the file regardless of what it represents (i.e a negative or positive number wouldn't matter just the fact that it is the correct value and it takes up the correct amount of space)
You are right that Java does not have any unsigned 8 bit types. In fact, there is only one unsigned type: char. But that's 16 bit as you already mentioned.
You can still use byte however. Any byte value between 0 and 127 will map perfectly to C unsigned char values between 0 and 127. Byte values between -128 and -1 will map perfectly to unsigned char values between 128 and 255. This because of how they are represented in binary.
So in the end, you can match them one on one perfectly. All you need to do is turn the Java byte into an int (or short) and add 256 (2^8) if you want to view them as unsigned values. [ September 10, 2008: Message edited by: Rob Prime ]
This what I thought. But why do we have signed or unsigned types. Are they just for convince. Because 0xff = -1 (signed) = 255 (unsigned)
But we could always do the convertion
To get the number value for the corosponding unsigned byte. Except now it is not a byte size but in this case we have the byte value and i is just what the value of the byte unsigned would be.
So why in some languages such as C/C++ do they give you the option of signed or unsigned data types is this just for convinence. Because I would think you could always convert to get the corosponding value like above.
Question When I read/write the bytes out to a file I am wondering what classes I should use to make it correctly work. Should I use BufferedInputStream , FileInputStream,...etc etc which one or does it even matter provided I use the equivalent readByte writeByte method? For performance which is the best?
Thanks for your help.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com