# Re: [Oorexx-devel] An alternative algorithm for x2c()

```Well, you're missing all of the logic that allows for blanks at the
boundaries and you're also assuming that you have an even number of digits
to convert. If you remove all of the things that x2c() has to account for,
then yes, you can do a faster conversion.```
```
Rick

On Fri, Apr 19, 2019 at 6:51 AM Rony G. Flatscher <rony.flatsc...@wu.ac.at>
wrote:

> Thank you all for your feedback to my quite "dirty" ;) posting!
>
> As Mike pointed out 'a'-'z' were not honored, but also blanks in between
> pairs of hex digits, which Rexx allows for.
>
> Rewriting the algorithm a little bit it runs even faster (up to 4.5 times
> compared to x2c()):
>
> /* A Rexx hex string may include any number of whitespace after even hex
> characters, e.g.
>          "00 01   02"x
> */
> int64_t hex2char(CSTRING hexData, size_t len, char *data)
> {
>     size_t dIdx=0;
>
>     for (size_t i=0; i<len; i++)
>     {
>         char tmp='\0';
>
>         if      (hexData[i]>='0' && hexData[i]<='9') tmp=hexData[i]-'0';
>         else if (hexData[i]>='A' && hexData[i]<='F') tmp=hexData[i]-'A'+10;
>         else if (hexData[i]>='a' && hexData[i]<='f') tmp=hexData[i]-'a'+10;
>         else if (hexData[i]==' ' || hexData[i]=='\t') continue; // skip on
> whitespace
>         else
>         {
>             data[0]='\0';
>             return -i;      // error: return position as negative number
>         }
>         tmp<<=4;    // shift to high order nibble
>
>         i++;        // position on next hex digit
>         if      (hexData[i]>='0' && hexData[i]<='9') tmp|=hexData[i]-'0';
>         else if (hexData[i]>='A' && hexData[i]<='F') tmp|=hexData[i]-'A'+10;
>         else if (hexData[i]>='a' && hexData[i]<='f') tmp|=hexData[i]-'a'+10;
>         else        // also whitespace is not allowed in between of a hex pair
>         {
>             data[0]='\0';
>             return -i;      // error: return position as negative number
>         }
>         data[dIdx++]=tmp;
>     }
>     data[dIdx]='\0';
>     return dIdx;            // return number or characters in data
> }
>
> RexxRoutine1( RexxStringObject, cppX2C, CSTRING, hexData  )
> {
>     size_t len=strlen(hexData);
>
>     char *data=(char *) malloc (len+1);
>     int64_t res=hex2char(hexData,len,data);  // true, if translated; false,
> else
>
>     if (res==-1)
>     {
>         free(data);
>         return NULLOBJECT;      // indicate error; TODO: raise exception?
>     }
>
>     RexxStringObject rso=context->String(data, res);
>     free(data);
>
>     return rso;
> }
>
> Maybe missing something compared to the x2c()-BIF (x2c-method in the
> String class).
>
> ---rony
>
> _______________________________________________
> Oorexx-devel mailing list
> Oorexx-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/oorexx-devel
>
```
```_______________________________________________
Oorexx-devel mailing list
Oorexx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/oorexx-devel
```