short

Java - Explicit Conversion from Int to Short

瘦欲@ 提交于 2019-11-28 09:20:38
Can someone please explain why this following statement: short value = (short) 100000000; System.out.println(value); Gives me: -7936 Knowing that the maximum value of a short in Java is 32767 correct? With your value of 100 million, I get -7936. I can only get 16960 if I change 100 million to 1 million. The reason is that short values are limited to -32768 to +32767, and Java only keeps the least significant 16 bits when casting to a short (a narrowing primitive conversion, JLS 5.1.3 ). Effectively this operation: 1 million mod 2^16 (16 bits in a short ) is 16960. The way you did it merely

convert from short to byte and viceversa in Java

陌路散爱 提交于 2019-11-28 08:57:23
问题 I'm trying to convert a short into 2 bytes...and then from those 2 bytes try to get the same short value. For that, I've written this code: short oldshort = 700; byte 333= (byte) (oldshort); byte byte2= (byte) ((oldshort >> 8) & 0xff); short newshort = (short) ((byte2 << 8) + byte1); System.out.println(oldshort); System.out.println(newshort); For the value of 700 (oldshort), newhosrt is 444. After some testing, it looksl ike \tThis code only works for some values. Like...if oldshort=50, then

Java: Different outputs when add/remove short and integer elements in a Set

删除回忆录丶 提交于 2019-11-28 02:08:29
I wasn't sure on how to ask this question. But, what is the different between these 2 lines of code? Set<Integer> a = new HashSet<Integer>(); for (int i = 0; i < 100; i++) { a.add(i); a.remove(i - 1); } System.out.println(a.size()); I expected 99 to be the output Output is 1 Set<Short> a = new HashSet<Short>(); for (Short i = 0; i < 100; i++) { a.add(i); a.remove(i - 1); } System.out.println(a.size()); I expected 99 to be the output Output is 100 The type of the expression i - 1 is int because all operands in an integer arithmetic expression are widened to at least int . Set<Short> has add

Converting 2 bytes to Short in C#

 ̄綄美尐妖づ 提交于 2019-11-28 00:39:14
I'm trying to convert two bytes into an unsigned short so I can retrieve the actual server port value. I'm basing it off from this protocol specification under Reply Format. I tried using BitConverter.ToUint16() for this, but the problem is, it doesn't seem to throw the expected value. See below for a sample implementation: int bytesRead = 0; while (bytesRead < ms.Length) { int first = ms.ReadByte() & 0xFF; int second = ms.ReadByte() & 0xFF; int third = ms.ReadByte() & 0xFF; int fourth = ms.ReadByte() & 0xFF; int port1 = ms.ReadByte(); int port2 = ms.ReadByte(); int actualPort = BitConverter

Convert hexadecimal string with leading “0x” to signed short in C++?

十年热恋 提交于 2019-11-27 15:51:24
I found the code to convert a hexadecimal string into a signed int using strtol , but I can't find something for a short int (2 bytes). Here' my piece of code : while (!sCurrentFile.eof() ) { getline (sCurrentFile,currentString); sOutputFile<<strtol(currentString.c_str(),NULL,16)<<endl; } My idea is to read a file with 2 bytes wide values (like 0xFFEE), convert it to signed int and write the result in an output file. Execution speed is not an issue. I could find some ways to avoid the problem, but I'd like to use a "one line" solution, so maybe you can help for this :) Edit : The files look

Convert short[] to Stream which can be played as Audio

99封情书 提交于 2019-11-27 15:25:21
问题 so what I have is a short[] array which represents the raw data of a WAV file. This means it doesn't include any header or footer information which is usually included. In order to play this audio, I need to convert it to a stream of some sort, unfortunately the data in this short[] array is Int16 and many of the values exceed 255, therefore cannot be converted to bytes and cannot be converted to a stream. Does anyone have any idea how I would be able to play this audio data? 回答1: You can

php: number only hash?

落花浮王杯 提交于 2019-11-27 12:36:58
In php is there a way to give a unique hash from a string, but that the hash was made up from numbers only? example: return md5(234); // returns 098f6bcd4621d373cade4e832627b4f6 but I need return numhash(234); // returns 00978902923102372190 (20 numbers only) the problem here is that I want the hashing to be short. edit: OK let me explain the back story here. I have a site that has a ID for every registered person, also I need a ID for the person to use and exchange (hence it can't be too long), so far the ID numbering has been 00001, 00002, 00003 etc... this makes some people look more

Really simple short string compression

杀马特。学长 韩版系。学妹 提交于 2019-11-27 08:51:44
Is there a really simple compression technique for strings up to about 255 characters in length (yes, I'm compressing URLs )? I am not concerned with the strength of compression - I am looking for something that performs very well and is quick to implement. I would like something simpler than SharpZipLib : something that can be implemented with a couple of short methods. I think the key question here is " Why do you want to compress URLs? " Trying to shorten long urls for the address bar? You're better storing the original URL somewhere (database, text file ...) alongside a hashcode of the non

Why do I need to explicitly cast char primitives on byte and short?

帅比萌擦擦* 提交于 2019-11-27 08:15:10
问题 Concerning primitives: When I cast from smaller to bigger types, the casts are implicit, when I cast from bigger to smaller types, I need to explicitly cast the primitives, that's clear due to loss of data. But there is something I don't get. When I up- or downcast to char in some cases (byte and short), I always need to explicitly cast in both directions although byte (8bit) fits into char (16bit)? (see also http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) See my

Overflowing Short in java

左心房为你撑大大i 提交于 2019-11-27 05:37:49
I have one question about the short data type in Java. I know that the range for short is between -32768 to 32767. So, if I tried to add two short values that exceed the range, the result ends up being the supposed total minus either the positive range or the negative range times 2, as the following: short a = 30000; a = (short) (a+a); the result is -5536 . So the math is 32768 + 32768 = 65536 , 6000 - 65536 = -5536 . I know what it does, but I don't know why it does it this way. Can anybody explain the logic or why Java is doing it this way? What's happening is that your number is wrapping