6 kyu

Big to Little Endian

131 of 141maerch
Description
Loading description...
Algorithms
  • Please sign in or sign up to leave a comment.
  • akar-0 Avatar

    COBOL translation (author is inactive).

  • akar-0 Avatar

    This comment has been hidden.

  • ejini战神 Avatar
    • Ruby 3.0 should be enabled (Refer this & this for more detail)
  • phaelax Avatar

    I have never touched Ruby before. Still, only took about 30 seconds to write an solution. And then 30 minutes figuring out how to raise an exception without getting an error. In the end, I gave up and forfeited because everything I tried gave an error. Turns out, I wrote the exception correctly all along, I just checked the integer against 0xFFFFFF instead of 0xFFFFFFFF. FML.

  • Beast Avatar

    You should have more test cases to at least check for max integer allowed.

  • mlabrum Avatar

    This comment has been hidden.

  • moorsiek Avatar

    You could apply some markup to the description. I suggest this:

    Your operating system screwed some things up. It has received some unsigned 32 bit integer from the Internet, but nobody told your OS about the "network byte order" and his "host byte order".

    Your job is to write a function ntoh which takes a 32 bit unsigned integer in the Big Endian byte order and returns the correct 32 bit unsigned integer in the Little Endian order.

    To refresh your knowledge about byte order. One byte are 8 bits, so a 32 bit unsigned integer consists of 4 bytes which are ordered the wrong way.

    If an integer longer than 32 bit is given the function has to rise a nasty exception.

    Example:

    ntoh(42949695) # => 1063030530
    
    
    >`42949695` as a 32 bit integer is
    
    >`00000010 10001111 01011100 00111111` which translates to
    
    >`00111111 01011100 10001111 00000010` which is `1063030530`.
    
    Here is the markup for such look (just copy it & paste):
    
    >Your operating system screwed some things up. It has received some unsigned 32 bit integer from the Internet, but nobody told your OS about the "network byte order" and his "host byte order".
    
    >Your job is to write a function \`ntoh` which takes a 32 bit unsigned integer in the Big Endian byte order and returns the correct 32 bit unsigned integer in the Little Endian order.
    
    >To refresh your knowledge about byte order. One byte are 8 bits, so a 32 bit unsigned integer consists of 4 bytes which are ordered the wrong way.
    
    >If an integer longer than 32 bit is given the function has to rise a nasty exception.
    
    >Example:
    
    >\```ruby<br>
    ntoh(42949695) # => 1063030530<br>
    

    `42949695` as a 32 bit integer is

    `00000010 10001111 01011100 00111111` which translates to

    `00111111 01011100 10001111 00000010` which is `1063030530`.

  • moorsiek Avatar

    The kata is nice, but I had one difficulty. After reading description, I thought that argument is a signed integer, but it appeared to be unsigned when tests haved failed. It should be noted in briefing, shouldnt it?

  • Abbe Avatar

    Your code example calls the method nonh instead of ntoh.

    Personally, I would prefer calling the method something more readable, like switch_endian.

    • maerch Avatar

      Oh yeah! Thanks! I made the change!

      Regarding the method name I am not sure. On the one hand readable names are always a plus, but on the other hand "ntoh" and "hton" are quite established method names in operating systems for this matter. I thought this would bring some recall value later on for someone seeing this method names somewhere else.

      But should be a readable name more prefereable to the Codewars community, I can change that. This is my first kata and I am eager to learn!

    • Abbe Avatar

      If those are de facto standard names, I think you should keep them. I agree with your recognition idea.

      And congratulations to your first kata. You did a good job! :-)