Convert long to byte array to string representation

Posted on 2011-04-20
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,

Question by:Sunny_Kumar
    LVL 7

    Expert Comment

    UInt32 can be converted using BitConverter.GetBytes


    // 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( )
                "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
    LVL 7

    Expert Comment

    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
    LVL 19

    Accepted Solution

    LVL 10

    Expert Comment

    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.
    LVL 1

    Author Closing Comment

    Thanks for all the help guys! Much appreciated!
    LVL 32

    Expert Comment

    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.

    LVL 32

    Expert Comment

    there is a typo. it should be

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

    Open in new window



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now