0

What is the quickest way to translate char* to number ? I need to convert 4 chars to int, or two chars to short int. I tried like

char* ar;
//fill ar with values  
int x= ar[1]+1[2]<<8+ar[3]<<16+ar[4]<<24; // ar[0] number of chars for number (short 2, int 4)

but result is always zero.( to explain I convert numbers to char* and than send over network, on another side I am trying to reverse process).

PaolaJ.
  • 10,872
  • 22
  • 73
  • 111
  • 1
    Quickest to code or quickest to run? By the way, `sizeof(int)` isn't guaranteed to be 4, nor `sizeof(short)` 2. – chris Dec 11 '12 at 23:26
  • You are almost certainly guilty of optimizing code without having used a profiler first. The evil of premature optimization. String conversions should only occur when you convert data obtained or delivered by I/O. File, network stream, user interface, etcetera. The I/O is always **much** more expensive than the conversion. You'll shave a handful of nanoseconds off something that costs handfuls of microseconds. Compromising code readability and stability for a 0.1% perf gain. Measure three times, cut once. – Hans Passant Dec 12 '12 at 00:29

3 Answers3

4

Use atoi function:

#include <iostream>
#include <cstdlib>

int main ()
{
  int i;
  char * num = "325";
  i = atoi (num);
  std::cout << i << std::endl;
  return 0;
}

Edit

As pointed in comments, you should not use atoi function, because you can't see if there was an error in conversion (atoi will return 0 if failed, but what about this case int i = atoi("0");). As you are using C++, there is option to use stringstream

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
        char * num = "3443";
        int result;
        stringstream ss;
        ss << num;
        ss >> result;

        if (!ss.fail()) {
            cout << result << endl;
        }



        return 0;
}

Unfortunately, I don't have C++11 compiler here, so I cannot try variant with std::stoi.

Edit 2

I've done some quick research, and here is topic that suggests use strtol function: How to parse a string to an int in C++?

Community
  • 1
  • 1
Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
  • 1
    No, don't use `atoi`, it doesn't report error on failure. If you have a C++11 compiler use `std::stoi`, else use a `stringstream` or `boost::lexical_cast`. – Praetorian Dec 11 '12 at 23:32
  • Based on the OPs code, I don't think he's trying to turn `"1"` into the integer `1`, I think he's just trying to encode 4 chars or 2 chars in a fashion that can be later turned back into those same 4 (or 2) chars. – Lily Ballard Dec 11 '12 at 23:45
3
ar[1]+1[2]<<8+ar[3]<<16+ar[4]<<24; 

With c++ operator precedence is:

(ar[1]+1[2]) << (8+ar[3]) << (16+ar[4]) << 24

No wonder it's always 0. Use parens. You can also use |, but I would suggest parens anyway.

zch
  • 14,931
  • 2
  • 41
  • 49
2

Guessing based on your sample code I think this is what you are looking for (you really have a void* not a char*???)

unsigned int getValue(char* c) {
    if (c[0] == 0x2) {
        return *(reinterpret_cast<unsigned short*>(c + 1));
    } else if (c[0] == 0x4) {
        return *(reinterpret_cast<unsigned int*>(c + 1));
    } else {
        assert(false);
    }
}

int main() {

    char c[5];
    char d[5];

    c[0] = 0x2;
    d[0] = 0x4;

    char* cStart = &c[1];
    *(reinterpret_cast<unsigned short*>(cStart)) = 1000;

    char* dStart = &d[1];
    *(reinterpret_cast<unsigned int*>(dStart)) = 1123124;

    std::cout << getValue(c) << std::endl;
    std::cout << getValue(d) << std::endl;

    return 0;
}
anthony sottile
  • 61,815
  • 15
  • 148
  • 207