byte

how to take twos complement of a byte in c++

故事扮演 提交于 2019-12-18 04:14:55
问题 I am looking at some c++ code and I see: byte b = someByteValue; // take twos complement byte TwosComplement = -b; Is this code taking the twos complement of b? If not, What is it doing? 回答1: This code definitely does compute the twos-complement of an 8-bit binary number, on any implementation where stdint.h defines uint8_t : #include <stdint.h> uint8_t twos_complement(uint8_t val) { return -(unsigned int)val; } That is because, if uint8_t is available, it must be an unsigned type that is

How do I convert a byte array to UIImage

China☆狼群 提交于 2019-12-18 03:03:31
问题 I have a webservice which sends me back an array of bytes like this: <ax23:IMGBLOB>-119,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,100,0,0,0,106,8,3,0,0,0,125,54,4,22,0,0,0,51,80,76,84,69,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-73,-107,-30,-126,0,0,0,16,116,82,78,83,0,16,32,48,64,80,96,112,-128,-112,-96,-80,-64,-48,-32,-16,84,-32,-88,-56,0,0,2,126,73,68,65,84,120,-38,-19,90,65,

bitwise-ANDing with 0xff is important?

 ̄綄美尐妖づ 提交于 2019-12-17 23:39:39
问题 Doesn't bitwise-ANDing with 0xff essentially mean getting the same value back, for that matter, in this code? byte[] packet = reader.readPacket(); short sh; sh = packet[1]; sh &= 0xFF; System.out.print(sh+" "); Weirdly, I get a -1 if that ANDing is not included but a 255 when included Could someone explain the reason? As I see it 0xff is just 1111 1111. Isn't it? 回答1: Yes, 0xff is just 1111 1111 . But this is attempting to display the unsigned byte value, even though in Java byte s are signed

How to create a bmp file from byte[] in C#

夙愿已清 提交于 2019-12-17 22:16:58
问题 I have a byte[] array received in TCP Client.The array contains a 24-bit RGB Bitmap file.How to create that bitmap file with given Width ,Height and data? In C++ I use this int WriteBitmapFile(const char *filename, int width, int height, unsigned char *imageData) { FILE *filePtr; // file pointer BITMAPFILEHEADER bitmapFileHeader; // bitmap file header BITMAPINFOHEADER bitmapInfoHeader; // bitmap info header DWORD imageIdx; // used for swapping RGB->BGR unsigned char tempRGB; // used for

What happens when you cast from short to byte in C#?

拈花ヽ惹草 提交于 2019-12-17 21:29:25
问题 I have the following code: short myShort = 23948; byte myByte = (byte)myShort; Now I wasn't expecting myByte to contain the value 23948. I would have guessed that it would contain 255 (I believe the largest value for a byte). However, it contains 140, and it made me wonder why; what is actually going on behind the scenes? Please note that I am not looking for someone to solve the problem that 23948 cannot fit into a byte, I am merely wondering about the underlying implementation 回答1: Short is

stream a byte[] to load inside a jquery modal as PDF (MVC3)

笑着哭i 提交于 2019-12-17 21:12:03
问题 I am trying to stream a byte[] to load inside a jquery modal when a certain link is clicked using MVC3. In my controller I have public ActionResult GetTermsAndCondtion() { byte[] termsPdf = GetTermsAndConditions(DateTime.Now); return new FileContentResult(termsPdf, "application/pdf"); } In one of my view I have @Html.ActionLink("Terms and Conditon","GetTermsAndCondtion","Customer", new{id="terms"}) This opens the pdf file in a tab. But I want to open the byte[] as pdf file inside a modal. Any

BigInteger to byte[]

我的梦境 提交于 2019-12-17 19:33:27
问题 I need to convert a Java BigInteger instance to its value in bytes. From the API, I get this method toByteArray() , that returns a byte[] containing the two's-complement representation of this BigInteger. Since all my numbers are positive 128 bits (16 bytes) integer, I don't need the 2's-complement form that give me 128 bits + sign bit (129 bits)... Is there a way to get the standard (without the 2's-complement form) representation directly from a BigInteger? If not, how can I right shift the

How many bytes will a string take up?

江枫思渺然 提交于 2019-12-17 18:18:14
问题 Can anyone tell me how many bytes the below string will take up? string abc = "a"; 回答1: From my article on strings: In the current implementation at least, strings take up 20+(n/2)*4 bytes (rounding the value of n/2 down), where n is the number of characters in the string. The string type is unusual in that the size of the object itself varies. The only other classes which do this (as far as I know) are arrays. Essentially, a string is a character array in memory, plus the length of the array

Java Iterate Bits in Byte Array

风流意气都作罢 提交于 2019-12-17 17:39:34
问题 How can i iterate bits in a byte array? 回答1: You'd have to write your own implementation of Iterable<Boolean> which took an array of bytes, and then created Iterator<Boolean> values which remembered the current index into the byte array and the current index within the current byte. Then a utility method like this would come in handy: private static Boolean isBitSet(byte b, int bit) { return (b & (1 << bit)) != 0; } (where bit ranges from 0 to 7). Each time next() was called you'd have to

Converting 2 bytes to Short in C#

懵懂的女人 提交于 2019-12-17 16:43:13
问题 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