Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. CharToInt

CharToInt

Scheduled Pinned Locked Moved The Weird and The Wonderful
20 Posts 13 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Paulo Zemek

    My real problem here is that the autor of the code didn't test EVERY possible character. After all, using default to return an useless and invalid value is not a good idea. It will be much better to: case 'G': throw new Exception("G is an invalid value."); . . . case 'Z': throw new Exception("Z is an invalid value."); LOL

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #11

    Except I just realized that to fit with his "coding style" I should have suggested

    default:
    {
    throw new Exception(string.Format("\"{0}\" is an invalid value", pcChar));
    return pcChar;
    break;
    }

    I feel better now. Unclean, but better.

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    P 1 Reply Last reply
    0
    • P Paulo Zemek

      public int CharToInt(char pcChar) { switch(pcChar) { case '0' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '1' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '2' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '3' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '4' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '5' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '6' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '7' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '8' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case 'A' : return 10; break; case 'B' : return 11; break; case 'C' : return 12; break; case 'D' : return 13; break; case 'E' : return 14; break; case 'F' : return 15; break; default : return 0; } }

      L Offline
      L Offline
      Lutoslaw
      wrote on last edited by
      #12

      I'm doing A LOT of such stuff nowadays. In assembly. (AT&T syntax)

      # READ (char* bufer, int bufer_len)

      Reads a number from an ascii buffer and returns an actual number in eax

      PARAMETERS

      1. Address to the buffer

      2. The string's length

      Used registers:

      esi - contains all read characters

      ecx - index inside the buffer

      ebx - used in a conversion process char -> int

      edx - address to the buffer

      eax - the result

      .globl read
      .type read, @function
      read:
      .equ DIGIT_0,'0'
      .equ DIGIT_9,'9'
      .equ CASE_A,'A'
      .equ CASE_F,'F'
      .equ CASE_a,'a'
      .equ CASE_f,'f'
      .equ ONE_DIGIT_MASK, 0x0000000F

      mov 8(%esp), %esi # save number of read chars
      
      mov $0, %ecx
      mov $0, %eax # there was the zero at the beginnig of the Universe
      cmp %ecx, %esi # empty string case
      je read\_done
      

      read_loop:
      mov $0, %ebx

      mov 4(%esp), %edx
      #mov (%edx), %edx	
      mov (%edx, %ecx, 1), %bl # take the next char
      cmp $DIGIT\_9, %bl
      jle read\_0to9
      

      read_AtoF:
      cmp $CASE_a, %bl
      jge read_AtoF_lowercase
      sub $CASE_A, %bl # substitute'A'
      jmp read_AtoF_continue
      read_AtoF_lowercase:
      sub $CASE_a, %bl # substitute 'A'
      read_AtoF_continue:
      add $10, %bl # add 10 to get the correct value
      jmp read_char_done

      read_0to9:
      cmp $DIGIT_0, %bl
      jl read_done
      sub $DIGIT_0, %bl # substitute '0'
      jmp read_char_done

      read_char_done:
      # (bl is a lower part of ebx)
      add %ebx, %eax # ebx is a read-to-eat int

      inc %ecx # increment the index
      cmp %ecx, %esi # check if we're done
      je read\_done
      
      shl $4, %eax # shift the number by one hex digit capacity.
      
      jmp read\_loop
      

      read_done:
      ret

      :omg:

      Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

      1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Except I just realized that to fit with his "coding style" I should have suggested

        default:
        {
        throw new Exception(string.Format("\"{0}\" is an invalid value", pcChar));
        return pcChar;
        break;
        }

        I feel better now. Unclean, but better.

        P Offline
        P Offline
        Paulo Zemek
        wrote on last edited by
        #13

        default: { throw new Exception(string.Format("\"{0}\" is an invalid value", pcChar)); return pcChar; break; } I must agree. This is a much better option. But I will keep the idea of checking each value, and ALSO use this pattern. So: case 'G': throw new Exception(string.Format("\"{0}\" is an invalid value", pcChar)); return pcChar; break; case 'H': throw new Exception(string.Format("\"{0}\" is an invalid value", pcChar)); return pcChar; break; As this is the original pattern from 0 to 8 (9 is NOT a number, LOL).

        R 1 Reply Last reply
        0
        • P Paulo Zemek

          default: { throw new Exception(string.Format("\"{0}\" is an invalid value", pcChar)); return pcChar; break; } I must agree. This is a much better option. But I will keep the idea of checking each value, and ALSO use this pattern. So: case 'G': throw new Exception(string.Format("\"{0}\" is an invalid value", pcChar)); return pcChar; break; case 'H': throw new Exception(string.Format("\"{0}\" is an invalid value", pcChar)); return pcChar; break; As this is the original pattern from 0 to 8 (9 is NOT a number, LOL).

          R Offline
          R Offline
          riced
          wrote on last edited by
          #14

          Paulo Zemek wrote:

          As this is the original pattern from 0 to 8 (9 is NOT a number, LOL)

          I though 6 was not a number http://www.youtube.com/watch?v=29JewlGsYxs[^] :-D

          Regards David R

          J 1 Reply Last reply
          0
          • P PIEBALDconsult

            return ( "0123456789ABCDEF".IndexOf ( pcChar ) ) ; :-D Though it still doesn't support lowercase letters.

            J Offline
            J Offline
            Jeroen De Dauw
            wrote on last edited by
            #15

            What about ... ? return ( "0123456789ABCDEF".IndexOf ( pcChar.ToUpper ) ) ;

            My little forums: http://code.bn2vs.com 70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!

            1 Reply Last reply
            0
            • R riced

              Paulo Zemek wrote:

              As this is the original pattern from 0 to 8 (9 is NOT a number, LOL)

              I though 6 was not a number http://www.youtube.com/watch?v=29JewlGsYxs[^] :-D

              Regards David R

              J Offline
              J Offline
              Jeroen De Dauw
              wrote on last edited by
              #16

              Why not check if the char equal 10? And since your getting payed by line go on till you reach the max value of an int64? :D Cheers

              My little forums: http://code.bn2vs.com 70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!

              1 Reply Last reply
              0
              • R riced

                What's he/she got against the number 9? :)

                Regards David R

                Y Offline
                Y Offline
                Yusuf
                wrote on last edited by
                #17

                riced wrote:

                What's he/she got against the number 9?

                Oh, nothing. 9 will get flipped to 6 automagically

                Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]

                1 Reply Last reply
                0
                • S Steve Wellens

                  Please, Show some mercy and give the coder this:

                  String Test = "1Fa";
                  int Answer = int.Parse(Test, NumberStyles.HexNumber);

                  Steve Wellens

                  B Offline
                  B Offline
                  bulg
                  wrote on last edited by
                  #18

                  C#? how about some good ol'

                  ans = c_val > '9' ? (10 + c_val - 'A') : c_val-'0';

                  1 Reply Last reply
                  0
                  • P Paulo Zemek

                    public int CharToInt(char pcChar) { switch(pcChar) { case '0' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '1' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '2' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '3' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '4' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '5' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '6' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '7' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '8' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case 'A' : return 10; break; case 'B' : return 11; break; case 'C' : return 12; break; case 'D' : return 13; break; case 'E' : return 14; break; case 'F' : return 15; break; default : return 0; } }

                    B Offline
                    B Offline
                    Brady Kelly
                    wrote on last edited by
                    #19

                    What's with all the 'break' statements after 8? He didn't need them before? Or does he hope that Convert.ToInt32 does some voodoo that never returns to the switch block?

                    1 Reply Last reply
                    0
                    • P Paulo Zemek

                      public int CharToInt(char pcChar) { switch(pcChar) { case '0' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '1' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '2' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '3' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '4' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '5' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '6' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '7' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case '8' : return Convert.ToInt32(Convert.ToString(pcChar)); break; case 'A' : return 10; break; case 'B' : return 11; break; case 'C' : return 12; break; case 'D' : return 13; break; case 'E' : return 14; break; case 'F' : return 15; break; default : return 0; } }

                      R Offline
                      R Offline
                      Rajesh R Subramanian
                      wrote on last edited by
                      #20

                      Brilliant! Aren't we glad English has only 26 letters? I'd like to see the Chinese version of the code (thousands of letters).

                      It is a crappy thing, but it's life -^ Carlo Pallini

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups