We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Convert long to byte array to string representation

Medium Priority
768 Views
Last Modified: 2013-12-16
Hi all,

I'm working with a 3rd party com method with returns a "dword" which is 32 bits. The first 16 bits contains info, and the rest is empty.

Using c#, I'm trying to get the returned value (which I think is a long?), convert that value to a byte array, and then get the string representation of the whole lot.

What I'm hoping to end up with is something like this:
"0000 1011 0000 0000" - without the spaces.
Each bit represents an error message and this is why I need to shift through the string values at specific indexes etc.

Can someone please help with this, I'm getting nowhere fast.

Thank you in advance,

Sunny
Comment
Watch Question

Commented:
UInt32 can be converted using BitConverter.GetBytes


See: http://msdn.microsoft.com/en-us/library/1184xdy4.aspx



// Example of the BitConverter.GetBytes( uint ) method.
using System;

class GetBytesUInt32Demo
{
    const string formatter = "{0,16}{1,20}";

    // Convert a uint argument to a byte array and display it.
    public static void GetBytesUInt32( uint argument )
    {
        byte[ ] byteArray = BitConverter.GetBytes( argument );
        Console.WriteLine( formatter, argument,
            BitConverter.ToString( byteArray ) );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the BitConverter.GetBytes( uint ) " +
            "\nmethod generates the following output.\n" );
        Console.WriteLine( formatter, "uint", "byte array" );
        Console.WriteLine( formatter, "----", "----------" );

        // Convert uint values and display the results.
        GetBytesUInt32( 15 );
        GetBytesUInt32( 1023 );
        GetBytesUInt32( 0x100000 );
        GetBytesUInt32( 1000000000 );
        GetBytesUInt32( uint.MinValue );
        GetBytesUInt32( int.MaxValue );
        GetBytesUInt32( uint.MaxValue );
    }
}

/*
This example of the BitConverter.GetBytes( uint )
method generates the following output.

            uint          byte array
            ----          ----------
              15         0F-00-00-00
            1023         FF-03-00-00
         1048576         00-00-10-00
      1000000000         00-CA-9A-3B
               0         00-00-00-00
      2147483647         FF-FF-FF-7F
      4294967295         FF-FF-FF-FF
*/

Commented:
You can get your 0s and 1s by converting the HEX value into a binary value.

Hex - Binary
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
.
.
.
Independent Consultant
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Could you post the dword value that you are getting from 3rd party com method? so that any one of us help you with possible solution.

Author

Commented:
Thanks for all the help guys! Much appreciated!
CERTIFIED EXPERT
Top Expert 2016

Commented:
c# has the same bit operations as c and c++.

so you could do

if ((ui & (1<<i) != 0) 

Open in new window


what would test whether the unsigned integer ui has bit value 1 at bit position i.

that works cause 1<<i moves bit 0 to bit i  and the ui & operation makes a bitwise 'and' such that the result either is 0 (all bits are 0) or has exactly 1 bit at position i.

Sara
CERTIFIED EXPERT
Top Expert 2016

Commented:
there is a typo. it should be


if ((ui & (1<<i)) != 0)

Open in new window

 

Sara
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.