DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Converting Hex values to Decimal and Character

1. Jon Oliver Guest

## Re: Converting Hex values to Decimal and Character

The value statement will evaluate hexadecimal notation, but
using a different form:

Print Val("&H41")
65

Print Chr\$(Val("&H41"))
A

"Brad Markisohn" <dbmarkisohn@INDesign-LLC.com> wrote in message
news:3c2242c2@147.208.176.211...
> I have a hex value, for example 0x41. This has an equivalent

decimal value
> of 65 and a character representation of A. How can I get the

character
> representation of 0x41?
>
>
>
>

## Converting Hex values to Decimal and Character

I have a hex value, for example 0x41. This has an equivalent decimal value
of 65 and a character representation of A. How can I get the character
representation of 0x41?

## Re: Converting Hex values to Decimal and Character

Jon,

Thanks for the quick answer to a simple problem.

"Jon Oliver" <joliver@no.spam.maam.com> wrote in message
news:3c224322@147.208.176.211...
> The value statement will evaluate hexadecimal notation, but
> using a different form:
>
> Print Val("&H41")
> 65
>
> Print Chr\$(Val("&H41"))
> A
>
> "Brad Markisohn" <dbmarkisohn@INDesign-LLC.com> wrote in message
> news:3c2242c2@147.208.176.211...
> > I have a hex value, for example 0x41. This has an equivalent

> decimal value
> > of 65 and a character representation of A. How can I get the

> character
> > representation of 0x41?
> >
> >
> >
> >

>
>

4. Jon Oliver Guest

## Re: Converting Hex values to Decimal and Character

"Brad Markisohn" <dbmarkisohn@INDesign-LLC.com> wrote in message
news:3c2257ed\$1@147.208.176.211...
> Jon,
>
> Thanks for the quick answer to a simple problem.

I never know whether to say "thanks for the thanks," so I'll
just try it and see if it turns into the neverending phone
call...

5. Rick Rothstein Guest

## Re: Converting Hex values to Decimal and Character

> Print Chr\$(Val("&H41"))
> A

VB understands &h41 as the number 65 intrinsically; so you could leave the Val function
out and it will still work

Print Chr\$("&H41")
A

Rick

6. Phil Weber Guest

## Re: Converting Hex values to Decimal and Character

> Print Chr\$("&H41")

Rick: You also don't need the quotes around &H41; the following also works
(and, I suspect, skips a string-to-numeric conversion):

Print Chr\$(&H41)

---
Phil Weber

7. Rick Rothstein Guest

## Re: Converting Hex values to Decimal and Character

I know the quotes aren't needed; but then you would have a direct string constant and you
might as well use the letter "A" instead. I don't think there is anyway to feed a hex
value (in the shape of a hex value; that is, with the ampersand "H" part) into the Chr\$
function, as inputted from a user (which I assume is the ultimate goal) without it being a
String value. Perhaps my post would have been clearer on this point if I had written it
like this

Dim HexValueInputtedFromUserInSayATextBox As String
HexValueInputtedFromUserInSayATextBox = "&H41"
Print Chr\$(HexValueInputtedFromUserInSayATextBox )

<g>

Rick

"Phil Weber" <pweberonline@fawcette.com> wrote in message news:3c22c66c@147.208.176.211...
> > Print Chr\$("&H41")

>
> Rick: You also don't need the quotes around &H41; the following also works
> (and, I suspect, skips a string-to-numeric conversion):
>
> Print Chr\$(&H41)
>
> ---
> Phil Weber
>
>

8. Bob Butler Guest

## Re: Converting Hex values to Decimal and Character

"Rick Rothstein" <rick_newsgroup@email.com> wrote in message
news:3c22ae5f@147.208.176.211...
> > Print Chr\$(Val("&H41"))
> > A

>
> VB understands &h41 as the number 65 intrinsically; so you could leave the

Val function
> out and it will still work
>
> Print Chr\$("&H41")
> A

But now you've added an implicit conversion from string to numeric which I

Me.Print Chr\$(&H41)

Then I'd agree with your post completely <g>

9. Bob Butler Guest

## Re: Converting Hex values to Decimal and Character

"Rick Rothstein" <rick_newsgroup@email.com> wrote in message
news:3c232dc7@147.208.176.211...
> I know the quotes aren't needed; but then you would have a direct string

constant
<cut>

The constant &H41 is not a string constant, it's as numeric as 65 is.

10. Dean Earley Guest

## Re: Converting Hex values to Decimal and Character

"Phil Weber" <pweberonline@fawcette.com> wrote in message news:3c22c66c@147.208.176.211...
> > Print Chr\$("&H41")

>
> Rick: You also don't need the quotes around &H41; the following also works
> (and, I suspect, skips a string-to-numeric conversion):
>
> Print Chr\$(&H41)

But it does show you can do this...

Dim HexValue As String
HexValue = "41"
Print Chr\$("&H" & HexValue)

--
Dean Earley (dean.earley@icode.co.uk)
Assistant Developer

iCode Systems

11. Rick Rothstein Guest

## Re: Converting Hex values to Decimal and Character

> The constant &H41 is not a string constant, it's as numeric as 65 is.

The word "constant" was meant to apply to the Chr\$("&H41"); hence the reference to using
"A" instead. The point I was trying to make was that the user of the program can't *enter*
&H41 without it being a String (VB's evil type conversions during assignments to
non-String variables notwithstanding). So, while Print Chr\$(&H41) is valid, it is not
readily usable in that form when taking input from a user.

Rick

12. Bob Butler Guest

## Re: Converting Hex values to Decimal and Character

"Rick Rothstein" <rick_newsgroup@email.com> wrote in message
news:3c23611f\$1@147.208.176.211...
> > The constant &H41 is not a string constant, it's as numeric as 65 is.

>
> The word "constant" was meant to apply to the Chr\$("&H41");

OK, that just wasn't clear. I assumed you meant it in the "VB sense" in
which case it only applied to "&H41" or &H41.

> hence the reference to using
> "A" instead. The point I was trying to make was that the user of the

program can't *enter*
> &H41 without it being a String (VB's evil type conversions during

assignments to
> non-String variables notwithstanding). So, while Print Chr\$(&H41) is

valid, it is not
> readily usable in that form when taking input from a user.

I'll buy that, but I don't remember the question being limited to
user-enterable strings so you can't wiggle out taht easily oh great
kludgemaster <g>

BTW, I would still maintain that Chr\$(Clng("&H41")) is better than
Chr\$("&H41") since it explicitly acknowledges that a conversion is being
done and eliminates any question about what rules VB should use for that
conversion.

13. Rick Rothstein Guest

## Re: Converting Hex values to Decimal and Character

I think my point may have gotten lost somewhere. What I am trying to say is that a user
can't input &H41 directly as a number (there is no variable of type Hex that it can be
assigned to). He/she can only input a hex number as a String via a text component of some
sort. So, while Print Chr\$(&H41) is a valid construction, it can only exist within code as
a constant-type expression, for which the character "A" could be used directly instead.

Rick

"Bob Butler" <butlerbob@earthlink.net> wrote in message news:3c233928\$1@147.208.176.211...
>
> "Rick Rothstein" <rick_newsgroup@email.com> wrote in message
> news:3c22ae5f@147.208.176.211...
> > > Print Chr\$(Val("&H41"))
> > > A

> >
> > VB understands &h41 as the number 65 intrinsically; so you could leave the

> Val function
> > out and it will still work
> >
> > Print Chr\$("&H41")
> > A

>
> But now you've added an implicit conversion from string to numeric which I
>
> Me.Print Chr\$(&H41)
>
> Then I'd agree with your post completely <g>
>
>
>

14. Bob Butler Guest

## Re: Converting Hex values to Decimal and Character

"Rick Rothstein" <rick_newsgroup@email.com> wrote in message
news:3c2363dc\$1@147.208.176.211...
> I think my point may have gotten lost somewhere. What I am trying to say

is that a user
> can't input &H41 directly as a number (there is no variable of type Hex

that it can be
> assigned to). He/she can only input a hex number as a String via a text

component of some
> sort. So, while Print Chr\$(&H41) is a valid construction, it can only

exist within code as
> a constant-type expression, for which the character "A" could be used

Correct, but my point is that you are using VB's evil implicit type coercion
by leaving out VAL (or some similar explicit conversion); it may work OK
here but IMO it's a bad practice. If you have a string value that contains
something which can be converted to a numeric value without error then it is
better (again, IMO) to use the explicit conversion.

15. Rick Rothstein Guest

## Re: Converting Hex values to Decimal and Character

> Correct, but my point is that you are using VB's evil implicit type coercion
> by leaving out VAL (or some similar explicit conversion); it may work OK
> here but IMO it's a bad practice. If you have a string value that contains
> something which can be converted to a numeric value without error then it is
> better (again, IMO) to use the explicit conversion.

You are probably right, but there are some intrinsic internal conversions that VB performs
and which I readily accept. The Chr\$ function (as well as several other String functions)
has been around since day one of VB as well as its BASIC predecessor. The code is stable
and the internal conversion (string to number) is error-free. I know that and I trust it.
This is also true for using Strings in date form within Date functions (remember, I do no
international programming, so my dates are *always* in USA regional format). I don't need
to do this

Print DateDiff("m", CDate(Text1.Text), CDate(Text2.Text))

(let's assume I have validation code elsewhere controlling the shape of the user's entries
in those TextBoxes) when VB is nice enough to do it for me when I do this instead

Print DateDiff("m", Text1.Text, Text2.Text)

In a similar way, I have no problem doing this

Dim IntVar As Integer
Dim LongVar As Long
IntVar = 4
LongVar = IntVar

rather than

LongVar = CLng(IntVar)

comfortable with, across the years. I know the problem areas by rote now because I
developed and refined these bad habits across some 20 years of programming. (Yeah, I know
I'm in for a big problem if I ever move to VB.NET; which, as things look now, I won't be
doing.)

Oh, and I've mentioned in many prior posts that I am a "lazy" typist. I consider *great*,
anything that can cut down on the amount of typing I have to do. All of the above
accomplish that for me and my weary little fingers. <g>

Rick

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise