smartsrgr

updated

10 years ago

S

smartsrgr

5 Posts

0

2788

August 2nd, 2016 04:00

ASL Change string with binary data to numbers

@

From an snmptrap we get an octetstring with the ip address as hex binary digits i.e. 127.0.0.1 would be 4 bytes the first having 127 as value then two zero bytes and the last byte being 1.

In the console this will show up as the ascii equivalent of those characters (i.e. usually garbage unless something printable like 65 which would show up as 'A'). What we actually would like is that it would just show as the ip address.

I looked through the ASL manual but could not find a function to (what in other languages is usually called ord() ) of a character. The only function I could find was hextToString but that just displays the string above with any non-ascii removed.

Anyone know if this is possible?

  • RaghuBT

    1 Message

    2108

    0

    Posted October 19th, 2016 05:00

    Hi Ron,

    I think you can use the utility functions of SM_System::SM-System. This object is readily available to ASL.

    You can use bitwise operators along with byte conversions to extract all the octets of string.

        byteToString byteval

        stringToByte str

        bitOr v1 v2

        bitComplement v

        bitRUShift v b

        bitRRotate v b

    Regards,

    Raghu

  • nbruce

    53 Posts

    2107

    0

    Posted August 8th, 2016 08:00

    Unfortunately there isn't an easy straight forward method to convert the Binary data from a string into numbers.  The only way to accomplish this is via ASL.  I will get you an example script.

  • smartsrgr

    5 Posts

    2107

    0

    Posted August 10th, 2016 10:00

    Hi Nate,

    That would be great!

    I already have an ASL script hooked up to the event but couldn't figure out how to get the underlying numbers from the string. Looking through the ASL manual I didn't find anything else than using substring to get one char, but can't think of  way to change that into a number.

    Regards, Ron

  • nbruce

    53 Posts

    2107

    0

    Posted August 12th, 2016 07:00

    Haven't forgotten about you.  This isn't documented because its not easily done.  But I'm trying to find a work around for you.

  • nbruce

    53 Posts

    2108

    1

    Posted August 24th, 2016 07:00

    alright I have a sample asl file that will convert a binary string to its equivalent decimal value.  Hopefully this will help.

    Here is a Sample output from testing:

    ./sm_adapter.exe binary2decimal.asl |tee

    Binary to decimal conversion

    101

    10

    I hope this get you in the right direction.

    Cheers for you patience.

    here is the script.... START() {   .. eol } do { print("Binary to decimal conversion"); BINARYSTRING_TO_NUMERIC(1100101); BINARYSTRING_TO_NUMERIC(1010); } BINARYSTRING_TO_NUMERIC(binaryString) do {     charIndx = sizeOf(binaryString) - 1;     multiplier = 1;     numericValue = 0;     while (charIndx > -1) {     //print(charIndx);         numericValue = numericValue + numeric(substring(binaryString, charIndx, 1)) * multiplier;         multiplier = multiplier * 2;         charIndx = charIndx -1;     }     print(numericValue); }

  • smartsrgr

    5 Posts

    2108

    0

    Posted October 11th, 2016 03:00

    Hi Nate,

    Sorry, for the late reply (I was off for a few weeks). Thank you for your script, I studied it (good learning material :-)  ) however it seems to turn a string with binary number like '10100010' into a decimal.

    My problem is unfortunately that from a trap we get a string where the characters themselves are the decimals so in C++ it would be

    http://cpp.sh/9ifnk

    // 20161011 What we need in smarts some function like CastNumSubstr( String, CharOffset )

    #include

    #include

    using namespace std;

    int main()

    {

      std::string IPAddress;

      IPAddress.resize( 4 );

      IPAddress[ 0 ] = static_cast( 172 );

      IPAddress[ 1 ] = static_cast( 0   );

      IPAddress[ 2 ] = static_cast( 0   );

      IPAddress[ 3 ] = static_cast( 1   );

      cout << "-- String we have in smarts (well actually smarts probably uses UTF8 so 'wchar' but the idea is the same) " << endl;

      std::cout << IPAddress << endl;

      cout << "--  What we need " << endl;

      std::cout << static_cast( static_cast ( IPAddress[ 0 ] ) ) << "."

                << static_cast( static_cast ( IPAddress[ 1 ] ) ) << "."

                << static_cast( static_cast ( IPAddress[ 2 ] ) ) << "."

                << static_cast( static_cast ( IPAddress[ 3 ] ) ) << endl

                ;

      // Just prevent terminal from closing directly

      {

        string Dummy;

        cout << endl << "Press enter to close program: ";

        getline (std::cin, Dummy );

      }

      return 0;

    }

    Regards, Ron

  • smartsrgr

    5 Posts

    2108

    0

    Posted October 20th, 2016 05:00

    Hi Raghu,

    Great, thanks! It worked. I created an example below with the suggestion you gave me in case someone else has the same problem.

    Example created with the advice from Raghu

    (this is for changing binary data into a readable string for converting a string with a binary number to decimal see Nate's answer above.)

    C++ code to create a test file execute as:
    g++ test.cc ./a.out

    Code:

    #include 
    
    using namespace std;
    
    
    int main()
    {
      char Chars[] = { 192, 168, 50, 77, ' ' };
    
      ofstream Output( "/tmp/ron", ios_base::binary );
    
      for( char *SPtr = &Chars[ 0 ], *EPtr = &Chars[ 0 ] + sizeof( Chars ); SPtr != EPtr; ++SPtr )
      {
        Output << *SPtr;
      }
      Output << endl;
    
    }
    

    ASL Script execute as:

    sm_adapter --server=INCHARGE-AM-PM --file=/tmp/ron test.asl
    

    Code:

    // -- Script changing binary ip address into readable text
    
    SM_System = object(getInstances("SM_System")[0]);
    
    START
    {
      ipaddress:word ..eol
    }
    do
    {
    
      if ( SM_System->isNull() )
      {
        print( "Could not retrieve object SM_System" );
        return;
      }
      // -- Print the address as it appears in the console ( interpreted by the system as UTFx )
      print( "ipaddress = ".ipaddress );
    
      // -- Conversion back to a readable IP Address
      print( "Int = ".SM_System->stringToByte( substring( ipaddress, 0, 1 ) ) );
      print( "Int = ".SM_System->stringToByte( substring( ipaddress, 1, 1 ) ) );
      print( "Int = ".SM_System->stringToByte( substring( ipaddress, 2, 1 ) ) );
      print( "Int = ".SM_System->stringToByte( substring( ipaddress, 3, 1 ) ) );
    }
    

    Output asl script

    sm_adapter --server=INCHARGE-AM-PM --file=/tmp/ron test.asl
    ipaddress = �2M
    Int = 192
    Int = 168
    Int = 50
    Int = 77