Results 1 to 6 of 6

Thread: extracting hexadecimal data from string

  1. #1
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    65
    Posts
    37
    Rep Power
    4

    extracting hexadecimal data from string

    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
    Last edited by dco045; 30-01-2018 at 19:42.

  2. #2
    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
    

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,281
    Blog Entries
    2
    Rep Power
    10
    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/prod...es_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/prod...?variables.htm

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  4. #4
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    65
    Posts
    37
    Rep Power
    4
    Quote Originally Posted by primo View Post
    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

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,281
    Blog Entries
    2
    Rep Power
    10
    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)
    
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  6. #6
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    65
    Posts
    37
    Rep Power
    4
    Quote Originally Posted by ErosOlmi View Post
    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

Similar Threads

  1. The age of big data
    By Charles Pegge in forum Shout Box Area
    Replies: 0
    Last Post: 07-01-2014, 10:24
  2. String-in-String-Pointers?
    By ReneMiner in forum thinBasic General
    Replies: 9
    Last Post: 11-06-2013, 14:55
  3. Replies: 5
    Last Post: 13-12-2010, 00:19
  4. Hexadecimal
    By Charles Pegge in forum O2h Compiler
    Replies: 0
    Last Post: 17-03-2009, 15:40
  5. Data trigger and data events
    By ErosOlmi in forum TBEM module - thinBasic Event Manager module
    Replies: 9
    Last Post: 10-10-2008, 22:54

Posting Permissions

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