Results 1 to 6 of 6

Thread: Dec, Hex, Bin

  1. #1
    ChimpFace9000
    Guest

    Post Dec, Hex, Bin

    Hi, havent had much exp. in this department, but here goes...

    I have a string that will contain either all decimal chars, hexadecimal chars, or binary charcter and ill need to change that string from one (dec, hex or bin) to the other (dec, hex or binary). In the program, Ill know which type (dec, hex or bin) the string will contain. How do i go about doing this?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If it's a number, then can you use something like atoi/itoa to convert it to a long using the correct radix, and then back to a string with the new one?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    ChimpFace9000
    Guest
    Will that work if its a hex and binary number? Remember hex contains a - f.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No idea If you prefix it with 0x and pass it to atoi it should work.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Lively Member dubae524's Avatar
    Join Date
    Jun 2001
    Location
    Currently on this Super Star Destroyer being telekinetically strangled to death by Darth Vader
    Posts
    78
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    void main(void)
    {
    char oke[26] = "0xFF0";
    int acker = atoi(oke);

    printf("%i\n", acker);
    }

    I tried your suggestion, Parksie, but it didn't work. All it printed out was 0. So how do you take a string written in the hexadecimal, or octal, and use it in atoi?
    - Justin Patrick Butler

    Comme je trouve. "As I find."
    - Butler family quote

    Beneficia sumptos procul superant. "The benefits far exceed the costs."
    - Myself

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    int israddigit(char c, int rad)
    {
      int t;
      c = tolower(c);
      if(isdigit(c))
      {
        t = c - '0';
        if(t >= rad) return 0;
        else return 1;
      }
      if(islower(c))
      {
        t = c - 'a';
        if(t >= rad) return 0;
        else return 1;
      }
      return 0;
    }
    
    int raddigittoint(char c, int rad)
    {
      int t;
      c = tolower(c);
      if(isdigit(c))
      {
        t = c - '0';
        if(t >= rad) return 0;
        else return t;
      }
      if(islower(c))
      {
        t = c - 'a' + 10;
        if(t >= rad) return 0;
        else return t;
      }
      return 0;
    }
    
    int atoirad(const char* str, int rad)
    {
      const char* p;
      int sum = 0;
      for(p = str; *p && israddigit(*p, rad); p++)
      {
        sum *= rad;
        sum += raddigittoint(*p, rad);
      }
      return sum;
    }
    that needs a lot of optimizing, but should work for ANY radix (2, 3, 4, 5, 6...)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width