.net - Converting Boolean To Byte?
Feb 9, 2012
Why does casting a boolean to a byte in .NET give the following output?
Code Snippit:
Dim x As Boolean = 1
Dim y As Byte = x 'Implicit conversion here from Boolean to Byte
System.Diagnostics.Debug.Print( _
[Code].....
Why does True (which commonly is referred to as an integer representation of 1) convert to 255 when casted to a byte?
View 3 Replies
ADVERTISEMENT
May 6, 2007
I would like to know how to convert a unisgned byte to signed byte
Atm I got this
a
Function readSignedByte() As SByte
'-128/127
Dim b As SByte
[Code]....
it doesn't work one that well works for numbers positive over 127 if lets say ReadByte() has 128 it would give overflow error which I don't want it to give I would like it to overflow the number to negivate value aka its signed value.
View 5 Replies
Jul 22, 2010
I am converting some C# code to VB, the following code is part of a function
if (bool.Parse(v["Parent"].ToString()))
{
e.Row.CssClass = "Parent";
[Code]....
The code throws an error "String was not recognized as a valid Boolean" But if I use this code
If CBool(v("Parent").ToString) = True Then
e.Row.CssClass = "Parent"
End If
It works fine, which is great
View 8 Replies
Aug 1, 2009
I am interfacing my pc to a device called a MiniBee which is basically a usb digital output adapter to allow electronic devices to be controlled by the pc. So, I need to be able to individually address the output channels of this device, the only problem is the DLL supplied with it will only accept an integer value which represents a binary instruction if that makes any sense. I have basically got 8 public boolean vars OUT1, OUT2 etc that I need to use to turn on/off the outputs by them being set to true/false.
So there are say 8 digital output channels to operate and it has to work like this:
SetOutputs(1) would turn on output 1 and turn all others off
SetOutputs(2) would turn on output 2 and all others off
SetOutputs(3) turns on 1 and 2 and all others off
How to turn my public bool vars into an integer based on what is set to true/false if you get my meaning as there must be an easy way but working it out individually there are far too many possible permutations.
View 1 Replies
Dec 6, 2010
I am creating a 'generic input dialog': InputDialog(Of T).The idea is that I can create new instances of this dialog for different values of T. For example,I can create an InputDialog(Of String) and it displays a textbox.I create an InputDialog(Of Boolean) and it displays two radiobuttons (I could use a Checkbox, irrelevant).I create an InputDialog(Of Date) and it shows a DateTimePicker.
I do this by simply checking the type of T at run-time.In this way it is not really generic, as the dialog still has to know which type T is (which is usually not the case), but the generic is in the fact that the T can be multiple types that require a TextBox. For example, InputDialog(Of String), (Of Integer), (Of Single), (Of Double), etc, all simply display a TextBox (and it is validated later),so I still want to use generics as much as possible.Anyway, the result of the dialog is of course a property of type T. This property needs to return the value in the textbox, converted from string to T, OR the value in the DateTimePicker, converted from Date to T,OR the checked property of the 'Yes' radiobutton, converted from Boolean to T.In order to convert from any object to T I googled and found this
vb.net
Imports System.ComponentModel Public Class GenericTypeConverter Public Shared Function FromObject(Of T)(ByVal value As Object) As T Dim tc As TypeConverter = TypeDescriptor.GetConverter(GetType(T)) Return CType(tc.ConvertFrom(value), T) End Function End Class
I have to use this, I cannot simply CType a string to T since a String cannot be converted to any type T.So, my Result property looks like this at the moment:
vb.net
Public ReadOnly Property Result()[code].....
This actually works for Strings, Integers, Doubles, Singles, etc. I pass it the String in the textbox and it converts it to 'T'(note: T is then a String, Integer or Double!) just fine.So, it can convert a String to a String, Integer or Double without any problems (I am validating the text before using the Result property so it will always be a valid integer, double, etc).However, it does not work for a Boolean, nor for a DateTime. When I try that, it says "BooleanConverter cannot convert from System.Boolean" or "DateTimeConverter cannot convert from DateTime". I realize it is a bit of a strange thing, since the object already IS a Boolean or a Date(Time) so no conversion should be done at all, but this doesn't work in design-time because I need to return the objects as type T.I know that T will be Boolean when the value to be converted is a Boolean, and that T will be DateTime when the value to be converted is a DateTime, but the designer does not accept a simple CType. As I said, it does not accept this:
Return CType(rbYes.Checked, T)
because 'Boolean cannot be converted to T'.
However, I did find a way to make it work, but it seems like a complete hack... I can assign the boolean to an Object and then convert that using CType:
Dim obj As Object
obj = rbYes.Checked
Return CType(obj, T)
This works, it's accepted during design-time, and it works during run-time, but it seems very wrong... Is there no better way to handle this?
View 5 Replies
Feb 24, 2012
I'm reading bytes from a serial port but need to convert them to signed, 8-bit integers (SByte).Unfortunately, the overflow checking in VB prevents a Byte value of 255 from becoming -1 in an SByte. So, essentially, I want to do the following:
[Code]...
View 2 Replies
May 9, 2012
I have the following code:
Private Function SizeLogo(ByVal logoBytes As Byte()) As Byte()
Using originalMemStream As MemoryStream = New MemoryStream(logoBytes),
originalImage As System.Drawing.Image = System.Drawing.Image.FromStream(originalMemStream)
[Code]....
What I am trying to do is
Create 300width and 200height white bitmap Draw and center image that was uploaded into center
My problem is I am unable to save the created image. Is this a conversion issue? I've highlighted code where I think I am failing at.
View 1 Replies
Jun 28, 2010
I am sending data to some microchips with a serial port control using:
Code:
well, it work up to the value 9, but anything with more that one decimal place sends a byte value of the rightmost digit. So, 16 in the text box will send 6. Basically, it appears to be sending just 6, or 1 followed by 6. Is this how the serial port sends data...one digital at a time? Or, is the conversion to a byte failing?
View 6 Replies
Feb 7, 2010
I have a Generic List of Bytes with Hexadecimal values stored in it. for example, elements 0 to 3 contain 42 9E D1 EC which as a single, is equal to 79.41. I've tried using:
dim foo as string = ""
for i as integer = 0 to 3
foo = foo + system.convert.tosingle(byteArray(i)).ToString
[code].....
View 1 Replies
Mar 10, 2010
I am using the .NET Framework 2.0, and I am trying to code a client bot for a game called Minecraft, Originally written in Java, there have been quite a few people who have made custom multi-player servers for this game, Primarily in C#, or in a language that i've never heard of, such as Pascal.I'm looking right now at sending this:
Packet ID: 0x00 (0) As Byte
Protocol Version: 0x07(7) As byte
Username: "umby25" As String
[code].....
"Byte cannot be converted to 1-Dimensional array of byte"
I don't understand why this whole thing would work in all of the other programming languages, but not Visual basic.I have tried converting the string using the same method that one of the open source C# servers used, and it failed, telling me that the dictionary does not contain that or something.
View 4 Replies
Oct 26, 2009
I'm converting a stream of bytes from a network packet into a specific struct, and vice versa. For example, when converting a short back and forth, my method looks like this:
myShort = buf(0) + (buf(1) * 256)
or the other direction:
buf(0) = myShort mod 256
buf(1) = myShort 256
When it comes to a 4 byte integer, after some experimentation I found that converting from the byte array to integer is done this way:
myInt = buf(0) + (buf(1) * 256) + buf(2) + (buf(3) * 256)
Basically adding two shorts together. I'm not quite sure to do the reverse of that, to fill the 4 bytes from myInt.
View 4 Replies
Mar 8, 2011
I am working on a mini project that requires me to take a 8 byte hex string that I received from the Serial Port and convert it into a Byte Array and display it on the screen.An example of the string that I receive is 01050001FFFF8FFB
I am currently using the System.Text.ASCIIEncoding.ASCII.GetBytes(str) to help me achieve this. However I realised that if this does not support extended ASCII so whatever byte that is > 7F, I will not get the right value.My current code is as follows:
vb
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim str As String
[code]....
View 5 Replies
Aug 26, 2009
I have a hexadecimal value
07A5953EE7592CE8871EE287F9C0A5FBC2BB43695589D95E76A4A9D37019C8
Which I want to convert to a byte array. Is there a built in function in .NET 3.5 that will get the job done or will I need to write a function to loop through each pair in the string and convert it to its 8-bit integer equivalent?
View 2 Replies
May 25, 2009
Is there a way to convert byte values to binary so i can read the values from the bits themselves? I have some byte whose values are 00 09 and I'd like to convert the values to bits to be like this : 0000 0000 0000 1001. Then I'd like to read the value of first 2 bits and the remaining 14 bits separately.
View 10 Replies
May 17, 2011
I understand the concept of bytes and declaring variables to save on processing space. I understand that the max value that can be stored in a byte is 255. I cannot seem to wrap my head around my current issue. I don't have much experience working with byte manipulation.
[Code]...
View 3 Replies
Aug 11, 2009
What do i need to do to convert a recieved byte from Ascii to String, so i could display it properly on a textbox?
View 16 Replies
Apr 25, 2011
How would I convert the two individual byte values (in hex) to a decimal value representing the concatenated hex values? For example, if I have the
Dim byte1 As Byte = &H99
Dim byte2 As Byte = &H99
' I want the decimal representation of the hex value "9999" ' (byte1.ToString + byte2.ToString) which should be "39321" Currently I use the following code:
Dim decVal as integer
decVal = Val("&H" + Hex$(byte1).ToString + Hex$(byte2).ToString)
However, when I use this, the value (decVal) comes out to "-26215"
View 2 Replies
May 25, 2012
I am needing to write data to a usb device. The USB device uses a byte array (DataArray() as byte) to store the data. How can I assign a struct object (myStruct(0)) to the byte array. I run into the error "Value of type byte cannot be converted to 1-dimensional array of byte" when I try to cast the struct as a byte.
View 10 Replies
Jun 9, 2010
I'm calling a Windows API that gives me a byte array which represents a unicode string - the problem is that if I call Text.Encoding.ASCII.Get String on it I just get the first letter of the string. The reason for this is that byte array has an empty byte between each character. I've verified that removing these empty bytes resolves the problem by just using a simple For loop to add the bytes that do have a value in into a new byte array, then calling Text.Encoding.ASCII.GetString on that and that gives me the full string I'm expecting.I'm just wondering if there is any easier way of getting the working string though without having to do a loop and create a new byte array etc?
View 2 Replies
Sep 26, 2011
How to convert memorystream byte array to csv file. I have GetExport method that converts rpt file to excelformatted memorystream and then returns that as a byte array. Is there a way to convert this byte array to a csv file? Due to some reasons I can only pass this byte array back to calling function.
Public Function GetExport(ByVal reportID As ReportID) As Byte()
Dim byteArray As Byte() = Nothing
Using expRptDoc As ReportDocument = New ReportDocument()
Dim rptFileName As String = Server.MapPath(ReportCommand.GetXMLReportPath(reportID))
expRptDoc.Load(rptFileName)
[Code] .....
View 1 Replies
Mar 4, 2012
I am looking for something that will convert a byte array to a string and back, anything in-build that does it as this method would:
To string:
vb
Dim b = ByteArrSerialize(Obj, Compress, EncryptKey)
Dim sb As New System.Text.StringBuilder
For Each item In b
sb.Append(Chr(item))
Next
To byte:
vb
Dim b(0 To Len(TheString) - 1) As Byte
Dim bCount As Long = 0
For Each item In TheString
b(bCount) = Asc(item)
bCount += 1
Next
View 10 Replies
Nov 4, 2010
The following image or RTF file illustrates how I'm trying to transform an input Byte array into an output array of Booleans. This is basically what I have listed below. figure out how to get from one array to the next while shifting values of bytes within 8 byte groupings to shift low order to high order or vica versa. What I mean by that is best seen by looking at the illustration of actual input an required output. The reason for this is that the hardware devices have registers and the display which can be seen in the links above needs to convey the disabled sensor cable cell locations.
[Code]...
View 1 Replies
Apr 8, 2012
i do not know why data type image on ms sql is not working ! so i choose to use varchar(max) as my datatype to store images.. but i do now know how to load it on crystal report..
View 1 Replies
Sep 28, 2010
I can't seem to find how to recast a char to Ascii, the VB6 way was to ASC(thechar/thestring).What is the new method? or how to perform the recast while converting to a byte so it will fit and not buffer overrun.
View 10 Replies
Aug 7, 2010
Why does C# define Byte+Byte=Int32 instead of Byte+Byte=Byte like VB?
View 1 Replies
Nov 17, 2009
From what I read it is a good convention to name a method that returns a boolean value with the prefix of "is" or "has". So in keeping with this convention I am trying to name a method in my program with this prefix but I am running Specifically I have a class called Day. It is a simple class with a few data members and one method that returns a boolean value of true or false. The name of the boolean variable is isSpecialDay. This class has a method called isSpecialDay which takes the date of the day, applies some criteria to the date and then sets the variable isSpecialDay to true or false. My problem is that the boolean variable is named isSpecialDay and so it the method. What should I do?
Public Class Day
Private TheDate as String
Private DayName as String
[code].....
View 8 Replies
May 23, 2010
I've been trying to figure this out for hours now, and I've given up on trying to solve it myself. Basically what I need to do, is convert a "double octet" binary string, such as "11010011 01011101", into a byte or char type if possible.I use the following code to convert my Unicode text into binary:
Private Function ByteToStr(ByVal bt() As Byte) As String
Dim st As New StringBuilder
For Each byt As Byte In bt
[code]....
But that gives me weird results. The "Convert.FromBase64String" obviously isn't what I'm after. Any help would be great. If I can convert the "binary string" into a bytes or chars I can get it into a string.
View 6 Replies
Jan 21, 2011
I am in the process of converting some c# code to that of VB.NET...I am running into error at the following
[Code]...
View 2 Replies
May 23, 2010
I've been trying to figure this out for hours now, and I've given up on trying to solve it myself. Basically what I need to do, is convert a "double octet" binary string, such as "11010011 01011101", into a byte or char type if possible. I use the following code to convert my Unicode text into binary:
[Code]...
View 13 Replies
Aug 29, 2009
I am trying to communicate with an external device and i am trying to send a byte array to the external device via sockets but i am always getting a response the message size is too small so i am not sure what i have done wrong. Between the data type there should be no alignment present and all numbers are represented in little endian format. The char array is not null terminated as mentioned in the protocol specifications.
I have to send data based on a struct that embeds 2 other struct. So here's my vb.net code for the struct used to convert to byte array and the sending part.
Public Structure MESSAGETYPE_OIP_Login
Dim Header() As COMMANDHEADER
Dim UserName() As PSTRING
[Code]....
View 2 Replies