String to Hex

  • Thread starter Thread starter Jerry Spence1
  • Start date Start date
J

Jerry Spence1

I have the string "15" which I need represented as a hex number - ie &H15

Simple I would have thought but I can't find the answer.

-Jerry
 
I have the string "15" which I need represented as a hex number - ie &H15

Simple I would have thought but I can't find the answer.

-Jerry

It would be:

Dim int As Integer = Integer.Parse("15",
Globalization.NumberStyles.HexNumber)

If I remember correctly

Thanks,

Seth Rowe
 
rowe_newsgroups said:
It would be:

Dim int As Integer = Integer.Parse("15",
Globalization.NumberStyles.HexNumber)

If I remember correctly

Thanks,

Seth Rowe
That converts the number to 21. I need to keep 15. I have tried starting
with a string of "&H15" and playing around with that but still can't seem to
do it.

-Jerry
 
That converts the number to 21. I need to keep 15. I have tried starting
with a string of "&H15" and playing around with that but still can't seem to
do it.

-Jerry

Sorry, I apparently misunderstood what you are trying to do.

As a matter of fact - I still have no idea what you are trying to do.
Could you try explaining in more detail what exactly you are trying to
do and why?

Thanks,

Seth Rowe
 
That converts the number to 21. I need to keep 15. I have tried
starting with a string of "&H15" and playing around with that but
still can't seem to do it.


&H15 *is* 21 in decimal (and integers are stored in decimal)

If you're just after the string "&H15" from a string of "15" then just add
"&H" on the front..

I guess we're just not clear on what you want.

Perhaps it would help if you explain what you would like to do with what
you end up with?
 
rowe_newsgroups said:
Sorry, I apparently misunderstood what you are trying to do.

As a matter of fact - I still have no idea what you are trying to do.
Could you try explaining in more detail what exactly you are trying to
do and why?

Thanks,

Seth Rowe

Thanks Seth

I have "15" as a string. I need to do some XOR and ANDing of bits. I can't
directly do:

"15" AND 01

so I need to make 15 a hex value first (&H15). I feel I need something like
ctype(integer,hexvalue) if such a thing existed. I'm sure I will kick myself
when I find the answer.

-Jerry
 
Thanks Seth

I have "15" as a string. I need to do some XOR and ANDing of bits. I can't
directly do:

"15" AND 01

so I need to make 15 a hex value first (&H15). I feel I need something like
ctype(integer,hexvalue) if such a thing existed. I'm sure I will kick myself
when I find the answer.

-Jerry

Can't you just use the byte datatype instead of string?

Thanks,

Seth Rowe
 
I have "15" as a string. I need to do some XOR and ANDing of bits. I
can't directly do:

"15" AND 01

so I need to make 15 a hex value first (&H15). I feel I need
something like ctype(integer,hexvalue) if such a thing existed. I'm
sure I will kick myself when I find the answer.

Bitwise ANDing of 2 numbers should work regardless of whether they are decimal
or hex because internally the mahince only understands nbbinary anyway.
Thus
-------------------------------------
Dim Result as Integer = CInt("&H" & "15") AND CInt("01")
-------------------------------------

This is untested but should result in "Result" containing an integer in this
case 1 (Also 1 in HEX)

Does this help?
 
Jerry Spence1 said:
Thanks Seth

I have "15" as a string. I need to do some XOR and ANDing of bits. I
can't directly do:

"15" AND 01

so I need to make 15 a hex value first (&H15). I feel I need
something like ctype(integer,hexvalue) if such a thing existed. I'm
sure I will kick myself when I find the answer.

Why is "15" there as a string not as a number? If that's not avoidable, use
Convert.ToInt32 to convert it to an integer first, then use numeric
operators.

dim number as integer

number = convert.toint32("15", 16) '16 is the base (hexadecimal)
number = number and 1




Armin
 
Rory Becker said:
Bitwise ANDing of 2 numbers should work regardless of whether they are
decimal or hex because internally the mahince only understands nbbinary
anyway.
Thus -------------------------------------
Dim Result as Integer = CInt("&H" & "15") AND CInt("01")
-------------------------------------

This is untested but should result in "Result" containing an integer in
this case 1 (Also 1 in HEX)

Does this help?
Thanks Rory - yes it does help. I didn't realise that I could so bitwise
comparisons on a decimal number. It works OK.

Thanks a lot and thanks to Seth for your input as well.

-Jerry
 
Armin Zingler said:
Why is "15" there as a string not as a number? If that's not avoidable,
use Convert.ToInt32 to convert it to an integer first, then use numeric
operators.

dim number as integer

number = convert.toint32("15", 16) '16 is the base (hexadecimal)
number = number and 1




Armin
That's a neat trick. Thanks Armin

-Jerry
 
Hi Jerry,
To convert a string into HEX you need to it char by char ie: convert every
char into it's ascii value then convert the ascii to HEX:
here is the code:

Private Sub txtNormal_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txtNormal.TextChanged

Dim sTemp As String

Dim sTemp2 As StringBuilder = New StringBuilder

For i As Integer = 0 To Me.txtNormal.Text.Length - 1

sTemp = Me.txtNormal.Text.Substring(i, 1)

sTemp2.Append(Hex(Asc(sTemp)))

Next

Me.txtHEX.Text = sTemp2.ToString()

End Sub

hope this will help

cheers
 
Jerry said:
Thanks Rory - yes it does help. I didn't realise that I could so bitwise
comparisons on a decimal number. It works OK.

Actually it's not a decimal number at all, it's just an integer.

Decimal and hexadecimal are two ways of representing numbers as text. As
long as you handle the integer as an integer, it doesn't have any
textual representation at all, it's just some bits in memory.

Whenever you display the value of an integer, a string is created that
is the textual representation of the integer, as the integer itself
can't be displayed as text.
 
Back
Top