2

I need to take an int and turn it into its byte form.

i.e. I need to take '1' and turn it into '00000001' or '160' and turn it into '10100000'

Currently, I am using this

 int x = 3;
 string s = Convert.ToString(x, 2);
 int b = int.Parse(s);

This is an awful way to do things, so I am looking for a better way.

Any Suggestions?

EDIT

Basically, I need to get a list of every number up to 256 in base-2. I'm going to store all the numbers in a list, and keep them in a table on my db.

UPDATE

I decided to keep the base-2 number as a string instead of parsing it back. Thanks for the help and sorry for the confusion!

asked Feb 21, 2011 at 13:16
6
  • 3
    You've already got its binary value in string s, why do you convert it back to an int?(which "looks" like a binary value but actually it's a larger int value) Commented Feb 21, 2011 at 13:21
  • x is already the number 160 in byte form. Convert.ToString(x, 2) returns that number in binary, just like Convert.ToString(x) returns the number in decimal. Commented Feb 21, 2011 at 13:21
  • Its a standard way.. if you want to simplify even further, create a extension method for int like "3.to_b(base)" and call the actual converstion inside Commented Feb 21, 2011 at 13:22
  • 1
    why does it need to be an int at the end? In your code, it contains a number that looks like a binary representation when reading it as a decimal number! I mean, the int already is binary... ToString makes it readable as binary... Commented Feb 21, 2011 at 13:24
  • 1
    @Ramesh Vel: What kind of method name is to_b??? You sound like my colleagues! Commented Feb 21, 2011 at 13:28

5 Answers 5

4

If you need the byte form you should take a look at the BitConverter.GetBytes() method. It does not return a string, but an array of bytes.

answered Feb 21, 2011 at 13:22

Comments

1

The int is already a binary number. What exactly are you looking to do with the new integer? What you are doing is setting a base 10 number to a base 2 value. That's kind of confusing and I think you must be trying to do something that should happen a different way.

answered Feb 21, 2011 at 13:24

1 Comment

Yes, I guess I just explained it incorrectly. I want a base 2 number from its base 10 counter-part.
1

I don't know what you need at the end ... this may help:

Turn the int into an int array:

byte[] bytes = BitConverter.GetBytes(x);

Turn the int into a bit array:

BitArray bitArray = new BitArray(new[] {x});
answered Feb 21, 2011 at 13:26

Comments

1

You can use BitArray.

The code looks a bit clumsy, but that could be improved a bit.

int testValue = 160;
System.Collections.BitArray bitarray = new System.Collections.BitArray(new int[] { testValue });
var bitList = new List<bool>();
foreach (bool bit in bitarray)
 bitList.Add(bit);
bitList.Reverse();
var base2 = 0;
foreach (bool bit in bitList)
{
 base2 *= 10; // Shift one step left
 if (bit)
 base2++; // Add 1 last
}
Console.WriteLine(base2);
answered Feb 21, 2011 at 13:30

Comments

0

I think you are confusing the data type Integer with its textual representation.

int x = 3;

is the number three regardless of the representation (binary, decimal, hexadecimal, etc.) When you parse the binary textual representation of an integer back to integer you are getting a different number. The framework assumes you are parsing the number represented in the decimal base and gives the corresponding integer. You can try

int x = 1600;
string s = Convert.ToString(x, 2);
int b = int.Parse(s);

and it will throw an exception because the binary representation of 1600 interpreted as decimal is too big to fit in an integer

answered Feb 21, 2011 at 14:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.