Exact match. Not showing close matches.
PICList
Thread
'[EE] Visual Basic 6.0 Questions'
2005\03\03@114357
by
Roberts II, Charles K.
Charles K Roberts II
Hello all
I use to have a nifty little program to do this but it was on another
machine and I lost that PC and didn't make a back up. So I am rehasing
the same problem. Here is what I am trying to do.
Take a text string from a text box that is a fixed point decimal number
between 0-3000 and convert it to a two byte hex number. Then I transmit
the two byte hex number via mscomm.
I remember doing it this way last time, I put the text in a Varian
variable then I made it a two byte hex number. I can't remember how I
did this.
I remember masking off the upper and lower bytes by XORing them with
FF00H and 00FFH. But there was some catch about the data types.
Any hints or suggestions, other than make back up copies next time,
would be helpful.
Chuck
2005\03\03@121022
by
Mario Mendes Jr.
|
Hi Charles,
I don't know how you did it before, and I am not a VB guys, but having
worked a lot with VC++ including v6, you can try this:
1. cast the variant as an INT (or copy it's value into an int variable)
2. AND the int with 0xFF, that gives you byte 1
3. shift int number 8 places to the right
4. AND the result of the shift with 0xFF, that gives you byte 2
Good luck.
-Mario
{Quote hidden}>
>
> Charles K Roberts II
> Hello all
>
> I use to have a nifty little program to do this but it was on another
> machine and I lost that PC and didn't make a back up. So I am rehasing
> the same problem. Here is what I am trying to do.
>
> Take a text string from a text box that is a fixed point decimal number
> between 0-3000 and convert it to a two byte hex number. Then I transmit
> the two byte hex number via mscomm.
>
> I remember doing it this way last time, I put the text in a Varian
> variable then I made it a two byte hex number. I can't remember how I
> did this.
>
> I remember masking off the upper and lower bytes by XORing them with
> FF00H and 00FFH. But there was some catch about the data types.
>
> Any hints or suggestions, other than make back up copies next time,
> would be helpful.
>
>
> Chuck
>
> -
2005\03\03@125103
by
Paul Hutchinson
Use VB's built-in Hex() function, it accepts a number and returns a string
representing the hexadecimal value of a number.
Example:
Create a form with a text box named "Text1" and a label named "Label1".
Add a command button to the form and put the following code in the buttons
click event.
------------------------------------
Dim strHexVal As String
strHexVal = Hex(Val(Text1.Text))
'Add leading zeros to make it 4 characters long
strHexVal = String(4 - Len(strHexVal), "0") & strHexVal
Label1.Caption = strHexVal
------------------------------------
Run the program, put a number in the text box, click the button and, the
four character hex equivalent appears in the label.
Hope this helps,
Paul Hutch
> {Original Message removed}
2005\03\03@125143
by
Phillip Vogel
How about:
string = hex$(number)
if len(string) = 1 then string = "0"+string
something like that.
Phillip
http://www.visualconductor.com
{Original Message removed}
2005\03\03@140148
by
Bradley Ferguson
|
On Thu, 03 Mar 2005 11:43:43 -0500, Roberts II, Charles K.
<spam_OUTrobertsckTakeThisOuT
ornl.gov> wrote:
> I use to have a nifty little program to do this but it was on another
> machine and I lost that PC and didn't make a back up. So I am rehasing
> the same problem. Here is what I am trying to do.
>
> Take a text string from a text box that is a fixed point decimal number
> between 0-3000 and convert it to a two byte hex number. Then I transmit
> the two byte hex number via mscomm.
>
> I remember doing it this way last time, I put the text in a Varian
> variable then I made it a two byte hex number. I can't remember how I
> did this.
>
> I remember masking off the upper and lower bytes by XORing them with
> FF00H and 00FFH. But there was some catch about the data types.
Other people have brought up the Hex() that will do the conversion.
There are a few issues that you might bump into, though. As I recall
these are:
1. A long variable is a signed 8 byte number, which means VB will
complain about overflow above 2147483647. The way around this is to
OR &h80000000 into the variable if you need to. This shouldn't come
up for you with small numbers, but I recently bumped into it in doing
mchip float conversions in VB.
2. The internal constants for VB use two byte numbers. This means
that if you AND &HFF00 with a number it will be sign extended to
&HFFFFFF00, which is not generally what you want. I simply use 65280
as VB will not see this as a negative number.
3. I generally transfer byte arrays to the comm control, which has
always worked. Strings might work, but I'm never quite sure when VB
is doing a hidden Unicode conversion.
I'm not sure that you want to actually convert to hex at any point.
If you convert to hex, you'll actually have ascii characters and not
the binary representation of the number. If you just wanted to split
a number up into high byte, low byte, then you can do that just with
math:
byteH = (longValue \ 256) And 255
byteL = longValue And 255
The backslash (\) does integer division and dividing by 256 = 2^8 does
a right shift of 8 bits. Anding this with 255 = &HFF masks the lower
bits.
Bradley
2005\03\03@171603
by
Paul Hutchinson
After reading other replies and re-reading the original message I'm not sure
what is needed. I had thought that a four character string containing the
hexadecimal text was desired and, that's what the code in my previous post
gives.
If what is needed is an integer converted to a two byte string suitable for
binary storage/transmission then, the MKI$() function from DOS versions of
MS Basic is probably what you want. A VB compatible version of the function
is available here:
http://www.hutchrick.com/MKI.htm
A VB compatible version of the inverse function from DOS Basic, CVI%(), is
also available at, http://www.hutchrick.com.
Paul Hutch
> {Original Message removed}
2005\03\04@070305
by
Jason Graham
Robert
>From: "Roberts II, Charles K."
>
>
>Take a text string from a text box that is a fixed point decimal number
>between 0-3000 and convert it to a two byte hex number. Then I transmit
>the two byte hex number via mscomm.
0 to 3000 requires a 12 bit binary number which translates to 3 hexidecimal
characters. This means masking the last 4 bits of a two byte binary value.
......AND the 2 bytes with 00FF FFF
In order to convert decimal to hex try,
somevariable = hex(decimalnumber)
In order to convert back, append string in a text box with "&H"
hexval = "&H" + textstring
Hope this helps
Jason
In order to
>
>I remember masking off the upper and lower bytes by XORing them with
>FF00H and 00FFH. But there was some catch about the data types.
>
>Any hints or suggestions, other than make back up copies next time,
>would be helpful.
>
>
>Chuck
>
>
More... (looser matching)
- Last day of these posts
- In 2005
, 2006 only
- Today
- New search...