72323

Working with Byte literals

Question:

I'm using the following function to brighten up color values (it's a lambda in my code, but that shouldn't make a differende):

Function ReduceDistanceTo255(ByVal i As Byte) As Byte Return i + (255 - i) \ 2 End Function

It won't compile, since the compiler interprets 255 and 2 as integers rather than bytes, making the result of type Integer. Unfortunately, <a href="https://stackoverflow.com/questions/252817" rel="nofollow">there is no Byte type character</a>, so I cannot just write 255B or something like that.

There are a few obvious workarounds to the problem:

Function ReduceDistanceTo255(ByVal i As Byte) As Byte Return i + (CByte(255) - i) \ CByte(2) End Function

and

Function ReduceDistanceTo255(ByVal i As Byte) As Byte Return CByte(i + (255 - i) \ 2) End Function

and

Function ReduceDistanceTo255(ByVal i As Byte) As Byte Dim FF As Byte = 255 Dim two As Byte = 2 Return i + (FF - i) \ two End Function

The first one is just plain ugly and hard to read, because <em>every</em> literal needs to be CByted. The second one performs calculations in integers and then converts the result to Byte, which is OK, but not as elegant as a pure-Byte operation. The third workaround doesn't require CBytes, but it's drawbacks are obvious.

Did I miss some (elegant) fourth option which allows me to do Byte-only-math without cluttering my formula with CBools?

Answer1:

It is specifically mentioned in the Visual Basic Language Specification, chapter 2.4.2:

<blockquote>

<strong>Annotation</strong> <em>> There isn’t a type character for Byte because the most natural character would be B, which is a legal character in a hexadecimal literal.</em>

</blockquote>

Well, that's true I guess. "Octet" got voted down too, no doubt. Use Return CByte(...), it is cheaper than ToByte().

Answer2:

How about using constants:

Function ReduceDistanceTo255(ByVal i As Byte) As Byte Const bFF As Byte = 255 Const b02 As Byte = 2 Return i + (bFF - i) \ b02 End Function

no conversion, no casting, no extra variables

Answer3:

How about the easy way:

Imports System.Convert Function ReduceDistanceTo255(ByVal i As Byte) As Byte Return ToByte(i + (255 - i) \ 2) End Function

<strong>Edit:</strong> I'd prefer this workaround because it would do less casting and it is pretty clear to me what's going on.

Recommend

  • jquery easing breaks jquery on wordpress
  • Various HMAC_SHA256 functions in classic ASP gives different results
  • mysql date format regarding to a specific country
  • Pandas Apply(), Transform() ERROR = invalid dtype determination in get_concat_dtype
  • How to use checkbox in listview
  • Sending POST params with Netty and why isn't DefaultHttpDataFactory not in the releases?
  • Should not allow file upload if anyone changes extension from exe to png via multer in node js appli
  • Writing a file to a USB stick in linux with Python?
  • Revert to default value in mongoose if field is set to null
  • Foursquare missing file upload / InvalidPhotoFormat error while uploading photo through api
  • Not able to partial mock static method with PowerMockito
  • Color Space Mapping YCbCr to RGB
  • Angular infinite digest loop with ui-router
  • Creating globally accessible constant buffer in Metal Shading Language
  • How to add paragraph wise texts separated by vertical line into itextsharp created PDF
  • Capture literal brackets inside brackets in pyparsing
  • mailto link is not working in Chrome Browser (Version :18.0.1025.152 m)
  • Load balanced Fiware Orion
  • Change ListView font size in JavaFX
  • Cross browser key event handler in Dart
  • Problem in Loading xml from specified url using javascript in FF & Google Chrome
  • Sympy: working with equalities manually
  • Express JS Display Data By ID
  • Double-click autofill - dynamic based on adjacent cell
  • Excel VBA URLDownloadToFile Error for https Ressource
  • How can I let users share their location in Bot Framework webchat channel?
  • Efficient User-Agent Regex to find Safari in Python
  • Creating a Multidimensional, Associative Array in VBScript
  • Is it possible to get the word under the mouse cursor in a ``?
  • NSIS decompiler
  • HttpURLConnection Closing IO Streams
  • abstracting over a collection
  • How to 'create temp table as select' in Slick?
  • Can I programmatically choose the Android layout folder?
  • Conversion from string “a” to type 'Boolean' is not valid
  • Parsing a CSV string while ignoring commas inside the individual columns
  • ilmerge with a PFX file
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to add date and time under each post in guestbook in google app engine
  • coudnt use logback because of log4j