byte

signed short to byte in c++

断了今生、忘了曾经 提交于 2019-12-19 12:21:47
问题 I'm trying to convert a HEX number into a short (2 Bytes) using C++ everything is OK except for one thing... signed conversion from short to Byte (last test) i found this question and couldn't really benefit from it: portable signed/unsigned byte cast,C++ here are my tests: // test 1 - positive B2Short (success) byte *b = new byte[2]; b[0] = 0x10; //low byte b[1] = 0x00; //heigh byte signed short test = 0; test = ByteToShort(b); cout << test << endl; // test 2 - negative B2Short (success) b[0

C# - Remove Bitmap padding

浪子不回头ぞ 提交于 2019-12-19 11:16:07
问题 I was wondering if there's a way in order to remove the padding generated by the 24 bit Bitmap for each scan line. What I mean is like this : Original [Pure Cyan 24 Bit BMP] : FF FF 00 FF FF 00 FF FF **00 00** FF FF 00 FF FF 00 FF FF 00 Desired output [Removed Padding] : FF FF 00 FF FF 00 FF FF **00** FF FF 00 FF FF 00 FF FF 00 Here's my code for getting the pixel data. Bitmap tmp_bitmap = BitmapFromFile; Rectangle rect = new Rectangle(0, 0, tmp_bitmap.Width, tmp_bitmap.Height); System

How to convert tuple to byte array in c++11

被刻印的时光 ゝ 提交于 2019-12-19 10:00:45
问题 I need to write a function to convert tuple to byte array. The type of tuple may include int, long, double, std::string, char* ,etc. The size and type of tuple are arbitrary, such as std:tuple<string, int, double> t1("abc", 1, 1.3); or std:tuple<char*, int, int, float, double, string> t2("abc", 1, 2, 1.3, 1.4, "hello"); I want use these tuple as input, and the byte array as return value. What should I do ? 回答1: There is also the brilliant C++ API for message pack which supports tuples

Changing a specific byte in a file

荒凉一梦 提交于 2019-12-19 07:54:41
问题 I am trying to write a java function that will change 1 byte in a large file. How can I read in and write to a specific address in a file with java on android? I have tried fis.read(byte b[], int off, int len) and I get a force close every time. 回答1: Use RandomAccessFile. Kickoff example: RandomAccessFile raf = new RandomAccessFile(file, "rw"); try { raf.seek(5); // Go to byte at offset position 5. raf.write(70); // Write byte 70 (overwrites original byte at this offset). } finally { raf

java byte data type

吃可爱长大的小学妹 提交于 2019-12-19 07:04:28
问题 In Sun' tutorial it says about a byte: byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation. How does it save memory? What is 2's

Convert byte string to bytes or bytearray

纵饮孤独 提交于 2019-12-19 05:47:28
问题 I have a string as follows: b'\x00\x00\x00\x00\x07\x80\x00\x03' How can I convert this to an array of bytes? ... and back to a string from the bytes? 回答1: in python 3: >>> a=b'\x00\x00\x00\x00\x07\x80\x00\x03' >>> b = list(a) >>> b [0, 0, 0, 0, 7, 128, 0, 3] >>> c = bytes(b) >>> c b'\x00\x00\x00\x00\x07\x80\x00\x03' >>> 回答2: From string to array of bytes: a = bytearray.fromhex('00 00 00 00 07 80 00 03') or a = bytearray(b'\x00\x00\x00\x00\x07\x80\x00\x03') and back to string: key = ''.join

bytes vs bytearray in Python 2.6 and 3

天大地大妈咪最大 提交于 2019-12-19 05:12:55
问题 I'm experimenting with bytes vs bytearray in Python 2.6. I don't understand the reason for some differences. A bytes iterator returns strings: for i in bytes(b"hi"): print(type(i)) Gives: <type 'str'> <type 'str'> But a bytearray iterator returns int s: for i in bytearray(b"hi"): print(type(i)) Gives: <type 'int'> <type 'int'> Why the difference? I'd like to write code that will translate well into Python 3. So, is the situation the same in Python 3? 回答1: In Python 2.6 bytes is merely an

Java - bit shifting with integers and bytes

烈酒焚心 提交于 2019-12-19 04:42:21
问题 Consider the following code (where byteIndex is an int): int bitNumber = b-(8*byteIndex); bitMask = 0x8>>(byte)bitNumber; This generates the error error: possible loss of precision when compiled (required byte, found int). The code int bitNumber = b-(8*byteIndex); bitMask = 0x8>>2; compiles fine. What is the problem here and how do I fix the first example to allow bit shifting by the int value? EDIT: Following the comments, here is a more-complete example: 48) int byteIndex; 49) byte bitMask;

Using the == operator to compare a char to 0x80 always results in false?

徘徊边缘 提交于 2019-12-19 04:05:05
问题 char byte = 0x80 if(byte == 0x80) { cout << "This message never gets printed!"; } The hexadecimal value 0x80 is equivalent in binary to 1000 0000 , which clearly fits in a byte. However, the compiler warns me about the line with the conditional: warning: comparison is always false due to limited range of data type Why is the result of the conditional false in this case? Is 0x80 getting expanded in the conditional to something like 0x80000000 ? Is it possible to use the == operator to check if

How to convert byte array to any type

不问归期 提交于 2019-12-18 18:55:14
问题 okay guys I'm seeing question from persons asking how to convert byte arrays to int , string , Stream , etc... and the answers to which are all varying and I have personally not found any satisfactory answers. So here are some types that we want to convert an array of bytes to. UnityEngine.Font which can take in ttf data. UnityEngine.Testure2D which h can take in data from image files like .png , .jpg , etc... How would we convert a byte array to a String , UnityEngine.Testure2D,UnityEngine