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. General Programming
  3. C / C++ / MFC
  4. use of _ltoa

use of _ltoa

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestion
7 Posts 4 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.
  • K Offline
    K Offline
    Kranti1251984
    wrote on last edited by
    #1

    Hi, I want to compare two character arrays containing binary values. For e.g. if m_Effects array contains 0001, string comparison does not return 0 even if binEffects contains binary equivalent of 1. Even if I do byte by byte comparison same problem occurs. What does _ltoa exactly perform? //--------------------------------------------------------------------- for(int i=0; i <16; i++) { _ltoa(i, binEffects, 2); if(strcmp((char *)binEffects, (char *)m_Effects) == 0) return i; } //--------------------------------------------------------------------- Thanks, Kranti

    N C M 3 Replies Last reply
    0
    • K Kranti1251984

      Hi, I want to compare two character arrays containing binary values. For e.g. if m_Effects array contains 0001, string comparison does not return 0 even if binEffects contains binary equivalent of 1. Even if I do byte by byte comparison same problem occurs. What does _ltoa exactly perform? //--------------------------------------------------------------------- for(int i=0; i <16; i++) { _ltoa(i, binEffects, 2); if(strcmp((char *)binEffects, (char *)m_Effects) == 0) return i; } //--------------------------------------------------------------------- Thanks, Kranti

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      u can directly use strcmp/memcmp to comapre two integer array nave

      K 1 Reply Last reply
      0
      • K Kranti1251984

        Hi, I want to compare two character arrays containing binary values. For e.g. if m_Effects array contains 0001, string comparison does not return 0 even if binEffects contains binary equivalent of 1. Even if I do byte by byte comparison same problem occurs. What does _ltoa exactly perform? //--------------------------------------------------------------------- for(int i=0; i <16; i++) { _ltoa(i, binEffects, 2); if(strcmp((char *)binEffects, (char *)m_Effects) == 0) return i; } //--------------------------------------------------------------------- Thanks, Kranti

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        I think you want to convert a binary string into its value ? Right ? Maybe you can write a simple function to do that: int BoolStringToVal(char* szString) { int Value = 0; for (int Index=strlen(szString)-1; Index>=0; Index--) { int BinValue = 1 << (Index - strlen(szString) + 1 ); if (szString[Index]) Value += BinValue ; } return Value; } So, what it does is look for each char in the string and if this char is 1 then we add its corresponding value to the total value. The code has not been tested so I'm not sure if this will work, but you got the principle.

        1 Reply Last reply
        0
        • N Naveen

          u can directly use strcmp/memcmp to comapre two integer array nave

          K Offline
          K Offline
          Kranti1251984
          wrote on last edited by
          #4

          when I check the values stored in both of the arrays, something like the following is observed ... binEffects[3] = '0' binEffects[2] = '0' binEffects[1] = '' binEffects[0] = '1' and the source array contains ... m_Effects[0] = '0' m_Effects[1] = '0' m_Effects[2] = '0' m_Effects[3] = '1' Even if i use memcmp(), it returns -1 if this example is concerned. Hence, i've a doubt regarding _ltoa function since it sets the values in binEffects. Kranti

          C 1 Reply Last reply
          0
          • K Kranti1251984

            when I check the values stored in both of the arrays, something like the following is observed ... binEffects[3] = '0' binEffects[2] = '0' binEffects[1] = '' binEffects[0] = '1' and the source array contains ... m_Effects[0] = '0' m_Effects[1] = '0' m_Effects[2] = '0' m_Effects[3] = '1' Even if i use memcmp(), it returns -1 if this example is concerned. Hence, i've a doubt regarding _ltoa function since it sets the values in binEffects. Kranti

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            Did you read my post ? That is a standard behaviour. If you want to convert an integer (let's say 3) in binary string, you won't have the zeros in front of the number, that sounds logical isn't ? So, when you convert 3 into a binary string, it's logical that you get '11' and not '0011' (how can the compiler knows that you want 2 zeros at the begining of the string). The same when you convert 1 into a string, you will get '1'.

            1 Reply Last reply
            0
            • K Kranti1251984

              Hi, I want to compare two character arrays containing binary values. For e.g. if m_Effects array contains 0001, string comparison does not return 0 even if binEffects contains binary equivalent of 1. Even if I do byte by byte comparison same problem occurs. What does _ltoa exactly perform? //--------------------------------------------------------------------- for(int i=0; i <16; i++) { _ltoa(i, binEffects, 2); if(strcmp((char *)binEffects, (char *)m_Effects) == 0) return i; } //--------------------------------------------------------------------- Thanks, Kranti

              M Offline
              M Offline
              mbue
              wrote on last edited by
              #6

              It would be better and faster to use the reverse function of _ltoa -> strtol. And now your code looks like that: char* e; int iEffects = strtol((const char *)m_Effects,&e,2); for(int i=0; i <16; i++) { if(i==iEffects) return i; }

              K 1 Reply Last reply
              0
              • M mbue

                It would be better and faster to use the reverse function of _ltoa -> strtol. And now your code looks like that: char* e; int iEffects = strtol((const char *)m_Effects,&e,2); for(int i=0; i <16; i++) { if(i==iEffects) return i; }

                K Offline
                K Offline
                Kranti1251984
                wrote on last edited by
                #7

                hey, it worked! :) Thanks a lot! Kranti

                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