- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Yeah, seriously, that's the 'most stupid' way to do it. Anyone who comes up with a stupider method isn't actually a programmer.
Even people who have no idea that languages usually come with base-changing tools should be able to work 'your method' out unless they literally have no idea what a 'base' is.
Hell, there's even a more stupid method that would work better. If they can't do the math in one line, they could always have a counter like:
counter += 4 * bit3;
That's just four lines of code. Or even, for the seriously math challenged: if (bit3) counter += 4;
The idea that you would write if statements for a 4! matrix of possibilities if you had any other way to do it is insane. Especially if this was all to set a single value.
Admin
But... wait... A char doesn't have to be 1 byte right? So wouldn't that be like.... #define NUMBER_OF_BITS_IN_BYTE CHAR_BIT / sizeof(char)
Long live the {,u}int{8,16,32}_t and similar types
Admin
How do we know that the javavm isn't just doing this in the backend when we call the one line of code? :)
Admin
(cough) you what?
Admin
It does, sorry.
Admin
The japanese might disagree with you there.
Admin
this code can't be real.. and if it is.. we should hunt him down and remove him from the gene pool...
Admin
#define NUMBER_OF_BITS_IN_BYTE CHAR_BIT //sizeof(char) is 1 by definition.
..but then you would be assuming that byte equals char. What kind of fouled up logic is that?
Admin
I once wrote a function in C to convert from any base (up to 36) to any base (up to 36) which had .. what was it, 25 lines or so?
Admin
Admin
Admin
Admin
Admin
If you are laughing at this code, have you considered that maybe the developer was trying to write something cross platform that is independent of the proprietary java packages that provide hex functions? I can see how if you have never developed cross platform code it might seem strange.
Admin
Dang, I wrote a working demo in powerpoint that was cleaner than this.
Admin
Java is a PLATFORM.
Admin
Sucks to be japanese. They have to use wide-chars which are two bytes long. All of their strings take up twice as much RAM!
Admin
Admin
Since the number of bits per byte is always 8, the following should be enough:
#define EIGHT 8
An advantage of this approach is that it is reusable. You can eliminate magic numbers without having to pollute the code with numerous #defines for NUMBER_OF_BITS_PER_CHAR, NUMBER_OF_BYTES_IN_SIXTY_FOUR_BITS, NUMBER_OF_SIDES_IN_AN_OCTAGON, etc.
Admin
Actually, most but not ALL systems have 8 bits in a byte. But your approach is sound, because, for example, if a byte is 7 bits, it lets you do this, and make the change in only one place...
#define EIGHT 7
Admin
This has the additional benefit that if for any reason the digit 8 turns out to have the value 7, (presumably due to the a decay in the value 8 leading it to be rounded down during an int cast - these things happen. math isn't as robust as you'd think), you can fix all of the problems in your program in one simple quick fix.
Admin
My favorite part is that he has 3-4 lines of comments on EVERY LINE of code. "Number of characters per byte" is fairly hilarious. And do those bit arrays actually get used?
Admin
In ninth grade, using an Apple II, and the first language I ever learned (BASIC), I wrote equally bad code to convert Fahrenheit to Centigrade.
I can forgive myself for being a 14 year old neophyte. I can't forgive this code's author!
Admin
Admin
I discovered tonight that this is also very helpful for internationalization. We just landed a contract with a large corporation in Oaxaca, Mexico, and I was tasked with translating our entire framework to Spanish. Sure enough, it turns out Mexican computers use only 7.5 bits per byte (must be cheaper to manufacture) which, over the years, have decayed to 7 bits. I was able to change the above line to
#define OCHO 7
and it fixed every problem in our program. My manager let me go home early for the holiday for finishing the conversion so quickly.
Admin
A sentence in English, translated to a language that uses characters (Japanese, Chinese, etc) would take less space than their English counterpart, because theoretically, 1 character could = 1 word. If a particular word was really long, the character equivalent could possibly be only one character, therefore, taking less space. :D
Admin
And thus, a new form of compression was born.
Admin
The other half a bit crossed the border into the US.
Admin
it works and it is commented. complaints, complaints, complaints.
Admin
Of course you know all modern compilers will do such trivial optimization? The only reason to use shifts there is that it is actually easier to read shifts that multiplication when working width bits mask and etc.
Admin
Yeah, cause we might port our embedded software to a processor and OS from the 70's.
Admin
(With appy polly loggies to A. Burgess)
Admin
For the record, here is the C# oneliner:
This is, by the way, orders of magnitude faster than using a StringBuilder and String.Format. The only thing that's still faster is the DIY solution (allocate a char[] twice as big as the byte array, use "0123456789abcdef"[bytes[n]]).
Admin
The Japanese might argue that a character doesn't have to be one byte but they'd be wrong to say the same about a char.
Admin
Admin
Admin
Yes, but apart from the '$_', it seems like Perl masquerading as C.
TIMTOWTDI (See perlfaq4, fifth question)
Admin
Admin
Admin
I created a function once to do this for me, but it was much better than this piece of crap. Back then I was working with VB, which does not have functions to work with Hex (that I know of, at least).
Even if it had, my function was still useful because it worked for any base. It went something like
It was about 40 lines long and worked fine, even if you wanted something absurd as converting from base 17 to base 3
Admin
The Code looks very Enterprisey to me!
Well done work.
Admin
The C standard requires that
must be at least 8 bits. On a PDP-10 (with 36-bit registers), is 9 bits.Other requirements:
must be no shorter than , must be no shorter than , and must be no longer thatn .In other words, if you know exactly how many bits you need, include stdint.h and use the u?int(8|16|32|64)_t types.
Admin
Even better would have been a hash table lookup. Then you could extend it anyway you want without having to do any math in the code, and you wouldn't be tied down by how someone else implemented bytes, 1 bit to 842^16 bits.
Admin
Depends on the dialect of C. Some C compilers used for 8-bit microcontroller development may have both a "byte" data type and a "bit" data type. 8-bit processors may support either or both of these data types without having to pack and unpack larger data structures.
Admin
I would have used Integer.toHexStringOnMondaysWednesdaysAndAlternateFridays() That is the one Java method people forget about because it is verbose.
Admin
I smell a new OMGWTF coding contest coming on...
Admin
iToad, by saying "C", referred to the ISO standard 9899, which only defines char to be an arithmetic data type having at least 8 significant bits.
You, frustrati, on the other hand, by saying "C", didn't refer to the ISO standard, but to some implementation of it. Since the "number of bits in a char" is implementation-defined by the C standard, your implementation defines it, probably to 8, maybe to something else. Otherwise, you don't have a C compiler, but only a compiler for "a language based on and looking almost like C".
Admin
Many platforms that lack arbitrary barrel-shift instructions also lack multiply instructions. A "multiplication" will then be implemented as a loop of single shifts and additions. If the compiler is clever then the two implementations will be exactly the same. If not, the "multiply" routine might end up being a larger loop due to inclusion of addition instructions in the loop.
Agreed. Unfortunately 99% of the code I write is for microcontrollers, and a lot of the compilers leave something to be desired when it comes to smart optimizers that can shrink your code without breaking the intended functionality.
Admin
On the other hand, the
public static final char ZERO = '0';
suggests that he's ready for the day when they overhaul mathematics and redefine the symbol that represents zero.
Admin
Bitwise shifts are faster than multiplication. Unless you assume that your compiler will only be used on settings which optimize constant multiples like that.