Comment On Classic WTF: To the Hexth Degree

It's a particularly busy week for me: on top of a few looming deadlines, I'll be at Business of Software 2008 in Boston. So, I figured it'd be the perfect opportunity to revisit some classics. [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Classic WTF: To the Hexth Degree

2008-09-01 08:02 • by Me (unregistered)
I just cannot unsee it!

Re: Classic WTF: To the Hexth Degree

2008-09-01 08:02 • by Bombe
Duh. Not only is counting done from 1 to 4, it also starts on the wrong end of the nibble. Not bad. :)

Re: Classic WTF: To the Hexth Degree

2008-09-01 08:04 • by IgglePiggle (unregistered)
Oh sweet Mother of God!

Re: Classic WTF: To the Hexth Degree

2008-09-01 08:09 • by Deze (unregistered)
My eyes!

Re: Classic WTF: To the Hexth Degree

2008-09-01 08:15 • by Sunday Ironfoot
public static string ConvertToHexadecimal(string input)

{
byte[] bytes = Encoding.ASCII.GetBytes(input);
StringBuilder s = new StringBuilder();
for (int n = 0; n < bytes.Length; n++) {
s.Append(String.Format("{0,2:x}", bytes[n]).Replace(" ", "0"));
}
return s.ToString();
}


Well that would be my implementation. Probably a better /shorter way somewhere.

Re: Classic WTF: To the Hexth Degree

2008-09-01 08:16 • by DOA
This guy was obviously paid by the line. Laugh all you want, but he's now sipping martinis on his private 100m yacht.

Re: Classic WTF: To the Hexth Degree

2008-09-01 08:17 • by vonNeumann
Dear sir, we are interested in your implementation of this function. We find it to be sufficiently complex for use in our enterprise software, as we find that all functions under 1000 lines are inadequate and poor. Please expand on your comments somewhat, and we would be willing to pay the going market rate for this product ($200/LOC).

Yours sincerely,

PHB

Re: Classic WTF: To the Hexth Degree

2008-09-01 08:18 • by evir doh cysp (unregistered)
hello freind

i am try ot hex some String for collage. but you Jaava is not work. ther is nto complier erorr.

how to mak wrok?

emial of list plz?

Re: Classic WTF: To the Hexth Degree

2008-09-01 08:39 • by Dalden
215137 in reply to 215133
Sunday Ironfoot:


Well that would be my implementation. Probably a better /shorter way somewhere.


What about Integer.toHexString() ?

Re: Classic WTF: To the Hexth Degree

2008-09-01 08:44 • by Mog (unregistered)
215138 in reply to 215133
Well, I dunno much about java, but C...
*("0123456789ABCDEF" + ((bit1 << 3) | (bit2 << 2) | (bit3 << 1) | bit4))
should work...

But at least *their* version is easier to extend if hexadecimal suddenly gets a 17th character...

Re: Classic WTF: To the Hexth Degree

2008-09-01 08:51 • by Stavros (unregistered)
No fewer than!

Re: Classic WTF: To the Hexth Degree

2008-09-01 08:53 • by Gruff (unregistered)
So *THAT'S* why my Vista runs so slow...

Re: Classic WTF: To the Hexth Degree

2008-09-01 09:03 • by Bisqwit (unregistered)
215141 in reply to 215138
Well, I dunno much about java, but C...
*("0123456789ABCDEF" + ((bit1 << 3) | (bit2 << 2) | (bit3 << 1) | bit4))
should work...

I like "0123456789ABCDEF"[bit1*8+bit2*4+bit3*2+bit4] slightly better.

Re: Classic WTF: To the Hexth Degree

2008-09-01 09:07 • by blub (unregistered)
215142 in reply to 215141
Bisqwit:
Well, I dunno much about java, but C...
*("0123456789ABCDEF" + ((bit1 << 3) | (bit2 << 2) | (bit3 << 1) | bit4))
should work...

I like "0123456789ABCDEF"[bit1*8+bit2*4+bit3*2+bit4] slightly better.


I don't know much C but I love this solution :)

Re: Classic WTF: To the Hexth Degree

2008-09-01 09:12 • by Zecc
215144 in reply to 215141
var idx = 0001 * (bit1 ? 1 : 0)

+ 0010 * (bit2 ? 1 : 0)
+ 0100 * (bit3 ? 1 : 0)
+ 01000 * (bit4 ? 1 : 0);

switch(idx){
case 0:
return "0";
case 1:
return "1";
case 2:
return "2";
//...
case 16:
return "F";
}

Re: Classic WTF: To the Hexth Degree

2008-09-01 09:16 • by SlyEcho (unregistered)
215145 in reply to 215133
Sunday Ironfoot:
public static string ConvertToHexadecimal(string input)

{
byte[] bytes = Encoding.ASCII.GetBytes(input);
StringBuilder s = new StringBuilder();
for (int n = 0; n < bytes.Length; n++) {
s.Append(String.Format("{0,2:x}", bytes[n]).Replace(" ", "0"));
}
return s.ToString();
}


Well that would be my implementation. Probably a better /shorter way somewhere.



public static string ConvertToHexadecimal(string input)
{
return ConvertToHexadecimal(Encoding.Default.GetBytes(input));
}
public static string ConvertToHexadecimal(byte[] input)
{
return ConvertToHexadecimal(input, 0, input.Length);
}
public static string ConvertToHexadecimal(byte[] input, int index, int length)
{
const string hex = "0123456789abcdef";
char[] buffer = new char[length * 2];
for (int n = index; n < length; n++) {
buffer[2*n] = hex[input[n] >> 4];
buffer[2*n+1] = hex[input[n] & 0x0f];
}
return new string(buffer);
}


A slightly better version would be one that works with Streams and TextWriters.

Re: Classic WTF: To the Hexth Degree

2008-09-01 09:18 • by NiceWTF (unregistered)
215147 in reply to 215133
Sunday Ironfoot:
[..]
Well that would be my implementation. Probably a better /shorter way somewhere.


In Java it can indeed be done in 1 line:


public static String toHex(byte[] input) {
return new BigInteger(input).toString(16);
}

Re: Classic WTF: To the Hexth Degree

2008-09-01 09:19 • by csrster (unregistered)
We had a related wtf here a couple of weeks ago.
On my current project, we use BigInteger to do this conversion:

new BigInteger(1, data).toString(16);

which should be fine. Except that our byte-arrays actually
represent sha1 hashcodes. We spent a long time scratching our hands over strange checksum mismatches in the code, until it
finally came to our attention that the problem only arose when
the checksum had leading 0's .

Re: Classic WTF: To the Hexth Degree

2008-09-01 09:33 • by ambrosen
Wow, I've written it more concisely in XSL (version 1)

Re: Classic WTF: To the Hexth Degree

2008-09-01 09:45 • by kennytm
215151 in reply to 215144
Zecc:
var idx = 0001 * (bit1 ? 1 : 0)

+ 0010 * (bit2 ? 1 : 0)
+ 0100 * (bit3 ? 1 : 0)
+ 01000 * (bit4 ? 1 : 0);

That would be octal.

Re: Classic WTF: To the Hexth Degree

2008-09-01 09:59 • by Anonymous (unregistered)
Python:
'data as bytestring'.encode('hex')

Re: Classic WTF: To the Hexth Degree

2008-09-01 10:00 • by myself (unregistered)
In C:

what about a good old sprintf?

Re: Classic WTF: To the Hexth Degree

2008-09-01 10:15 • by Marcelk (unregistered)
215158 in reply to 215141


Well, I dunno much about java, but C...
*("0123456789ABCDEF" + ((bit1 << 3) | (bit2 << 2) | (bit3 << 1) | bit4))
should work...

I like "0123456789ABCDEF"[bit1*8+bit2*4+bit3*2+bit4] slightly better.


In C, such should always be written as (bit1*8+bit2*4+bit3*2+bit4)["0123456789ABCDEF"]

Re: Classic WTF: To the Hexth Degree

2008-09-01 10:19 • by Me (unregistered)
215159 in reply to 215158
Marcelk:


Well, I dunno much about java, but C...
*("0123456789ABCDEF" + ((bit1 << 3) | (bit2 << 2) | (bit3 << 1) | bit4))
should work...

I like "0123456789ABCDEF"[bit1*8+bit2*4+bit3*2+bit4] slightly better.


In C, such should always be written as (bit1*8+bit2*4+bit3*2+bit4)["0123456789ABCDEF"]


That's just fugly.

Re: Classic WTF: To the Hexth Degree

2008-09-01 10:28 • by ambrosen
215161 in reply to 215139
Stavros:
No fewer than!
Wanna bet‽

Linguists say no! Or actually, they say "prescriptivist poppycock", but it's much the same thing.

Re: Classic WTF: To the Hexth Degree

2008-09-01 10:32 • by D0R
215162 in reply to 215138
But at least *their* version is easier to extend if hexadecimal suddenly gets a 17th character...

Exactly. I find especially enlightened this part of the code:
public static final int NUMBER_OF_HEXADECIMAL_CHARACTERS_PER_BYTE = 2;

to ensure compatibility, should the Universe reboot and the laws of mathematics be modified, in case the number of hex numbers per byte changes.

Re: Classic WTF: To the Hexth Degree

2008-09-01 10:47 • by Not Dorothy (unregistered)
215165 in reply to 215162
D0R:

Exactly. I find especially enlightened this part of the code:
public static final int NUMBER_OF_HEXADECIMAL_CHARACTERS_PER_BYTE = 2;

to ensure compatibility, should the Universe reboot and the laws of mathematics be modified, in case the number of hex numbers per byte changes.


Pah, who says that the numbers of bits in a byte will be an integer value after the reboot. Put everything in strings and decode it as required.

Re: Classic WTF: To the Hexth Degree

2008-09-01 10:56 • by brazzy
215166 in reply to 215162
D0R:
But at least *their* version is easier to extend if hexadecimal suddenly gets a 17th character...

Exactly. I find especially enlightened this part of the code:
public static final int NUMBER_OF_HEXADECIMAL_CHARACTERS_PER_BYTE = 2;

to ensure compatibility, should the Universe reboot and the laws of mathematics be modified, in case the number of hex numbers per byte changes.

Actually, in C a byte does not necessarily have 8 bits. There is nothing in the least unusual about needing more than 2 hex digits to represent a byte.

Re: Classic WTF: To the Hexth Degree

2008-09-01 11:01 • by srednitsa (unregistered)
215168 in reply to 215166
In Java, a byte does have 8 bits.

Re: Classic WTF: To the Hexth Degree

2008-09-01 11:05 • by brazzy
215169 in reply to 215168
srednitsa:
In Java, a byte does have 8 bits.

Yes. It would thus require a change of the Java standard - not quite as epochal as a "Universe reboot", wouldn't you agree?

Re: Classic WTF: To the Hexth Degree

2008-09-01 11:41 • by JimBob (unregistered)
215175 in reply to 215141
Bisqwit:
Well, I dunno much about java, but C...
*("0123456789ABCDEF" + ((bit1 << 3) | (bit2 << 2) | (bit3 << 1) | bit4))
should work...

I like "0123456789ABCDEF"[bit1*8+bit2*4+bit3*2+bit4] slightly better.

Like it all you want, but all it'll get you is a core dump ;)

Re: Classic WTF: To the Hexth Degree

2008-09-01 11:43 • by Me (unregistered)
That's so obviously wrong! The arrays are never used!

A proper implementation should be:
public static final object[] F_BITS = new object[]{true, true, true, true, F, F_LOWER};

and then they can do the check by simply doing:
if (bit1 == F_BITS[FIRST_BIT_OFFSET] && bit2 == F_BITS[SECOND_BIT_OFFSET] && bit3 == F_BITS[THIRD_BIT_OFFSET] && bit4 == F_BITS[FOURTH_BIT_OFFSET])
return F_BITS[LOWERCASE_OFFSET];

(They need to add constants for LOWERCASE_OFFSET and UPPERCASE_OFFSET and use the same number constant for both from 0 to 9).

for each letter in the array. There, much better.

Re: Classic WTF: To the Hexth Degree

2008-09-01 11:55 • by Rob (unregistered)
215178 in reply to 215137
Dalden:
Sunday Ironfoot:


Well that would be my implementation. Probably a better /shorter way somewhere.


What about Integer.toHexString() ?


or
hex = sprintf(i, '%x'); // not sure on the java syntax

Re: Classic WTF: To the Hexth Degree

2008-09-01 11:58 • by emddudley
215179 in reply to 215141
Bisqwit:
Well, I dunno much about java, but C...
*("0123456789ABCDEF" + ((bit1 << 3) | (bit2 << 2) | (bit3 << 1) | bit4))
should work...

I like "0123456789ABCDEF"[bit1*8+bit2*4+bit3*2+bit4] slightly better.


Shift and OR operations are faster than multiplication. I'd be curious to compare the assembly output from an optimizing compiler for each and see which is more efficient.

Re: Classic WTF: To the Hexth Degree

2008-09-01 11:59 • by Leon (unregistered)
215180 in reply to 215161
ambrosen:
Stavros:
No fewer than!
Wanna bet‽

Linguists say no! Or actually, they say "prescriptivist poppycock", but it's much the same thing.



Yakka foob mog, grub pubbawup zink watoom gazork. Chumble spuzz.

And if you don't like those words, you're being prescriptivist. I think they're perfectly valid.

Re: Classic WTF: To the Hexth Degree

2008-09-01 12:09 • by Roman (unregistered)
Perl code examples plz? ;-)

Re: Classic WTF: To the Hexth Degree

2008-09-01 12:23 • by Jens Kleemann (unregistered)
215187 in reply to 215179
a (good) optimizing compiler will most definitely transform the "bit1*8" to "bit1<<3" as long as bit1 .. bitn are integer/byte/... types

btw.
see also this (i love this example - code like that has driven me nuts on "reengineering" ) http://www.nynaeve.net/?p=115

Re: Classic WTF: To the Hexth Degree

2008-09-01 12:25 • by poochner
215188 in reply to 215180
Leon:
ambrosen:
Stavros:
No fewer than!
Wanna bet‽

Linguists say no! Or actually, they say "prescriptivist poppycock", but it's much the same thing.



Yakka foob mog, grub pubbawup zink watoom gazork. Chumble spuzz.

And if you don't like those words, you're being prescriptivist. I think they're perfectly valid.


Indeed, quite cromulent.

Re: Classic WTF: To the Hexth Degree

2008-09-01 12:35 • by frustrati (unregistered)
215189 in reply to 215166
brazzy:

Actually, in C a byte does not necessarily have 8 bits. There is nothing in the least unusual about needing more than 2 hex digits to represent a byte.
Actually, in C there is no "byte" data type

Re: Classic WTF: To the Hexth Degree

2008-09-01 12:46 • by zzo38
They use not only too many lines to do a simple things that could be done in one line, but they have way too many comments, also!

Re: Classic WTF: To the Hexth Degree

2008-09-01 12:51 • by mentaldingo
215193 in reply to 215182
Roman:
Perl code examples plz? ;-)

sprintf("%x",$_);

?

Re: Classic WTF: To the Hexth Degree

2008-09-01 12:54 • by YAFIYGI (unregistered)
215194 in reply to 215189
frustrati:
brazzy:

Actually, in C a byte does not necessarily have 8 bits. There is nothing in the least unusual about needing more than 2 hex digits to represent a byte.
Actually, in C there is no "byte" data type

Actually, brazzy did not write anything about data types. TRWTF: Trying to be a smart butt + lacking knowledge. Hint: Read the standard before posting nonsense about C.

Re: Classic WTF: To the Hexth Degree

2008-09-01 13:28 • by lolwtf
215196 in reply to 215148
csrster:
We had a related wtf here a couple of weeks ago.
On my current project, we use BigInteger to do this conversion:

new BigInteger(1, data).toString(16);

which should be fine. Except that our byte-arrays actually
represent sha1 hashcodes. We spent a long time scratching our hands over strange checksum mismatches in the code, until it
finally came to our attention that the problem only arose when
the checksum had leading 0's .
Working for Nintendo?

Re: Classic WTF: To the Hexth Degree

2008-09-01 13:34 • by Manos
One day, after a code review by one of our 'senior' developers this little beauty showed up...

#define NUMBER_OF_BITS_IN_BYTE 8

The justification was... that the byte might change.... and no 'magic' numbers should appear in the code...

Re: Classic WTF: To the Hexth Degree

2008-09-01 14:05 • by Ayin (unregistered)
215199 in reply to 215179
Not necessarily, depends on the platform. Some platforms only have 1-bit shift operations, and on those platforms, bit1<<3 would need a loop to be implemented, which may be slower than a multiplication.

Anyway, good optimizers always decide, on a case-per-case basis, which one is faster, so programmers should just stick to what is more readable (do you actually want to multiply values, or just shift bits?).

Never try to do the optimizer's work manually!

Re: Classic WTF: To the Hexth Degree

2008-09-01 14:16 • by Nobody (unregistered)
215200 in reply to 215197
Manos:
One day, after a code review by one of our 'senior' developers this little beauty showed up...

#define NUMBER_OF_BITS_IN_BYTE 8

The justification was... that the byte might change.... and no 'magic' numbers should appear in the code...


Actually I can understand this. Not because the byte might change but rather to document the code. Why make the next guy who has to maintain your code have to figure out what the number 8 represents when you can make it glaringly obvious this way?

Re: Classic WTF: To the Hexth Degree

2008-09-01 15:01 • by Geek (unregistered)
I'm sure the code making use of this depends on a certain bug in this "algorithm", so you won't be able to exchange it.

Re: Classic WTF: To the Hexth Degree

2008-09-01 15:32 • by brazzy
215210 in reply to 215197
Manos:
One day, after a code review by one of our 'senior' developers this little beauty showed up...

#define NUMBER_OF_BITS_IN_BYTE 8

The justification was... that the byte might change....

You might want to read up on what a "byte" is and is not:
http://en.wikipedia.org/wiki/Byte
The only WTF in that is that he didn't use CHAR_BIT in limits.h, which has exactly that content for exactly that reason.

Re: Classic WTF: To the Hexth Degree

2008-09-01 15:47 • by NullAndVoid
From: Management
To: Code Monkey
Date: Friday, August 29, 2008 04:15 pm
Subject: Labor Day Holiday

Mr. Mankey,

I have received your request for time off on Monday. This will be no problem as long as you finish the hexadecimal conversion method assigned to you earlier this month. It is looking good so far, but the client has requested a small piece of additional functionality. Please upgrade your method to allow balanced ternary input and provide the option for base 42 output. This should be an easy fix for an experienced programmer. Remember, the new method must be released to production by the end of the day.

Regards,

P. H. Boss

Re: Classic WTF: To the Hexth Degree

2008-09-01 16:21 • by rbonvall
215218 in reply to 215158
Marcelk:

I like "0123456789ABCDEF"[bit1*8+bit2*4+bit3*2+bit4] slightly better.

In C, such should always be written as
(bit1*8+bit2*4+bit3*2+bit4)["0123456789ABCDEF"]


This one is cleaner:
*(bit1*8 + bit2*4 + bit3*2 + bit4 + "0123456789ABCDEF")
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment