944 questions
461
votes
25
answers
653k
views
Convert a string representation of a hex dump to a byte array using Java?
I am looking for a way to convert a long string (from a dump), that represents hex values into a byte array.
I couldn't have phrased it better than the person that posted the same question here.
But ...
924
votes
11
answers
2.4m
views
"TypeError: a bytes-like object is required, not 'str'" when handling file content in Python 3
I've very recently migrated to Python 3.5.
This code was working properly in Python 2.7:
with open(fname, 'rb') as f:
lines = [x.strip() for x in f.readlines()]
for line in lines:
tmp = line....
231
votes
13
answers
780k
views
Is the size of C "int" 2 bytes or 4 bytes?
Does an Integer variable in C occupy 2 bytes or 4 bytes? What are the factors that it depends on?
Most of the textbooks say integer variables occupy 2 bytes.
But when I run a program printing the ...
153
votes
2
answers
98k
views
What does a b prefix before a python string mean?
In a python source code I stumbled upon I've seen a small b before a string like in:
b"abcdef"
I know about the u prefix signifying a unicode string, and the r prefix for a raw string literal.
What ...
143
votes
14
answers
499k
views
Byte array to image conversion
I want to convert a byte array to an image.
This is my database code from where I get the byte array:
public void Get_Finger_print()
{
try
{
using (SqlConnection thisConnection = new ...
194
votes
8
answers
559k
views
Convert bytes to int?
I'm currently working on an encryption/decryption program and I need to be able to convert bytes to an integer. I know that:
bytes([3]) = b'\x03'
Yet I cannot find out how to do the inverse. What am ...
193
votes
12
answers
643k
views
Java Byte Array to String to Byte Array
I'm trying to understand a byte[] to string, string representation of byte[] to byte[] conversion... I convert my byte[] to a string to send, I then expect my web service (written in python) to echo ...
79
votes
8
answers
266k
views
Saving any file to in the database, just convert it to a byte array?
Is converting a file to a byte array the best way to save ANY file format to disk or database var binary column?
So if someone wants to save a .gif or .doc/.docx or .pdf file, can I just convert it ...
125
votes
4
answers
152k
views
What does value & 0xff do in Java?
I have the following Java code:
byte value = 0xfe; // corresponds to -2 (signed) and 254 (unsigned)
int result = value & 0xff;
The result is 254 when printed, but I have no idea how this code ...
235
votes
15
answers
478k
views
Java integer to byte array
I got an integer: 1695609641
when I use method:
String hex = Integer.toHexString(1695609641);
system.out.println(hex);
gives:
6510f329
but I want a byte array:
byte[] bytearray = new byte[] { (...
101
votes
6
answers
88k
views
What is the maximum number of bytes for a UTF-8 encoded character?
What is the maximum number of bytes for a single UTF-8 encoded character?
I'll be encrypting the bytes of a String encoded in UTF-8 and therefore need to be able to work out the maximum number of ...
20
votes
6
answers
23k
views
Is addition of byte converts to int because of java language rules or because of jvm?
byte a = 1;
byte b = 1;
byte c = a + b;
Throws error: possible loss of precision
byte subt = a_s - a_b;
^
required: byte
found: int
Is this behavior has something to do with ...
162
votes
13
answers
351k
views
Convert any object to a byte[]
I am writing a prototype TCP connection and I am having some trouble homogenizing the data to be sent.
At the moment, I am sending nothing but strings, but in the future we want to be able to send ...
72
votes
7
answers
30k
views
Will a `char` always-always-always have 8 bits?
I've always assumed:
that a char is represented by a byte,
that a byte can always be counted upon to have 8 bits,
that sizeof (char) is always 1,
and that the maximum theoretical amount of memory I ...
220
votes
13
answers
120k
views
Why is a boolean 1 byte and not 1 bit of size?
In C++,
Why is a boolean 1 byte and not 1 bit of size?
Why aren't there types like a 4-bit or 2-bit integers?
I'm missing out the above things when writing an emulator for a CPU
58
votes
1
answer
144k
views
PIL: Convert Bytearray to Image
I am trying to verify a bytearray with Image.open and Image.verify() without writing it to disk first and then open it with im = Image.open(). I looked at the .readfrombuffer() and .readfromstring() ...
26
votes
3
answers
5k
views
Is CHAR_BIT ever > 8?
The ISO C Standard requires CHAR_BIT to be at least 8.
With POSIX mandating CHAR_BIT be equal to 8, and (almost?) all networking and communication standards using octets, is there any contemporary C ...
92
votes
4
answers
220k
views
C program to check little vs. big endian [duplicate]
Possible Duplicate:
C Macro definition to determine big endian or little endian machine?
int main()
{
int x = 1;
char *y = (char*)&x;
printf("%c\n",*y+48);
}
If it's little endian it ...
74
votes
6
answers
196k
views
Java: object to byte[] and byte[] to object converter (for Tokyo Cabinet)
I need to convert objects to a byte[] to be stored in the Tokyo Cabinet key-value store.
I also need to unbyte the byte[] to an Object when reading from the key-value store.
Are there any packages ...
77
votes
2
answers
99k
views
Java: BufferedImage to byte array and back
I see that a number of people have had a similar problem, however I'm yet to try find exactly what I'm looking for.
So, I have a method which reads an input image and converts it to a byte array:
...
55
votes
14
answers
233k
views
Convert bytes to bits in python
I am working with Python3.2. I need to take a hex stream as an input and parse it at bit-level. So I used
bytes.fromhex(input_str)
to convert the string to actual bytes. Now how do I convert these ...
94
votes
4
answers
197k
views
How to store a byte array in Javascript
I'm going to be storing a large array of byte values (most likely over a million) in Javascript. If I use a normal array with normal numbers, that will take 8 MB, because numbers are stored as IEEE ...
56
votes
7
answers
59k
views
byte array to short array and back again in java
I'm having some issues taking audio data stored in a byte array, converting it to a big-endian short array, encoding it, then changing it back into a byte array. Here is what I have. The original ...
209
votes
11
answers
1.0m
views
How do I initialize a byte array in Java?
I have to store some constant values (UUIDs) in byte array form in java, and I'm wondering what the best way to initialize those static arrays would be. This is how I'm currently doing it, but I feel ...
114
votes
12
answers
282k
views
How to Convert Int to Unsigned Byte and Back
I need to convert a number into an unsigned byte. The number is always less than or equal to 255, and so it will fit in one byte.
I also need to convert that byte back into that number. How would I ...