Thread: Conversion string to arrays

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    65

    Conversion string to arrays

    My programming partner and I have been working on a cool project dealing with binary/hex and it is working great. Everything is just about done and the mathematical side is finished moving into the second phase of user input we have run into some issues. Our ultimate goal is to open files read there contents and then convert to binary. Well the File IO is easy enough but we have run into a sort of standstill, this occurs in the conversion. At this point in the project Will and I have is testing with stdin, assuming we get that to work we will append the code for the File IO. Will and I have developed notes for the conversion:
    Code:
    int main()
    {
    int k[SIZE];
    char ch[SIZE];
    
    fscanf(stdin,"%s",ch);
    for(i=0;i<SIZE;i++){
    if(isalpha(char[i])
    {
    k[i] = (int) (ch - 'A')+65
    }
    
    else if(isalnum(charin[i]))
    {
    k[i] = atoi(charin);
    }             /*This is just notes from code, if there is syntax errors it does not reflect the true code. I am unable to access the source at the moment*/
    Now I am sure some of you have already noticed the huge errors, I hope you will be able to solve my problem.
    Error 1: Am I using the right Input function
    Error 2: When a char string (1 byte element) holds an int greater then 255 it spaces it between many elements and / or it sets it to values 48-57 from ANSI
    Values chart
    Error 3: If the information is placed between elements or is converted to ANSI form then ATOI will ether not work correctly or will convert the number from the ANSI value

    A few questions:
    Should I use an int array? This will solve some issues with holding but other issues will be presented.
    Is my conversion code k = (int)(ch-'A')+65 going to function correctly.

    Thanks for your time!
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well, if u wanted to read the contents of the file as binary, why dont u open the file in binary mode, and start working on binary data.

    That is what u are trying to do in the above case isn;t it.

    Error 1: Am I using the right Input function
    Use fgets to read a string, well the above read method works, but u wont do boundary check. Its best to use fgets, so that u wont overflow the buffer when reading line.

    When a char string (1 byte element) holds an int greater then 255 it spaces it between many elements and / or it sets it to values 48-57 from ANSI
    Values chart
    Yes, thats right, When u reach 255 and plus 1 on top of that will roll back to 0. Thats how it works.

    If the information is placed between elements or is converted to ANSI form then ATOI will ether not work correctly or will convert the number from the ANSI value
    Not sure what u are trying to say.

    ssharish2005
    Last edited by ssharish2005; 05-18-2007 at 02:20 AM.

  3. #3
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    char cannot "hold" an int. And a byte can't have a value greater than 255.

    11111111 is 255. No more ones fit in this byte.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by movl0x1 View Post
    char cannot "hold" an int. And a byte can't have a value greater than 255.

    11111111 is 255. No more ones fit in this byte.
    11111111 is NOT 255, it loops around, so 255+1 = 0, 255+2 = 1, etc.

  5. #5
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    I know that it loops around, but a single byte can't hold more than 1111 1111
    just like an 8 oz. glass can't hold more than 8 ounces.

    If you do this in C

    Code:
     unsigned char = 255;
    that byte will contain 1111 1111. It can't hold 256 ( 1 0000 0000 ), that would take at least 16 bits.
    Last edited by movl0x1; 05-18-2007 at 02:33 AM.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Stop working at a bit level in your posts theres no need to. And it would take at least 9bits
    or 1 & 1/8th otects

  7. #7
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Yes, 9 bits, but in C there aren't 9 bit variables.

    And there is a need to go to the bit level for what I'm doing

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No there isn't the OP only needs to know that a 16bit variable does not fit into 8 bits.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    well I allready knew that the intergers would not fit but assumed that it would convert each digit to there ANSI value.
    Code:
    #include <stdio.h>
    
    int main()
    {
    int x;
    for(x=48;x<58;x++);
                                 {
                                  printf("&#37;d %c",x,x);
                                  } 
    }
    Well If I can convert my int input of lets say "1,232" to "1 2 3 2" and from there on each element convert back to 1-9 (base 10) using a simple conversion of char o ='2'; int i;
    i = (int) (o-'0');. Then compare elements in the array and use atoi on certain blocks of memory. Before I got back to my work bench does anyone have objections to this method.
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It seems to me that if you're going to all of the trouble to extract each digit, you might as well go the whole way and write your own atoi() variation.

    But really, there's probably a function already in existence that does what you want. Take a look at strtol() and sscanf(), for example.

    BTW, you have a stray semicolon after your for loop in the code in your last post.

    Yes, 9 bits, but in C there aren't 9 bit variables.
    There are, in a way.
    Code:
    struct { unsigned data : 9; } var;
    [offtopic]
    11111111 is NOT 255, it loops around, so 255+1 = 0, 255+2 = 1, etc.
    Is that portable? I'm pretty sure overflow of unsigned variables is guaranteed to wrap, but what about with signed variables? What about underflow?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Code:
         char str[] = "1232";
         int i, num, tmp;
         i = num = 0;
    
        do {
               tmp = str[i++] - 0x30;
               num = tmp + (num * 10);
        } while (str[i] != '\0');
    
        /* int num now holds 1232 */

    Is that similar to what your attempting to do?
    Or am I way off base?

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    do not use magic numbers in your code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM