Unsigned Long in java
Hi all,
I have a code in C which i want to convert into java.. Iam facing problem because java does not support
unsigned long.The code in c look like this:
WCD.cardno = ( SerBuf[3]* 0x00010000UL+
SerBuf[4]* 0x00000100UL+
SerBuf[5]* 0x00000001UL);
I want to convert this code into java. What i have done is something like this
cardno = (tempBuffer[3]*0x01000000 + tempBuffer[4]*0x00010000 + tempBuffer[5]*0x00000100 +tempBuffer[6]*0x00000001);
Can anyone help me out. This program is for serial port implementation.
Thanks
Kiran
kiranJNI wrote:
Basically how can we achieve unsigned long in java???In your case, this isn't going to be a problem for two reasons.
AFAIK, there aren't any numbers where multiplying them as unsigned numbers is going to be different from multiplying them as signed numbers and still give a valid result. To make this easier, let's work with signed vs unsigned bytes. Negative numbers are those in the range 0x80 to 0xFF; these are the only numbers where the "value" of the byte differs between signed and unsigned. Now think of those numbers as unsigned numbers. The only unsigned numbers you could multiply them by and still remain within the range of a byte are 0 and 1. If you multiply by 2, the result is 0x100, which is larger than what a byte can represent. If the result will remain within the range of the data type, it don't think there are any numbers where signed and unsigned multiplication return a different bit pattern.
Furthermore, your example doesn't even need multiplication. You're multiplying by powers of 2, so all you really need is a series of shifts and adds.
Similar Messages
-
Java newbie help (type casting, 64bit unsigned Long)
Hi I am java newbie and need help on my project. I have a few questions. Can you put strings in a hashtable and test for their being their with the appropriate hashtable method? I want to test for equal strings, not the same object. Second question can you use all 64 bits of an unsigned long? java doesn't seem to allow this. Any packages that do?
Thanks,
DaveTry casting it to Long instead of long. Long (capital L) is an Object, while long (lower case l) is not. You may also check to make sure the value isn't null. I would have thought that autoboxing would have worked here unless the value was null. But I am no expert on autoboxing.
Edit >> Checking for null ain't a bad idea but has nothing to do with the problem - this is a compile time problem. Sorry.
Also>> This code should work:
long cTime=(Long)session.getAttribute("creationtime");Edited by: stevejluke on Jul 1, 2008 11:00 AM -
Hi,
I have a Oracle field configured as NUMBER(38). The number in that field gets incremented sequentially and is currently around 5345232341.
I am using callableStatement.registerOutParameter(1,java.sql.Types.INTEGER); which is working fine.
But the problem happens when I am trying to retrieve the value and store it within my Program.
I am using 'long' to do that but I presume I will hit a problem when the number crosses 9223372036854775807 (2^63-1). As we don't have unsigned long option in Java(unlike in C/C++), how do we achieve this? Please can somebpdy tell me the workaround.
Currently, I am storing it as follows:
long my_number = callableStatement.getInt(1);Currently, I am storing it as follows:
long my_number = callableStatement.getInt(1);While reading the docs, you should probably also read about getInt(), because your current code is buggy. -
Unsigned Long declaration in JAVA ?????
Hi,
Java doesnt allow unsigned declaration.
I have a 'C' program which does the following:
unsigned long someLongInteger; //in C
someLong>>18;
SomeLong<<14;
SomeLong>>=14;
When I did the same thing in JAVA by declaring someLongInteger as long, it gives me a different result.
Is that because it is considering java Long as signed??
Is there any work around this?
I appreciate your reply
Thank you.Depending on what you are trying to do, the unsigned right shift operator (>>>) may be of use for this situation.
- K
Hi,
Java doesnt allow unsigned declaration.
I have a 'C' program which does the following:
unsigned long someLongInteger; //in C
someLong>>18;
SomeLong<<14;
SomeLong>>=14;
When I did the same thing in JAVA by declaring
someLongInteger as long, it gives me a different
result.
Is that because it is considering java Long as
signed??
Is there any work around this?
I appreciate your reply
Thank you. -
Converting jlong to unsigned long
Hi Everyone,
I am calling a interface from a dll file which need a unsigned long or long as a parameter. In the C program side, how can I convert the jlong to unsigned long or long and then call the interface from the dll file? Also, how can I convert the long back to jlong and return back to Java side? Thank you.
JackyStraight cast - BigInteger would be overkill.
-
Parsing binary file- unsigned longs
Hello everyone.
I'm currently trying to write a quick parser for a binary file format (ASF). However, java's handling of bit shifting and lack of unsigned types is driving me completely mad.
How can I represent an unsigned long?
How can I set the bits for it anyway? It seems that the shift operator can't shift more than an int's worth (it just loops around and starts adding to the other bits- weird).
Thanks in advance.
Simonejp wrote:
But why in the world does the following code also do nothing?
long x = 45 << 32;
Try long x = 45L << 32;
The answer appears to be that java uses ints to represent all constants, but this presents some serious problems- namely- how on earth do you get a number bigger than an int into a long?With 'L'. Same as in C or C++. In Visual Basic it's '&'. There's generally something.Where did that come from? Why have I never seen anything like that before?
If I do long x = 0x7FFFFFFF; all is well, but if I do long x = 0x80000000; I get FFFFFFFF80000000, which isn't what I asked for.Actually it is exactly what you asked for. You've overlooked several facts: (i) Java longs are 64 bits; (ii) you specified a negative 32-bit constant; (iii) the rules of every programming language I can think of provide for sign-extension when going from a shorter to a longer type.Right. It makes sense now that I know how to specify long constants.
As someone pointed out signed/unsigned is actually the same, so long as you interpret it correctly. So to avoid the total stupidity of using twice as much memory for everything I've decided that I am actually going to use the correct types.They're not the correct types. As I pointed out in an earlier post, your 'unsigned longs' must be coming from C or C++ where they are generally 32 bits, so using a 64-bit Java long is incorrect.Where they came from doesn't matter. The spec doesn't say it's a "long"- it says that this particular value is 64bit, and that all values are unsigned. So I do need a Java long.
WHY IN THE WORLD IS JAVA "INTELLIGENT" WHEN DOING THINGS BITWISE? WHICH BRAIN DEAD IDIOT THOUGHT THAT UP? That is broken and is asking for trouble.It is you who is asking for trouble here. The rules of Java are consistent and in conformity with the practice in other programming languages. You've just overlooked several relevant facts.I think I've worked out where I was going wrong here. When doing something like
int i;
long x;
x = x | i;The i is converted to a long before the bitwise operation, so it's not the bitwise operation that's the problem, it's the conversion between int and long?
It's not Java whose stupidity is the issue here ;-)That wouldn't surprise me.
Thanks. -
hi there,
i was wondering if there is any way to handle unsigned int in java. im making some byte processing and i have a "switch case" statement like below
so when it comes at 0x80 it does not enter the case because it converts it as -1.
im using java 1.7
ByteBuffer record;
pkgId = record.get();
switch ((int) pkgId) {
case 0x75:
break; /* 117 */
case 0x7D:
break; /* 125 */
case 0x80:
break; /* 128 */
case 0x82:
break; /* 130 */
case 0x86:
break; /* 134 */
case 0x87:
break; /* 135 */
}user8999602 wrote:
hi there,
i was wondering if there is any way to handle unsigned int in java.Use a long.
im making some byte processing and i have a "switch case" statement like below
so when it comes at 0x80 it does not enter the case because it converts it as -1.
im using java 1.7
ByteBuffer record;
pkgId = record.get();
switch ((int) pkgId) {
case 0x75:
break; /* 117 */
case 0x7D:
break; /* 125 */
case 0x80:
break; /* 128 */
case 0x82:
break; /* 130 */
case 0x86:
break; /* 134 */
case 0x87:
break; /* 135 */
}Looks more like you want an unsigned byte. The usual approach to that is to use an int, but you need to mask it. The byte 0x80 has the value -128, so when you simply cast to an int, it sign extends it, and you get 0xFFFFFF80, which is an int value of -128.
int pkgIdInt = pkgId & 0xFF;
switch (pkgIdInt);Alternatively, you could just cast each case value to byte, but that's too cluttered for my taste. -
Why is there no unsigned type is java?
Just wondering? Why no unsigned int or unsigned long? I have often used these in the past but java requires me to do some clever stuff to get over the limitation.
i found this on the net somewhere. it doesn't mention anything about java but it does seem to fit the typical "don't include anything that can be confusing or misunderstood" mold that java utilizes:
"One problem is that unsigned types tend to decrease your
ability to detect common programming errors. Another is that
they often increase the likelihood that clients of your classes
will use the classes incorrectly.
Consider first error detection. Suppose a programmer defines
an Array object as follows:
int f(); // f and g are functions that return
int g(); // ints; what they do is unimportant
Array<double> a(f()-g()); // array size is f()-g()
There�s nothing wrong with this definition for a, except for the
possibility that the programmer writing it made an error. Instead
of the size of the array being f()-g(), perhaps it
should have been g()-f(). Or maybe it should have been
f()+g(). Or possibly it should have been f()-g()+1; off-byone
errors are certainly common enough. Heck, it�s even possible
that f()-g() is correct, but f() or g() is returning a
bad value. Any of these scenarios could lead to the Array
constructor being passed a size that was less than zero. As
the author of the Array class, there�s no way you can keep
clients from making mistakes such as these, but you can do
the next best thing: you can detect such errors and act on
them.
Well, actually, maybe you can�t. You can�t if you declared Array�s
constructor to take an unsigned value, because if a
negative number is passed to a function as an unsigned, the
number seen by the function isn�t negative at all. Instead, it�s
a very large positive number. As a result, you�d have no way
within the Array constructor of distinguishing between a
large, but valid, positive size value and a large, but invalid,
positive size value arising from passing in a negative number.
Hence, you�d be unable to detect programming errors that result
in negative array sizes being requested. Because such errors
are not uncommon, this makes your class easy to use
incorrectly." -
Signature:
FSUpdateOperationStatus(void const*, TCountedPtr<TCFURLInfo> const&, long long, long long, long long, long long, unsigned long)
No Support Docs on File.
Firefox crashes every time it is opened.
UUID 38fc1438-492f-4ce3-91d4-5ef922101027
Time 2010-10-27 11:19:32.620395
Uptime 11
Last Crash 110 seconds before submission
Install Age 610295 seconds (1.0 weeks) since version was first installed.
Product Firefox
Version 3.6.11
Build ID 20101012104758
Branch 1.9.2
OS Mac OS X
OS Version 10.6.4 10F569
CPU x86
CPU Info GenuineIntel family 6 model 15 stepping 11
Crash Reason EXC_BAD_ACCESS / KERN_PROTECTION_FAILURE
Crash Address 0x8
User Comments
Processor Notes
EMCheckCompatibility FalLooking at the crash log it looks like AE might be crashing in:
net.telestream.wmv.export
Can you try uinstalling Flip4Mac (or whatever you have installed from http://www.telestream.net/telestream-products/desktop-products.htm)? You may need to manually go deep into /System to remove them.
--c -
Converting a Wide String to unsigned long long?
Hi there,
I am looking for a way to convert a string to unsigned long long on
Solaris. On AIX and Linux, there is wcstoull. However, I do not find
that in solaris. So, I plan to use swscanf ( str, L"%llu", &val).
However, running thru Rational Purify, each call to swscanf leaks 1
byte.
Anyone has a 3rd solution other than the two above? Or maybe there
is fix to the swscanf leak?
Thanks in advance,
Miranda% man -s 3C strtoull
-
How to specify in TS unsigned long? unsigned short?
Hi,
I'm lusing a c/c++ step type from TestStand.
I need to use a structure whose fields according to the h file of the dll are:
unsigned char
unsigned long
unsigned short
Now, when I define a container in TestStand (to interface with the dll structure) my options are:
integer
unsigned integer
etc...
What am I supposed to do ? what is the appropriate numeric format?
ThanksHi Doug,
I'm given the dll and load of h files. The definitions you mentioned are not shown...
Suppose I want to creat in TS a container to match variable FCPortConfig. This variable structure is given here....
typedef struct tagFCPortConfig
unsigned char ucTopology; /* For Point-To-Point use TOPOLOGY_PT_2_PT
and for LOOP Topology use TOPOLOGY_LOOP */
unsigned char ucSpeed; /* SPEED_1GHZ, SPEED_2GHZ */
unsigned char ucDisableTimer; /* (Not Currently Used) enable or disable
timeout timer */
unsigned long ulRRDYGap; /* (Not Currently Used) up to a 27-bit val */
unsigned long ulRRDYGapRandomEnable;/* (Not Currently Used)enable/disable */
unsigned long ulRRDYGapSeed; /* (Not Currently Used) If random set;
a 27-bit val */
unsigned long ulRRDYGapMin; /* (Not Currently Used) If random set;
a 27-bit val */
unsigned long ulRRDYGapMax; /* (Not Currently Used) If random set;
a 27-bit val */
unsigned char ucAutoNegotiate; /* (Not Currently Used) enable or disable
auto negotiation */
unsigned short uiBBCreditConfigRx; /* BB_credit this port will advertise */
unsigned char ucStatsMode; /* Determines weather or not to retrieve
the extended stats counters, use
STATS_MODE_NORMAL & STATS_MODE_EXTENDED */
unsigned char ucReserved[15]; /* Reserved */
} FCPortConfig;
1) Is there a way to find out how they define unsigned char, unsigned short and unsigned long?
2) Can I still use the TS default numeric type?
Thanks
Rafi -
How to express the unsigned byte in Java?
we know that data are normally in unsigned byte format when communicating with COMM. How to express the unsigned byte in Java? java only supports 127 ~ -127 as byte, but I need 255~0.
Anyone know how?
Thanks!You mean when a byte ( -127) converts to a int, it
will become 255?In your example -1 will be printed in both cases because of the implicit conversion Java makes. The 0xff in the byte will become 0xffffffff in the int. Both are interpreted as -1.
This will keep the byte bitpattern intact and print -1 and 255.
byte b = (byte)(0xff);
int i = (b & 0xff); // mask of rightmost 8 bits
System.out.println(i); -
How to map C/C++ unsigned char[] to Java
hi all,
I'm using w2k OS.
Given that C code:
BYTE *fBuf;
fBuf = new BYTE[256];
Is there anyone of you know how to pass/map the unsigned char to java?
regards
elviswhy did you classify this as byte? how do you did
that?They probably guessed. It is probably a good guess.
You can use the following to determine the size exactly.
First determine what "BYTE" is exactly. You will have to find that in an include file somewhere. Your IDE might do this for you automatically.
So, for example you might find the following...
typedef unsigned char BYTE;
So then you would know that the type is actually "unsigned char".
Once you have this information you then look in limits.h (this is an ANSI C/C++ file so it will exist somewhere.) In it you find the "..._BIT" that corresponds to the type. For the type given above you would be looking for "CHAR_BIT" (because unsigned and signed chars are the same size.)
On my system that would be...
#define CHAR_BIT 8
That tells you that there are 8 bits in the BYTE value. So now you need to find a java type that also has at least 8 bits. And the java "byte" value does. -
Taking an array of unsigned word an using it in a subVI that accepts an unsigned long
I have a user specified sized array that is in unsigned word(16bit). I need to use a subVI, but the subVI only accepts unsigned long(32bit). I was wondering if anyone would know how to make this work.
Hi Amy;
Unless I am missing something, there should not be any problem inside LabVIEW.
To play it safe and keep LabVIEW performance up, convert the array using the "To Unsigned Long Integer" vi located in the function palette:
Numeric -> Conversion
Regards;
Enrique
www.vartortech.com -
Should using unsigned long long be this difficult?
So I have a number. Possibly a big number that I would like to use. When I do the following, valueToGet is fine (1252904880010) when it returns but when I step to currentTimeStamp the value is something like 18446744072483981194.
I can only assume that maybe I am missing some basic understanding of how methods return this value.
-(void)processTimestampReceived
NSString* timeString = @"1252904880010";
unsigned long long currentTimestamp = [self getUnsignedLongLong: timeString];
NSLog(@"currentTimeStamp (converted from timeString): %qu", currentTimestamp);
-(unsigned long long)getUnsignedLongLong:(NSString*)longString
NSScanner* scanner = [NSScanner scannerWithString:longString];
unsigned long long valueToGet;
if([scanner scanLongLong:&valueToGet] == YES) {
return valueToGet;
return -1;That is so discouraging that code that SHOULD work actually does work for everyone but me. Here is the data after I changed the following code. I had to use scanHexLongLong to actually return an unsigned long long instead of just long long. Is it possible that maybe something is just stomping on that memory location somehow and giving me bad data? But consistently the same way? Seems odd.
2009-09-14 02:36:37.325 TestApp[46744:20b] lVal=322333076619280
2009-09-14 02:36:37.530 TestApp[46744:20b] currentTimeStamp (converted from timeString): 322333076619280
Run using:
[self processTimestampReceived];
unsigned long long lVal = [self getUnsignedLongLong:@"1252904880010"];
NSLog(@"lVal=%qu", lVal);
-(void)processTimestampReceived
NSString* timeString = @"1252904880010";
unsigned long long currentTimestamp = [self getUnsignedLongLong: timeString];
NSLog(@"currentTimeStamp (converted from timeString): %qu", currentTimestamp);
-(unsigned long long)getUnsignedLongLong:(NSString*)longString
NSScanner* scanner = [NSScanner scannerWithString:longString];
unsigned long long valueToGet;
if([scanner scanHexLongLong:&valueToGet] == YES) {
return valueToGet;
return -1;
Maybe you are looking for
-
Using OR condition in RTF Template
Hi, How to use OR condition in RTF? say for example,consider the EMP table. I want to show the employees of department 10 and 20 in my XML Publisher report. How can i write -- if deptno = 10 or deptno =20 in RTF template
-
Is it possible to send data to ITS via POST?
Hi guys! I need to call SAP transaction via thin client and I need to make a call via POST, cos' I need to send there some data to process. Is it possible? Thanx, Peter
-
Portal Runtime Error When Previewing the BI Report iViews
Hi Experts, I hope this is the right place for my issue. I have an issue previewing the BI reports that I have created as the iViews and have put them in the portal. I am able to preview them in the BW/BI server successfully. Then I have created them
-
No video or startup after chime - could it be...
a few days ago, while away from home, my pb would only chime and then not startup. you can hear a small chirp of disk activity, but then only the fan. the only way out was to hold the power button and power down. i tried everything, zapping the pram,
-
Looking for a Web Interface for Editing Data in an Oracle 10g Database
Greetings all! My organization is implementing Oracle 10g Release 2 Enterprise version. We have an employee who will need to edit data in the database which will hold staff phone numbers, email addresses, and other contact information. This employee