For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > February 2005 > Visual Basic Hex conversion









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Visual Basic Hex conversion
JWH3535

2005-02-25, 3:56 pm

I am getting hex numbers in from an RS-232 source and coverting them to store
in a database. The numbers are 4 bytes long, but often have non-zero values
in the rightmost 2 bytes. Is there any standard coversion method that gets
around the following anomoly?
?hex(32768)
8000
?&H8000
-32768
?&H00008000
-32768
I have to check for this bit set and subtract it. Do the conversion and add
32768 back in.
Best regards,
John

Bob Butler

2005-02-25, 3:56 pm

"JWH3535" <JWH3535@discussions.microsoft.com> wrote in message
news:BCCC8240-8533-4EFD-9396-48383A0D0534@microsoft.com
> I am getting hex numbers in from an RS-232 source and coverting them
> to store in a database. The numbers are 4 bytes long, but often have
> non-zero values in the rightmost 2 bytes. Is there any standard
> coversion method that gets around the following anomoly?
> ?hex(32768)
> 8000
> ?&H8000
> -32768


?&H8000&
32768

Since '&H8000' fits into 16 bits that's what VB uses by default and since
the high bit is set it gets treated as a negative number. Appending &
forces VB to store it in 32 bits instead.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."

Duane Bozarth

2005-02-25, 3:56 pm

Bob Butler wrote:
>
> "JWH3535" <JWH3535@discussions.microsoft.com> wrote in message
> news:BCCC8240-8533-4EFD-9396-48383A0D0534@microsoft.com
>
> ?&H8000&
> 32768
>
> Since '&H8000' fits into 16 bits that's what VB uses by default and since
> the high bit is set it gets treated as a negative number. Appending &
> forces VB to store it in 32 bits instead.


I'd only add that if(f) your external device is unsigned 32-bit integers
you'll have the same issue if the sign bit is set there as well. If
they're signed, no problem.
JWH3535

2005-02-25, 3:56 pm

Found my own answer:
?&H00008000&
32768
Appending an '&' treats the data as a long.


"JWH3535" wrote:

> I am getting hex numbers in from an RS-232 source and coverting them to store
> in a database. The numbers are 4 bytes long, but often have non-zero values
> in the rightmost 2 bytes. Is there any standard coversion method that gets
> around the following anomoly?
> ?hex(32768)
> 8000
> ?&H8000
> -32768
> ?&H00008000
> -32768
> I have to check for this bit set and subtract it. Do the conversion and add
> 32768 back in.
> Best regards,
> John
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com