Hex to Binary Conversion

D

dondigitech

I want to convert hex to binary without losing bits. I want to
preserve the 8-bits because I ultimately need a 24-bit string to grab
information from. I am just using this line of code for the
conversion:

string revLim = Convert.ToString(curveData[4], 2);
// curveData[4] = 0x14
// I want 00010100
// I am getting 10100

So, this creates an issue when I want to combine the three bytes
because it totally screws up the bit fields that I need to grab.

Thanks in advance!!
 
A

Alberto Poblacion

I want to convert hex to binary without losing bits. I want to
preserve the 8-bits because I ultimately need a 24-bit string to grab
information from. I am just using this line of code for the
conversion:

string revLim = Convert.ToString(curveData[4], 2);
// curveData[4] = 0x14
// I want 00010100
// I am getting 10100

So, this creates an issue when I want to combine the three bytes
because it totally screws up the bit fields that I need to grab.

One way to add the required number of zeroes is to use the String.PadLeft
method:

string revLim = Convert.ToString(curveData[4], 2).PadLeft(8,'0');
 
D

dondigitech

Thank you for your replies. Using the padleft() method worked out for me but
now I'm trying to figure out if it would ultimately be useful in the overall
scheme of things. Any advice in a direction to take would be much
appreciated.

Basically, I have an array of hex bytes. In 3 byte chunks i need to grab the
bits 0(lsb)-12 which will be ORed with 0xE0 (which is why i'm doing the hex
to binary string conversion), then I will have to take the 2s complement of
it to get my desired offset value. Bits 13-23(msb) will just be converted to
a decimal number which will be a multiplier for a function that I will have
to perform. So right now, this is my approach (i know its not even close to
being the most efficient):

1. convert hex bytes to binary
2. grab bit fields needed (11 for multiplier and 13 for offset)
3. OR offset (13 bit) with 0xE0 (ultimately yield a 16 bit string) & take 2s
complement of it
4. convert to decimal to get finalOffset number
5. convert (11 bit) multiplier to decimal


Peter Duniho said:
I want to convert hex to binary without losing bits.

No you don't. You want to convert an integer to a string formatted as
binary without losing digits.

You might find that pedantic, but it's an important distinction.
I want to
preserve the 8-bits because I ultimately need a 24-bit string to grab
information from. I am just using this line of code for the
conversion:

string revLim = Convert.ToString(curveData[4], 2);
// curveData[4] = 0x14
// I want 00010100
// I am getting 10100

So, this creates an issue when I want to combine the three bytes
because it totally screws up the bit fields that I need to grab.

Probably the easiest approach would be to use the String.PadLeft() method
to fill out the result string with '0' characters.

That said, you're not showing us the rest of the problem, but if after
creating the string you turn around and try to extract specific
bit-fields, it seems to me that you're going to a lot of extra work for
nothing. You probably just should be doing all of the processing of the
value as an integer, using bit-masks (possibly declared as an enum).

Pete
 
D

dondigitech

i forgot to include the first step, which was to combine the 3 byte chunks to
an int

dondigitech said:
Thank you for your replies. Using the padleft() method worked out for me but
now I'm trying to figure out if it would ultimately be useful in the overall
scheme of things. Any advice in a direction to take would be much
appreciated.

Basically, I have an array of hex bytes. In 3 byte chunks i need to grab the
bits 0(lsb)-12 which will be ORed with 0xE0 (which is why i'm doing the hex
to binary string conversion), then I will have to take the 2s complement of
it to get my desired offset value. Bits 13-23(msb) will just be converted to
a decimal number which will be a multiplier for a function that I will have
to perform. So right now, this is my approach (i know its not even close to
being the most efficient):

1. convert hex bytes to binary
2. grab bit fields needed (11 for multiplier and 13 for offset)
3. OR offset (13 bit) with 0xE0 (ultimately yield a 16 bit string) & take 2s
complement of it
4. convert to decimal to get finalOffset number
5. convert (11 bit) multiplier to decimal


Peter Duniho said:
I want to convert hex to binary without losing bits.

No you don't. You want to convert an integer to a string formatted as
binary without losing digits.

You might find that pedantic, but it's an important distinction.
I want to
preserve the 8-bits because I ultimately need a 24-bit string to grab
information from. I am just using this line of code for the
conversion:

string revLim = Convert.ToString(curveData[4], 2);
// curveData[4] = 0x14
// I want 00010100
// I am getting 10100

So, this creates an issue when I want to combine the three bytes
because it totally screws up the bit fields that I need to grab.

Probably the easiest approach would be to use the String.PadLeft() method
to fill out the result string with '0' characters.

That said, you're not showing us the rest of the problem, but if after
creating the string you turn around and try to extract specific
bit-fields, it seems to me that you're going to a lot of extra work for
nothing. You probably just should be doing all of the processing of the
value as an integer, using bit-masks (possibly declared as an enum).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top