Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9246887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:35:56+00:00 2026-06-18T09:35:56+00:00

I would like to be able to convert a high-valued unsigned-integer (a value that

  • 0

I would like to be able to convert a high-valued unsigned-integer (a value that uses the highest-order bit) to a signed-integer. In this case, I don’t care that the value is higher than the maximum value of the signed integer type. I just want it to convert to whatever the bit-values represent as a signed-integer. In other words, I would expect it to result in a negative number.

However, with VB.NET, the CType operation doesn’t work that way (or any of the other conversion functions like CShort andCInteger). When you try to convert an unsigned value that is higher than the desired signed-type’s maximum value, it throws an OverflowException rather than returning a negative number. For instance:

Dim x As UShort = UShort.MaxValue
Dim y As Short = CShort(x)  ' Throws OverflowException

It’s worth mentioning, too, that the DirectCast operation cannot be used to cast the value between the signed and unsigned types, since neither type inherits or implements the other. For instance:

Dim x As UShort = UShort.MaxValue
Dim y As Short = DirectCast(x, Short)  ' Won't compile: "Value of type 'UShort' cannot be converted to 'Short'

I have figured out one way to do what I want, but it seems unnecessarily ugly. Here’s how I got it to work:

Dim x As UShort = UShort.MaxValue
Dim y As Short = BitConverter.ToInt16(BitConverter.GetBytes(x), 0)  ' y gets set to -1

Like I said, that works, but if there’s an easier, cleaner way of doing it in VB.NET, I’d love to know what it is.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:35:57+00:00Added an answer on June 18, 2026 at 9:35 am

    Constant use of BitConverter is going to be a bit inconvenient if you are using that a lot – in particular for performance. If that was me, I would be sorely tempted to add a utilities library in C# that can do direct conversions (via unchecked, although unchecked is normally the default in C# anyway), and reference that library for this. Another option might be to abuse a “union” struct; the following should translate to VB fairly easily:

    [StructLayout(LayoutKind.Explicit)]
    struct EvilUnion
    {
        [FieldOffset(0)] public int Int32;
        [FieldOffset(0)] public uint UInt32;
    }
    ...
    var evil = new EvilUnion();
    evil.Int32 = -123;
    var converted = evil.UInt32;
    

    i.e.

    <System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Explicit)>
    Structure EvilUnion
        <System.Runtime.InteropServices.FieldOffset(0)>
        Public Int32 As Integer
        <System.Runtime.InteropServices.FieldOffset(0)>
        Public UInt32 As UInteger
    End Structure
    ...
    Dim evil As New EvilUnion
    evil.Int32 = -123
    Dim converted = evil.UInt32
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to create a variable that holds an instance
I would like to be able to convert between std::vector and its underlying C
I would like to be able to convert videos to flv through the use
I would like to be able to convert a string such as 1,2,5-7,10 to
I would like to be able to convert a raw UTF-8 string to a
I would like to be able to accept \n or \r\n and convert them
I would like to be able to convert TV series' episodes from #7.11 to
I would like to be able to convert simple one digit time strings into
I would like to be able to convert a python mathematical expression into content
I would like to be able to parse XML that isn't necessarily well-formed. I'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.