Octet String in SNMP has a value 8? - snmp

Hi I have a packet capture from wireshark .
I have opened the file and saw the below output in variable bindings.
Object Name: .1.3.6.1.4.1.193.183.4.1.4.5.1.8(iso.3.6.1.4.1.193.183.4.1.4.5.1.8)
Value(Octect String ) : 353038
Can I find the integer value of the octect string ?
Octect string and Octal value mean the same ? If so , can a octal value contain 8 in 353038 .. ?
Please guide how do I know the integer value of the octect string : 353038

Octet string is a sequence of bytes. In your example, it consists of 3 bytes with hexadecimal values 35, 30, and 38. In a general case, you cannot talk in a meaningful way about an "integer value of the octet string". In this particular case, if you interpret the individual bytes as ASCII, then 35 hex represents character "5", 30 hex represents character "0", and 38 hex represents character "8", so the whole string is "508".
I suspect that the "integer value" you are looking for is 508 in this case, but keep in mind that the interpretation of the meaning of the string depends very much on the application.

Octet strings are defined as OCTET STRING in ASN.1 and SNMP. There is nothing called "Octect String". Meanwhile, octet strings have little relationship with the octal numeric system.
When you see 353038 is as an octet string, it simply means on the wire an SNMP packet have arrived, and it contains a string of ASCII characters "3", "5", "3", "0", "3", and "8". It is a string by nature, and does not necessarily need to be an integer (unless the definition of .1.3.6.1.4.1.193.183.4.1.4.5.1.8 in the corresponding MIB document mandates that fact).
Most of your questions above are invalid, as you misunderstood the concept of octet strings. But if the object definition does indicate the string to represent an integer, you can always convert the string received to an integer. Most programming languages support that kind of conversion.

Related

Why does typecasting a single byte to string not work in go?

I am trying to convert a single byte value to a string in golang. When I do a typecast of a byte to string like string(byte) and print the value I get "{" in response. I read other answers that the correct way to convert a byte to string would be strconv.Itoa(int(bytevalue)). Why does the former not work and why is the latter approach correct.
The expression string(bytevalue) is a conversion, not a typecast.
The specification says this about conversions from numeric types to a string:
Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer.
The expression string(byte(123)) evaluates to the string "{" because { is the the string containing the UTF-8 representation of the rune 123.
Use the strconv package to get the decimal representation of the byte. The expression strconv.Itoa(int(byte(123))) evaluates to the string "123".

Calculating checksum or XOR operations

I'm using hyperterminal and trying to send strings a to 6 digit scoreboard. I was sent a sample string from the manufacturer to test with and it worked, but to be able change the displayed message I was told to calculate a new Checksum value.
The sample string is: &AHELLO N-12345\71
Charactors A and N are addresses for the scoreboards(allowing two displays be used through one RS232 connection). HELLO and -12345 are the characters to be shown on the display. The "71" is where I am getting stuck.
How can you obtain 71 from "AHELLO N-12345"?
In the literature supplied with the scoreboard, the "71" from the sample string is described as a character by character logical XOR operation on characters "AHELLO N-12345". The manufacturer however called it a checksum. I'm not trained in this type of language and I did try to research but I can't put it together on my own.
The text below is copied from the supplied literature and describes the "71" (ckck) in question...
- ckck = 2 ASCII control characters: corresponds to the two hexadecimal digits obtained by
performing the character by character logical XOR operation on characters
"AxxxxxxByyyyyy". If there is an error in these characters, the string is ignored
Example: if the byte by byte logical XOR operation carried out on the ASCII codes of the
characters of the "AxxxxxxByyyyyy" string returns the hexadecimal value 0x2A,
the control characters ckck are "2" and "A".
You don't specify a language but here's the algorithm in C#. Basically xor the values of the string all together and you'll end up with a value of 113, 71 in hex. Hence 71 is on the end of the input string.
string input = "AHELLO N-12345";
UInt16 chk = 0;
foreach(char ch in input) {
chk ^= ch;
}
MessageBox.Show("value is " + chk);
Outputs "value is 113"

How does pack work in Ruby?

I am a tad confused about what I see here:
a = [ "a", "b", "c" ]
n = [ 65, 66, 67 ]
a.pack("A3A3A3") #=> "a b c "
a.pack("a3a3a3") #=> "a\000\000b\000\000c\000\000"
n.pack("ccc") #=> "ABC"
From the docs:
Packs the contents of arr into a binary sequence according to the directives in aTemplateString (see the table below) Directives “A,'' “a,'' and “Z'' may be followed by a count, which gives the width of the resulting field.
Here are the directives:
So we're using the A directive 3 times it seems? What does it mean to pack the string a into an arbitrary binary string (space padded, count is width?) Can you help me understand the output? Why are there so many 0s?
In the first case, you're printing "a" but padding its length to 3 with spaces, hence the two spaces to get the total length to 3.
In the second case, you're doing the same but padding with null bytes instead (ASCII value 0). Null bytes in Ruby are printed (and can be read) using the escape syntax \000 (this is one character), so \000\000 is actually just two null bytes.
The variable n is irrelevant, so you can ignore it.
In the pack statements, the bytes "a", "b" and "c" are concatenated ("packed") into a single string, with padding between them. The padding is such that the number of bytes (the width) taken up by the contents plus the padding equals the number provided.
So in the first pack statement, the "a" is padded with two spaces to make these three bytes: "a.." where I've put a . in place of the spaces to make it clear. That is concatenated with the "b" and the "c" similarly padded, to produce "a..b..c..".
In the second pack statement, null characters ('\000') are used instead of spaces. The \xxx notation (called an "escape sequence") means the byte with octal value xxx. It's used when there isn't a useful ASCII character (like 'a' or ' ') to show. A null character has no useful ASCII character, so the \xxx notation is used instead.

Get string with base-16 (hex) rendering of the bytes of an ASCII string

E.g.
input := "Office"
want := "4f6666696365" // Note: this is a string!!
I know that string literals are stored in UTF-8 already.
What is the easiest way to get convert this to string in UTF-8 representation?
Calling EncodeRune on each character seems too cumbersome.
What you're looking for is a string that contains the hex representation of your input string. That is not UTF-8. (Any string that's valid ASCII is also valid UTF-8.)
In any case, this is how to do what you want:
want := fmt.Sprintf("%x", []byte(input))

Changing Double Quotes to Single

I'm working on a project in Ruby. The library I'm using returns a string in double quotes, for example: "\x00\x40". Since the string is in double quotes, any hex that can be converted to an ASCII character is converted. Therefore, when I print, I actually see: "\x00#".
I figured out that, if I use single quotes, then the string will print in pure hex (without conversion), which is what I want. How do I change a double quoted string to single quoted?
I do not have any way to change the return type in the library since it is a C extension, and I can't figure out where the value is being returned from. Any ideas greatly appreciated.
"\x00\x40" and '\x00\x40' produce totally different strings.
"\x00\x40" creates a 2 byte string with hex values 0x00 and 0x40:
"\x00\x40".length
# => 2
"\x00\x40".chars.to_a
# => ["\u0000", "#"]
'\x00\x40' creates a string with 8 characters:
'\x00\x40'.length
# => 8
'\x00\x40'.chars.to_a
# => ["\\", "x", "0", "0", "\\", "x", "4", "0"]
This is done by Ruby's parser and you cannot change it once the string is created.
However, you can convert the string to get its hexadecimal representation.
String#unpack decodes the string as a hex string, i.e. it returns the hex value of each byte as a string:
hex = "\x00\x40".unpack("H*")[0]
# => "0040"
String#gsub adds/inserts \x every 2 bytes:
hex.gsub(/../) { |s| '\x' + s }
# => "\\x00\\x40"

Resources