Questions tagged [byte]

A unit of information usually corresponding to 8 bits. This term is also most often used to indicate the smallest addressable unit of storage on a digital system.

A unit of information usually corresponding to 8 bits. This term is also most often used to indicate the smallest addressable unit of storage on a digital system.

These days, the term byte usually refers to a unit of information consisting of 8 . Historically, other sizes have been used as well. If one wants to stress the fixed size, the term octet can be used instead.

7742 questions
932
votes
20 answers

Converting string to byte array in C#

I'm converting something from VB into C#. Having a problem with the syntax of this statement: if ((searchResult.Properties["user"].Count > 0)) { profile.User = System.Text.Encoding.UTF8.GetString(searchResult.Properties["user"][0]); } I then…
nouptime
  • 9,929
  • 5
  • 22
  • 37
882
votes
11 answers

"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.strip().lower() if 'some-pattern' in tmp:…
masroore
  • 9,668
  • 3
  • 23
  • 28
663
votes
37 answers

MySQL Error #1071 - Specified key was too long; max key length is 767 bytes

When I executed the following command: ALTER TABLE `mytable` ADD UNIQUE ( `column1` , `column2` ); I got this error message: #1071 - Specified key was too long; max key length is 767 bytes Information about column1 and column2: column1 varchar(20)…
Steven
  • 24,410
  • 42
  • 108
  • 130
470
votes
34 answers

Correct way to convert size in bytes to KB, MB, GB in JavaScript

I got this code to covert size in bytes via PHP. Now I want to convert those sizes to human readable sizes using JavaScript. I tried to convert this code to JavaScript, which looks like this: function formatSizeUnits(bytes){ if (bytes >=…
l2aelba
  • 21,591
  • 22
  • 102
  • 138
442
votes
25 answers

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 to keep it original, I'll phrase it my own way:…
rafraf
  • 4,806
  • 3
  • 19
  • 20
375
votes
9 answers

What is the difference between a string and a byte string?

I am working with a library which returns a "byte string" (bytes) and I need to convert this to a string. Is there actually a difference between those two things? How are they related, and how can I do the conversion?
Sheldon
  • 9,639
  • 20
  • 59
  • 96
278
votes
6 answers

How do you specify a byte literal in Java?

If I have a method void f(byte b); how can I call it with a numeric argument without casting? f(0); gives an error.
Charbel
  • 14,187
  • 12
  • 44
  • 66
231
votes
14 answers

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[] { (byte) 0x65, (byte)0x10, (byte)0xf3,…
stefan
  • 2,313
  • 2
  • 14
  • 4
218
votes
13 answers

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 successive addresses of an array of integers it shows…
Rajiv Prathap
  • 2,219
  • 2
  • 14
  • 8
209
votes
13 answers

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
Asm
  • 2,101
  • 2
  • 13
  • 4
208
votes
18 answers

How do I convert Long to byte[] and back in java

How do I convert a long to a byte[] and back in Java? I'm trying convert a long to a byte[] so that I will be able to send the byte[] over a TCP connection. On the other side I want to take that byte[] and convert it back into a double.
Emre801
  • 3,043
  • 5
  • 25
  • 28
196
votes
8 answers

Difference between byte vs Byte data types in C#

I noticed that in C# there are both a byte and Byte data type. They both say they are of type struct System.Byte and represent an 8-digit unsigned integer. What are the differences (if any) between the two, and why you would use one over the other?
jaywon
  • 8,164
  • 10
  • 39
  • 47
193
votes
11 answers

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 like there must be a better way. private static…
dfickling
  • 2,545
  • 2
  • 16
  • 13
191
votes
12 answers

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 the data straight back to the client. When I send…
0909EM
  • 4,761
  • 3
  • 29
  • 40
167
votes
30 answers

Does .NET provide an easy way convert bytes to KB, MB, GB, etc.?

Just wondering if .NET provides a clean way to do this: int64 x = 1000000; string y = null; if (x / 1024 == 0) { y = x + " bytes"; } else if (x / (1024 * 1024) == 0) { y = string.Format("{0:n1} KB", x / 1024f); } etc...
John Smith
  • 4,416
  • 7
  • 41
  • 56
1
2 3
99 100