Convert long to byte array to string representation

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
LVL 1
Sunny_KumarAsked:
Who is Participating?
 
Daniel Van Der WerkenIndependent ConsultantCommented:
0
 
Gene_CypCommented:
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
*/
0
 
Gene_CypCommented:
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
.
.
.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
MathiyazhaganCommented:
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.
0
 
Sunny_KumarAuthor Commented:
Thanks for all the help guys! Much appreciated!
0
 
sarabandeCommented:
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
0
 
sarabandeCommented:
there is a typo. it should be


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

Open in new window

 

Sara
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.