Thread: Logic error: need advice

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    hurm.. what is the correct way to return that value?
    Normally I suggest passing the array to the function as a parameter. Otherwise using static will work, but you must be careful if you try to call the function more than once. Another way is to use dynamic memory allocation for this array (malloc/free).

    and there's also problem with the value inside hexnum[j] when I convert something more than '111111111' (9 digits)
    That's probably because your binNumChar has been defined with a size of 9.

    What exactly are you passing into this function? Please give a couple of examples.

    Jim

  2. #17
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    I'm passing a binary number.. at first I convert other number format to binary and then convert it to hexadecimal.. thus, the problem start when I convert 1024 in decimal to binary then to hexadecimal.. I used long long int for binary variable and change that to string so I can calculate the hexadecimal of it..

    I've changed the binNumChar to size 16.. It worked for hexnum[j] where the result is correct when I display it in the loop.. but when I use 'strcat(arrToStr,&hexNum[j--])' in the loop to combine hexadecimal digit to one variable, it become weird.. why is that? for example looping 'hexnum[j--]' display 7FFF but 'arrToStr' display 7F7FF7FFF7.. I attach the image below.

    Logic error: need advice-att1-png
    Attached Images Attached Images Logic error: need advice-att1-png 
    Last edited by jinjiro; 10-09-2012 at 12:23 PM.

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What compiler are you using?

    Also I don't see where you are putting the end of string character into your array before you use the string functions.

    Jim

  4. #19
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    Im using Dev-C++ 4.9.9.2. So where should I put the end of string character into my array? and what is the use of the end of string character?

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In C a string is a zero terminated character array. Without this terminating character '\0' you just have an array of characters. The functions in the string.h header file require C-strings and will not work with non-terminated character arrays.

    So where should I put the end of string character into my array?
    After the last valid character. You also need to insure that your array size allows for this extra character. So a C-string of "Hello" requires space for 6 characters, the five characters and the end of string character, 'H','e','l','l','o','\0'

    Jim

  6. #21
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    Code:
    char* convBinToHex(long long int binNumInt){
       
       char binNumChar[16],hexNum[10],arrToStrTemp[MAX];
       static char arrToStr[5]="";
       int i=0,j=0,temp;
       
       snprintf(binNumChar, sizeof(binNumChar), "%lld", binNumInt); //convert integer to char
       
       while(binNumChar[i]){
          binNumChar[i] = binNumChar[i] -48;
          ++i;
       }
    
       --i;
       
       while(i-2>=0){
           temp =  binNumChar[i-3] *8 + binNumChar[i-2] *4 +  binNumChar[i-1] *2 + binNumChar[i] ;
           if(temp > 9)
               hexNum[j++] = temp + 55;
           else
                hexNum[j++] = temp + 48;
           i=i-4;
       }
    
       if(i ==1)
          hexNum[j] = binNumChar[i-1] *2 +binNumChar[i] + 48 ;
       else if(i==0)
          hexNum[j] =  binNumChar[i] + 48 ;
       else
          --j;
          
       strcpy(arrToStr,""); 
       while(j>=0){
                   sprintf(arrToStrTemp, "%c",hexNum[j--]);
                   strcat(arrToStr,arrToStrTemp); //combine all string
    
        }
        
        return arrToStr;
    I try this one:
    sprintf(arrToStrTemp, "%c",hexNum[j--]);
    strcat(arrToStr,arrToStrTemp); //combine all string
    and it works..can you tell me what is happening?

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well sprintf() takes your character and converts it to a C-string, strcat() then appends one C-string to another.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Possible Logic Error
    By seanksg in forum C Programming
    Replies: 8
    Last Post: 03-14-2011, 06:42 PM
  2. Error in logic
    By Mentallic in forum C Programming
    Replies: 2
    Last Post: 03-28-2010, 08:02 AM
  3. logic Error
    By asmaa in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2009, 10:58 PM
  4. Error in logic
    By mesmer in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 06:42 AM
  5. Logic Error...
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 01-28-2006, 06:21 AM