Logic error: need advice

This is a discussion on Logic error: need advice within the C Programming forums, part of the General Programming Boards category; hurm.. what is the correct way to return that value? Normally I suggest passing the array to the function as ...

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    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.

    Name:  att1.png
Views: 175
Size:  28.3 KB
    Attached Images Attached Images  
    Last edited by jinjiro; 10-09-2012 at 01:23 PM.

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    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
    2,917
    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
    2,917
    Well sprintf() takes your character and converts it to a C-string, strcat() then appends one C-string to another.

    Jim

Page 2 of 2 FirstFirst 12
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, 07:42 PM
  2. Error in logic
    By Mentallic in forum C Programming
    Replies: 2
    Last Post: 03-28-2010, 09: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, 07:42 AM
  5. Logic Error...
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 01-28-2006, 06:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21