Convert VB to C# (byte sift, color ) RRS feed

  • Question

  • Hi I don t know how to convert this code from vb to c#, I don t know how to assign hexadecimal number in c# a work with color constants in c#. Can someone help me to convert this vb function in c# ???
    Private Function RGB565To32ARGBV3(ByVal rgb16 As UShort) As Int32
            Dim rgb32 As Integer
            Dim tmp As UShort
            rgb32 = &HFF
            tmp = rgb16 >> 11 And &H1F
            tmp = (tmp * &HFF) / &H1F
            rgb32 = (rgb32 << 8) + (tmp And &HFF)
            tmp = rgb16 >> 5 And &H3F
            tmp = (tmp * &HFF) / &H3F
            rgb32 = (rgb32 << 8) + (tmp And &HFF)
            tmp = rgb16 And &H1F
            tmp = (tmp * &HFF) / &H1F
            rgb32 = (rgb32 << 8) + (tmp And &HFF)
            Return rgb32
        End Function

    Thursday, November 30, 2017 12:03 PM

All replies

  • You have a lot of mixing of different integer types here, so the C# equivalent has to have a few casts and adjustments:

    	public Int32 RGB565To32ARGBV3(ushort rgb16)
    		int rgb32 = 0;
    		ushort tmp = 0;
    		rgb32 = 0xFF;
    		tmp = (ushort)(rgb16 >> 11 & 0x1F);
    		tmp = Convert.ToUInt16((tmp * 0xFF) / (double)0x1F);
    		rgb32 = (rgb32 << 8) + (tmp & 0xFF);
    		tmp = (ushort)(rgb16 >> 5 & 0x3F);
    		tmp = Convert.ToUInt16((tmp * 0xFF) / (double)0x3F);
    		rgb32 = (rgb32 << 8) + (tmp & 0xFF);
    		tmp = (ushort)(rgb16 & 0x1F);
    		tmp = Convert.ToUInt16((tmp * 0xFF) / (double)0x1F);
    		rgb32 = (rgb32 << 8) + (tmp & 0xFF);
    		return rgb32;

    Note that the "double" casts are necessary to force floating point division.  The VB "/" operator always does floating point division (e.g., the result could be a floating point type), but C# will perform integer division unless one of the operands is of a floating point type.

    However, you're then assigning the result to an integer type, so then you need to adjust again...

    So you might find that you can simplify this.

    Convert between VB, C#, C++, & Java (
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Thursday, November 30, 2017 3:44 PM
  • Thanks a lot, I will try a code 
    Thursday, November 30, 2017 4:33 PM
  • Hi Jony1989,

    Any update? Have you try the code provided by Dave Doknjas? 

    If your question has been solved, please mark the useful as answer. If you have something else about this issue, please feel free to contact us.

    In normal, if you want to convert VB to C#, you could try some converter tools online.

    Best Regards,


    Note: This response contains a reference to a third-party World Wide Web site. Microsoft is providing this information as a convenience to you. 

    Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. 

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Friday, December 1, 2017 5:51 AM