0

I would like to simulate byte overflow in VB.NET. The code below achieves the correct result but I suspect this is not the most efficient method. Is there a simpler/better way to achieve this?

 Dim src As Byte = 232
 Dim key As Byte = 231
 Dim encoded As Byte = 0
 Dim decoded As Byte = 0

 ' Encode
 encoded = CByte((CInt(src) + CInt(key)) Mod 256I)

 ' Decode
 Dim tmp As Int32 = CInt(encoded) - CInt(key)
 decoded = CByte(IIf(tmp < 0, 256I + tmp, tmp))

 ' encoded = 207
 ' decoded = src = correct
1
  • 3
    if you want feedback on your working code, a better place to ask is codereview.stackexchange.com Commented Jun 17, 2014 at 13:15

1 Answer 1

3

Yes, you can make that a lot faster. It however requires a project building option change. Add a new project to your solution. Right-click it, Properties, Build tab, scroll down, click the Advanced Compile Options button. Tick the "Remove integer overflow checks" option.

Now you can write it like this:

    Dim src As Byte = 232
    Dim key As Byte = 231
    Dim encoded As Byte = src + key
    Dim decoded As Byte = encoded - key

Which you'd do in Public methods you expose from your new project.

Do beware the very questionable utility of a Caesar cipher, it is far too easy to crack. The System.Cryptography namespace has much better alternatives, doesn't require you to tinker with global build options that have tricky side-effects either.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.