• Post Reply Bookmark Topic Watch Topic
  • New Topic

SNMP - Converting OctetString to Float  RSS feed

 
Jens Mustermann
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

currently I am playing around with SNMP (snmp4j and SimpleSnmpClient) to get some information from a network device.
This works as long as the result is an "normal" value like an integer.

If the value is an OctetString like
44:00:4f:00:30:00:4a:00:47:00:20:00:4d:00:49:00:58:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
labelled as Unicode I can decode it as well but before I have to replace the ":"



Now I want to get the value for a Voltage, the MiB says
- OCTECT STRING (SIZE(4))
- It should be changed to float format

OK I do the SNMP query and get as result:
e6:d9:4c:41
witch should be a voltage around 12.8 Volt

Now I try to convert this to Float

and get
-5.1308008E23
e6d94c41

So what is wrong with my code ?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If think you need to reverse the order of the 4 octet parts so rather than e6:d9:4c:41  you process it as 41:4c:d9:e6
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch
 
Jens Mustermann
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:If think you need to reverse the order of the 4 octet parts so rather than e6:d9:4c:41  you process it as 41:4c:d9:e6


Mmm interesting, I tried this before but now it works, many thanks
 
Jens Mustermann
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK now I changed it to work dynamically



and I get

Voltage: e6:d9:4c:41
Test: e6d94c41
Test: 414cd9e6
Voltage: 11.9357815

but I expect 12.803198 regarding this method




So what do I have to change ?
 
Jens Mustermann
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry it seems that editing a post is disabled, the first line of my code has to read:
sysDescr = "e6:d9:4c:41";
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Editing is disabled until you have a gained a little good reputation on the forum.

if I run:

I get

 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you reverse the operation, 11.9357815 gives 413ef8f6 as int bits.

The problem is in your hex2float() method. Here's a slight modification:

This is the result:

Test: 413ef8f6
412ef7f6


Rather than roll your own, you can use Integer.parseInt("413ef8f6", 16)

See the JavaDocs for Integer.parseInt()
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, the bug was that the input had lowercase letters and your lookup string was all uppercase letters. I'd still recommend you use Integer.parseInt(stringValue, radix) instead of rolling your own method to do the conversion.
 
Jens Mustermann
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK now I created a new method and all works fine, you are the best

- it removed the ":"
- it reads the HEX from right to left
- it convert the value to Float
- it returns a Sting value






Best Regards from Germany
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The technical term for that order of bytes is little endian (vs big endian) so you might want to make your method name reflect that instead. Also, returning a String when dealing with a value like that is suboptimal. Sure, a string might be convenient for your current purposes but the value is more naturally a floating point number. Let the client code convert the floating point value to String if it needs to but keep this conversion method as close to the value's natural type as possible.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method name of littleEndianToFloat() would be a good alternative, IMO.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!