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.

7028 questions
769
votes
17 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
  • 8,267
  • 5
  • 18
  • 33
715
votes
9 answers

TypeError: a bytes-like object is required, not 'str' when writing to a file in Python3

I've very recently migrated to Py 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
  • 7,815
  • 3
  • 15
  • 21
602
votes
34 answers

#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
  • 21,552
  • 40
  • 100
  • 126
398
votes
24 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,350
  • 3
  • 17
  • 20
330
votes
19 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
  • 18,585
  • 20
  • 90
  • 131
256
votes
7 answers

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

I am working with a library which returns a byte string and I need to convert this to a string. Although I'm not sure what the difference is - if any.
Sheldon
  • 7,678
  • 17
  • 55
  • 89
251
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
  • 12,761
  • 11
  • 40
  • 64
205
votes
13 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,053
  • 2
  • 13
  • 4
191
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
  • 1,949
  • 2
  • 12
  • 8
185
votes
11 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
  • 3,721
  • 3
  • 20
  • 30
178
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. So I am curious as to what the difference if any is between the two, and why you would use…
jaywon
  • 7,818
  • 10
  • 36
  • 47
177
votes
13 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
  • 2,615
  • 5
  • 22
  • 28
157
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,175
  • 2
  • 14
  • 13
155
votes
12 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
  • 1,561
  • 2
  • 10
  • 4
147
votes
12 answers

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 any object. The code is quite simple at the moment,…
Steve H.
  • 3,083
  • 4
  • 22
  • 29
1
2 3
99 100