This post is more than 5 years old
5 Posts
0
2641
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
0
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
0
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
0
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
0
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
1
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
0
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
Regards, Ron
smartsrgr
5 Posts
0
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:
ASL Script execute as:
Code:
Output asl script