473,396 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can I set a byte to hold the binary value 10?

How can I set a byte to hold the binary value 10?

Doing this doesn't work, I want to store the code for 10 in 1 byte of
memory.

byte encodedBytes = '10';

Doesn't compile "Too many characters in character literal"

Any thoughts?
Nov 16 '05 #1
5 5613
Hi Max,

Well, assuming you mean binary 10 as in decimal 2

byte encodedBytes = 2;

Or

byte encodedBytes = (byte)Convert.ToInt32("10", 2);

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Max,

If you want a general way to convert the binary representation of a
number to a byte, then use the static ToByte method on the Convert class,
like so:

// Convert "10" to a byte:
byte b = Convert.ToByte("10", 2);

The second parameter is the base to interpret the string in.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Max Adams" <ru************@hotmail.com> wrote in message
news:uE**************@TK2MSFTNGP12.phx.gbl...
How can I set a byte to hold the binary value 10?

Doing this doesn't work, I want to store the code for 10 in 1 byte of
memory.

byte encodedBytes = '10';

Doesn't compile "Too many characters in character literal"

Any thoughts?

Nov 16 '05 #3
Ignore my answer and use Nicholas' instead. No need to convert to int when ToByte has a similar overload.

Note though that you can set a byte directly to an integer value, but need to cast it to (byte) if it needs to be evaluated.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4
> How can I set a byte to hold the binary value 10?

Doing this doesn't work, I want to store the code for 10 in 1 byte of
memory.

byte encodedBytes = '10';

Doesn't compile "Too many characters in character literal"


To avoid confusion, my post should have read, how do I convert the integer
number 10 (ten) to a one byte representation.

Nov 16 '05 #5
Max Adams <ru************@hotmail.com> wrote:
To avoid confusion, my post should have read, how do I convert the integer
number 10 (ten) to a one byte representation.


Ah. That's easy:

int i = 10;
byte b = (byte)i;

Or just

byte b = 10;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
9
by: franzkowiak | last post by:
Hello, I've read some bytes from a file and just now I can't interpret 4 bytes in this dates like a real value. An extract from my program def l32(c): return ord(c) + (ord(c)<<8) +...
19
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
16
by: Samuel Thomas | last post by:
Hello Friends, I understand(could be wrong) that the smallest chunk of memory is called a word. If that is correct, that means if I am using a 32 bit OS a word is 4 bytes. So that's why the size...
6
by: John Hoffman | last post by:
Reading registry: .... RegistryKey rksub = rkey.OpenSubKey(s); String valstr = rksub.GetValueNames(); foreach (String vs in valstr) { String vstr = rksub.GetValue(vs).ToString(); OR String...
4
by: TRW1313 | last post by:
I'm looking to populate a byte array of some fixed size to send out over a UDP connection. The data in the byte array is mixed between characters and binary. I'm a beginner to this language. ...
2
by: DBuss | last post by:
OK, I'm reading a multicast socket. It attaches fine, reads fine, all of that. The problem is that while some of the data I get is normal text (ASCII String), some of it is Binary Integer. ...
2
by: sri | last post by:
I think the following question is related to this group that is why I am posting The question is: I am reading bytes from binary file. At a particular position the byte value is "FF". I am...
9
by: hari | last post by:
hi all, Im writing a code in which the bytes,needed to be splitted in to nibbles.each nibble needs to be made as byte. for example: consider 3B as byte, on splitting this I will get 3 and B.The...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.