B4R Question string to int convertion

Discussion in 'B4R Questions' started by Tayfur, Jun 28, 2019.

  1. Tayfur

    Tayfur Well-Known Member Licensed User

    Buffer() As Byte is >> "192.168.1.123" (this is 13byte)

    And I want use and convert like it

    Dim b() As Byte = Array As Byte(192, 168, 1, 123)

    how can i convert?
     
  2. janderkan

    janderkan Active Member Licensed User

    b(0) = (Buffer(1)-48)*100 + (Buffer(1)-48)*10 + (Buffer(2)-48

    if b(x)= 46 -> advance to next
     
  3. Tayfur

    Tayfur Well-Known Member Licensed User

    sorry ı dont understand.

    buffer length is 13
     
  4. janderkan

    janderkan Active Member Licensed User

    The Buffer will contain these 13 bytes : Array as byte(49,57,50,46,49,54,56,46,49,46,49,50,51)

    First byte is hundreds, second is tenths and third is ones
    So you take the value and subtract 48
    b(0) = (49-48)*100 + (57-48)*10 + 50-48 = 192
     
    Erel, Johan Schoeman and tigrot like this.
  5. Mostez

    Mostez Active Member

    output looks like this:

    AppStart
    192
    168
    1
    123


    Code:
    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'Public variables can be accessed from all modules.
        Public Serial1 As Serial
        
    Private BC As ByteConverter
       
    End Sub

    Private Sub AppStart
        Serial1.Initialize(
    115200)
        
    Log("AppStart")
        SplitIP(
    "192.168.1.123")
    End Sub


    Sub SplitIP(IPbuffer() As Byte )
        
    Dim IP(4As Byte 
        
    Dim idx As Byte 'array index counter
        For Each s() As Byte In BC.Split(IPbuffer, "."'split string and get each byte of IP address
            Dim IPval As Byte = Bit.ParseInt(BC.StringFromBytes(s),10'get value
            IP(idx) = IPval 'IP array holds the four bytes of IP address
            Log(IP(idx))
            idx = idx + 
    1
        
    Next
       
    End Sub
     
    Tayfur, MikeSW17 and janderkan like this.
  6. Tayfur

    Tayfur Well-Known Member Licensed User

    this great . thanks.
    I thinking like your sample. Split function added.

    I shearced on web for Ardiunino ide.
    İt has a ".toint" function
    https://circuits4you.com/2018/03/09/how-to-convert-int-to-string-on-arduino/


    Code:
    Example 1: Integer to String Conversion Arduino
    int a = 
    1234;
    String myStr;
    myStr = 
    String(a);   //Converts integer to string

    Example 
    2String to Integer conversion Arduino
    String val = “1234”;
    int result = val.toInt();  //Converts 
    string to integer


    does B4R have a same fonctions.
     
  7. Mostez

    Mostez Active Member

    yes, to convert integer(or any number) to string use NumberFormat, to convert byte array or string to integer use Bit.ParseInt
     
    Tayfur and janderkan like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice