Ask Your Question

ASCII to Integer in Dissector

asked 2019-11-05 20:19:54 +0000

padams gravatar image

I'm trying to write a dissector whose packets contain ASCII characters "00581804..." and I would like to grab them and convert them to integers so that my field types can be UINT32s. I'd like to grab 0058 (0x30303538) and convert it to 58 (0x3A). My code looks like the following:

static hf_register_info hf[] = {
        { &hf_foo, //- The index for this node.
            { "FOO Field Title", "foo",
            FT_UINT32, BASE_DEC,
            NULL, 0x0,
            NULL, HFILL }
static int dissect_foo(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree _U_, void *data _U_) {
    proto_tree_add_item(foo_tree, hf_foo, tvb, offset, 4, ENC_ASCII);

except it prints out 808465720 (the decimal equivalent of 0x30303538). Is there anyway for the proto_tree_add_item() function (or a similar tree add) to parse an ascii string but pass it to the formatting as an integer or other numerical value? Ideally I'd like to return or store this value for later use as well.


edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2019-11-06 10:17:03 +0000

Jaap gravatar image

This could be handled byproto_tree_add_item_ret_string() and friends, showing the string and handing it back to you for further handling, ie., conversion to numeric value.

edit flag offensive delete link more

answered 2019-11-06 10:08:52 +0000

Anders gravatar image

You should use FT_STRING for the hf. If you need the value internaly in your dissector you have to convert the string to a binary number by using something like ws_strtoi32(). There is a version of proto_tree_add... that fetches the string see proto.h

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-11-05 20:19:54 +0000

Seen: 139 times

Last updated: Nov 06 '19