From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by dpdk.org (Postfix) with ESMTP id 1E5F38E82 for ; Tue, 20 Oct 2015 16:33:06 +0200 (CEST) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP; 20 Oct 2015 07:31:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.17,707,1437462000"; d="scan'208";a="830915098" Received: from irsmsx154.ger.corp.intel.com ([163.33.192.96]) by orsmga002.jf.intel.com with ESMTP; 20 Oct 2015 07:31:13 -0700 Received: from irsmsx108.ger.corp.intel.com ([169.254.11.138]) by IRSMSX154.ger.corp.intel.com ([169.254.12.252]) with mapi id 14.03.0248.002; Tue, 20 Oct 2015 15:31:12 +0100 From: "Dumitrescu, Cristian" To: Thomas Monjalon , "Singh, Jasvinder" , "Zhang, Roy Fan" Thread-Topic: [dpdk-dev] [PATCH v2 6/8] example/ip_pipeline: add parse_hex_string for internal use Thread-Index: AQHRCnU3p4vmdqJs3Eyl1oYCP8N+r550cBnw Date: Tue, 20 Oct 2015 14:31:12 +0000 Message-ID: <3EB4FA525960D640B5BDFFD6A3D89126478F22F0@IRSMSX108.ger.corp.intel.com> References: <1444744652-573-1-git-send-email-jasvinder.singh@intel.com> <1444744652-573-7-git-send-email-jasvinder.singh@intel.com> <2304122.ekIElQmNCJ@xps13> In-Reply-To: <2304122.ekIElQmNCJ@xps13> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [163.33.239.182] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Cc: "dev@dpdk.org" Subject: Re: [dpdk-dev] [PATCH v2 6/8] example/ip_pipeline: add parse_hex_string for internal use X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Oct 2015 14:33:07 -0000 > -----Original Message----- > From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas Monjalon > Sent: Monday, October 19, 2015 4:50 PM > To: Singh, Jasvinder > Cc: dev@dpdk.org > Subject: Re: [dpdk-dev] [PATCH v2 6/8] example/ip_pipeline: add > parse_hex_string for internal use >=20 > 2015-10-13 14:57, Jasvinder Singh: > > From: Fan Zhang > > +static uint32_t > > +get_hex_val(char c) > > +{ > > + switch (c) { > > + case '0': > > + case '1': > > + case '2': > > + case '3': > > + case '4': > > + case '5': > > + case '6': > > + case '7': > > + case '8': > > + case '9': > > + return c - '0'; > > + case 'A': > > + case 'B': > > + case 'C': > > + case 'D': > > + case 'E': > > + case 'F': > > + return c - 'A' + 10; > > + case 'a': > > + case 'b': > > + case 'c': > > + case 'd': > > + case 'e': > > + case 'f': > > + return c - 'a' + 10; > > + default: > > + return 0; > > + } > > +} > > + > > +int > > +parse_hex_string(char *src, uint8_t *dst, uint32_t *size) > > +{ > > + char *c; > > + uint32_t len, i; > > + > > + /* Check input parameters */ > > + if ((src =3D=3D NULL) || > > + (dst =3D=3D NULL) || > > + (size =3D=3D NULL) || > > + (*size =3D=3D 0)) > > + return -1; > > + > > + len =3D strlen(src); > > + if (((len & 3) !=3D 0) || > > + (len > (*size) * 2)) > > + return -1; > > + *size =3D len / 2; > > + > > + for (c =3D src; *c !=3D 0; c++) { > > + if ((((*c) >=3D '0') && ((*c) <=3D '9')) || > > + (((*c) >=3D 'A') && ((*c) <=3D 'F')) || > > + (((*c) >=3D 'a') && ((*c) <=3D 'f'))) > > + continue; > > + > > + return -1; > > + } > > + > > + /* Convert chars to bytes */ > > + for (i =3D 0; i < *size; i++) > > + dst[i] =3D get_hex_val(src[2 * i]) * 16 + > > + get_hex_val(src[2 * i + 1]); > > + > > + return 0; > > +} >=20 > Why not use strtol()? Strtol() was considered, but the code would have been more complicated (mor= e corner case errors to handle) and less readable (more difficult to mainta= in), so our vote went for this simpler solution. This function needs to convert an array of characters of up to 128 hex digi= ts to the associated array of bytes, with output byte on position I represe= nting the value of the i-th pair of input characters. For example, "010203"= gets converted to array [1, 2, 3]. It only needs very simple math, which l= eads to the above code, which looks very readable. Strtol() was not really intended for this. Strtol() is primarily intended t= o convert up to 8 characters to the associated value. Using strtol() would = be possible to use (to convert each 2 characters to the byte) but we end up= with a lot of error cases to handle, which leads to more complex code in o= ur opinion. Thanks, Cristian