Marcono1234 created HADOOP-17606:
------------------------------------

             Summary: VersionInfoMojo.byteArrayToString(...) creates malformed 
hex strings
                 Key: HADOOP-17606
                 URL: https://issues.apache.org/jira/browse/HADOOP-17606
             Project: Hadoop Common
          Issue Type: Bug
    Affects Versions: 3.2.2
            Reporter: Marcono1234


{{org.apache.hadoop.maven.plugin.versioninfo.VersionInfoMojo.byteArrayToString(byte[])}}
 creates malformed hex strings:
{code}
private String byteArrayToString(byte[] array) {
    StringBuilder sb = new StringBuilder();
    for (byte b : array) {
        sb.append(Integer.toHexString(0xff & b));
    }
    return sb.toString();
}
{code}

The issue here is that {{toHexString}} returns only a single hex char if the 
value is < 16. Therefore {{1, 0}} and {{16}} would both have the result 
{{"10"}}.

The correct implementation would be:
{code}
private String byteArrayToString(byte[] array) {
    StringBuilder sb = new StringBuilder();
    for (byte b : array) {
        int unsignedB = b & 0xff;
        if (unsignedB < 16) {
            sb.append('0');
        }
        sb.append(Integer.toHexString(unsignedB));
    }
    return sb.toString();
}
{code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: common-dev-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-dev-h...@hadoop.apache.org

Reply via email to