Hi
Does a function as "val(string)" already exists for extracting number from hexadecimal string ?
Or do I write it by myself ?
Regardshex_str = "&h100" numb = hexval( hex_str) if numb = 256 then printl "I am happy"
Dany
Hi
Does a function as "val(string)" already exists for extracting number from hexadecimal string ?
Or do I write it by myself ?
Regardshex_str = "&h100" numb = hexval( hex_str) if numb = 256 then printl "I am happy"
Dany
Last edited by dco045; 30-01-2018 at 19:42.
Hi
the already existing Val function can convert hex string to decimal.
also you can assign a hex string to a Long type variable and it will automaticaly convert the string to decimal number (like in perl language) ie variables (in context)
your example:
'---Load Console Module Uses "Console" String hex_str = "&h100" 'Dim As String hex_str = "&h100" Long numb = hex_str PrintL numb If numb = 256 Then PrintL "I am happy" PrintL Val(hex_str) PrintL "Press a key to end program" '---Wait for a key press WaitKey
Thanks Primo.
Yes, wherever possible thinBasic try to do automatic conversion string/number/string without the need to use VAL or STR$
So you can do like:
Also check http://www.thinbasic.com/public/prod...es_numeric.htm to see how to handle numeric constants.Long MyLong = "1234" '---Automatic implicit conversion String MyString = 1234 '---Automatic implicit conversion MyLong = MyString '---Automatic implicit conversion from a string to a number MyString = MyLong '---Automatic implicit conversion from a number to a string
You can use hex numbers both using Basic notation (&H...) or C notation (0X...)
Check the many kind of native variable types (numeric or string) thinBasic has.
http://www.thinbasic.com/public/prod...?variables.htm
Ciao
Eros
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000
Ok , thanks
> > > > But, Eros says :
Also check http://www.thinbasic.com/public/prod...es_numeric.htm to see how to handle numeric constants.%MY_CONST = 0X200c000f As Long PrintL Hex$(%MY_CONST) , %MY_CONST ' works fine %MY_OTHER_CONST = &h200c000f As Long PrintL Hex$(%MY_OTHER_CONST) , %MY_OTHER_CONST ' works fine
You can use hex numbers both using Basic notation (&H...) or C notation (0X...)
char_str = "&x10b0" ' result 0 , OK format &x not listedchar_str = "&h10b0" numb = char_str PrintL char_str , numb , Val(char_str) ' result 4272 OK
char_str = "0x10b0" ' result 0 , But -> what about Eros remark on C notation ? works with constant, not variables ?
char_str = "0X10B0" ' result 0 , I also tried Uppercase if......
char_str = "0h10b0" ' result 0 , OK Format not listed
char_str = "&b10a0" ' result 2 , OK 'a' is not a binary digit
char_str = "&b1002" ' result 4 , OK '2' is not a binary digit
I think I will write a conversion routine since I have also to cope with assembler '$' notation.
And reject invalid chars in string should be considered as fatal error.
Regards
Dany
Not that way.
If you embed hex or binary notation inside double quote like
it will be assumed to be just the string &h100 and not its equivalent numeric"&h100"
Hex and bin notation is used to assign numeric hex/bin to numbers like:
If you want to generate a string from hex numbers you can use something like the following that concatenates single hex numbers:Long MyLong = &h10
string MyString = CHR$(&h20, &h0c, &h00, &h0f)
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000
Hi Eros ,
In my example, I set strings variables by chr_str = "something" . It was only for testing purpose.
But in my real prog, the string value is loaded from a file whose format for an hexadecimal value may be :
$4A3F - - uppercase
$4a3f - - lowercase
$0c4D - - mixed case
and also
0x12dd - - lower case for X
0X12cc - - upper
0h1b000da45da58e0a47afc6e3 Any length , the context will define how many digits are kept
and after reviewing specs I have also to cope with OCTAL base and binary strings too.
In reply to you last sentence, the goal in not to convert num to hex strings but hex strings to num. and also octal strings.
And illegal chars in strings should generate an error.
So it seems easier to write my own conversion routine.
Regards
Dany
Bookmarks