Hi all,
I am trying to conver string to decimal.
the sting is 8 byte long and contains data as hex nembers e.g
0000000000000D72

i want to conver this data into decimal as 3442

i'm not getting how to do this
help me...

Try strtol()

long int strtol(const char *nptr, char **endptr, int base);

DESCRIPTION
The strtol() function converts the initial part of the string in nptr
to a long integer value according to the given base, which must be
between 2 and 36 inclusive, or be the special value 0.

thanks for rply
ya i have tried strtol()
but not getting desire result

00 00 00 00 00 00 0D 72 (LSB) -> 3442

char buffer[8]={0x72, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
long int li2 = strtol (buffer,NULL,16);
cout << li2 << endl;

my requirement is

the hex value 00 00 00 00 00 BC 61 4E (LSB)

which is stored in string as

char data[8] = {0x4E, 0x61, 0xBC, 0x00,
0x00, 0x00, 0x00, 0x00};

now i want to convert it into int
(equivalent of above hex string is 12345678)

plz give some idea...

Edited 6 Years Ago by archana.c07: n/a

the hex value 00 00 00 00 00 BC 61 4E (LSB)

which is stored in string as

char data[8] = {0x4E, 0x61, 0xBC, 0x00,
0x00, 0x00, 0x00, 0x00};

It looks like the char bits need to be swapped, front to back. Is that right? An endian switcheroo.

Then strtol should work.

i have tried that also however i'm getting same result (0).
i tried below code but i'm getting undefined behavior

char a = 0x00;
char b = 0x00;
char c = 0x00;
char d = 0x00;
char e = 0x00;
char f = 0xbc;
char g = 0x61;
char h = 0x4e;

long long int combination = (a << 56) |(b << 48) | (c << 40) | (d << 32) |
(e << 24) | (f << 16) | (g << 8) | (h);

cout << combination << endl << endl;

u have any suggestion

thank


char buffer[8]={0x72, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

If your using C strings them the above shouldn't work...

0x00 is not equal to '0'

0x30 is equal to '0

Check out this link for ascii table conversions

http://www.google.ca/imgres?imgurl=http://www.cs.utk.edu/~pham/ascii_table.jpg&imgrefurl=http://www.cs.utk.edu/~pham/ascii.html&h=475&w=715&sz=142&tbnid=fHeK-W8VD8qXUM:&tbnh=93&tbnw=140&prev=/images%3Fq%3Dascii%2Btable&hl=en&usg=__SejXXWEBQhMmL66GziQUEWyCjIM=&sa=X&ei=jhE7TI6fFo2inQfEpIXgAw&ved=0CCsQ9QEwAw

Edited 6 Years Ago by gerard4143: n/a

Hi all,
I am trying to conver string to decimal.
the sting is 8 byte long and contains data as hex nembers e.g
0000000000000D72

i want to conver this data into decimal as 3442

i'm not getting how to do this
help me...

How do you do it with pen and paper? Explain the process here and we can help you convert the process into code.

#include <stdio.h>
#include <math.h>
#include <string.h>

void hex_conversion();

int main()
{

   hex_conversion();

   return 0;

}


void hex_conversion()
{

  char s[0xFF];

  int length,i;

  long current_val, sum=0;

  printf("Enter the String:");

  scanf("%s",&s);

  length = strlen(s);

  printf("\nThe length is %d Bytes", length/2);

  for (i = length-1; i>=0; i--)
	   {

	       current_val = (int)s[i];//Obtaining the value

	       if (current_val < 0x3A)
               {

                   current_val = 0x0F & current_val;
                   sum = sum + current_val * pow(16, 15-i);
                   
               }

               else if (current_val >= 0x41 && current_val <= 0x46)
               {
                   current_val = 0x0F & current_val;
                   current_val = current_val + 9;
                   sum = sum + (current_val) * (int)pow(16, 15-i);
               }
               

           }

           printf("\nThe decimal value is : %d", sum);

   
}
This article has been dead for over six months. Start a new discussion instead.