PDA

View Full Version : extracting hexadecimal data from string



dco045
30-01-2018, 19:41
Hi

Does a function as "val(string)" already exists for extracting number from hexadecimal string ?
Or do I write it by myself ?


hex_str = "&h100"
numb = hexval( hex_str)
if numb = 256 then printl "I am happy"


Regards



Dany

primo
30-01-2018, 22:05
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

ErosOlmi
31-01-2018, 00:23
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:


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

Also check http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?equates_numeric.htm to see how to handle numeric constants.
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/products/thinBasic/help/html/index.html?variables.htm

Ciao
Eros

dco045
01-02-2018, 00:56
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

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 = "&h10b0"
numb = char_str
PrintL char_str , numb , Val(char_str) ' result 4272 OK

char_str = "&x10b0" ' result 0 , OK format &x not listed

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

ErosOlmi
01-02-2018, 23:17
Not that way.
If you embed hex or binary notation inside double quote like

"&h100"
it will be assumed to be just the string &h100 and not its equivalent numeric

Hex and bin notation is used to assign numeric hex/bin to numbers like:

Long MyLong = &h10

If you want to generate a string from hex numbers you can use something like the following that concatenates single hex numbers:

string MyString = CHR$(&h20, &h0c, &h00, &h0f)

dco045
03-02-2018, 01:12
Not that way.
If you embed hex or binary notation inside double quote like

"&h100"
it will be assumed to be just the string &h100 and not its equivalent numeric

Hex and bin notation is used to assign numeric hex/bin to numbers like:

Long MyLong = &h10

If you want to generate a string from hex numbers you can use something like the following that concatenates single hex numbers:

string MyString = CHR$(&h20, &h0c, &h00, &h0f)


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