HelpPlsHow to convert large chars to integer and hex

Pls. help me I need to convert large char to numeric and hex, below are the requirements:

input:
char var_char = '9999999999999999' ---16 max length--

output:
9999999999999999 ---- integer type
2386F26FC0FFFF ----- hex value

using sprintf cuts the value of hex, would appreciate your help... thanks i really need this to catch the deadline...
If you are sure that the number of digits is not more than 16, then you can use long long int data type if your compiler supports it. It's a 64 bit data type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include<stdio.h>
int main()
{
    char char_num[17];
    long long int int_num;    
    
    printf("Enter a number (Max 16 digits) : ");
    scanf("%16s",char_num);
    
    sscanf(char_num,"%lld",&int_num);
    
    printf("%lld %llX",int_num,int_num);
    return 0;
}



Please see the following pages for more details:
http://home.att.net/~jackklein/c/inttypes.html#long_long
http://docs.freebsd.org/info/gcc/gcc.info.Long_Long.html
http://cboard.cprogramming.com/showthread.php?p=666259
Last edited on
Or if you want to use C++ methods instead of C:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <sstream>

int main()
{
  std::string num_str;

  // Read until the correct length is entered
  do {
    std::cout << "Enter a number (Max 16 digits) : ";
    std::cin >> num_str;
  } while (num_str.empty() || num_str.length() > 16);

  // Convert to integer
  std::stringstream ss(num_str);
  long long int num;
  ss >> num;

  // Write result
  std::cout << "Number: " << num << std::endl;
  std::cout << "Hex: " << std::uppercase << std::hex << num << std::endl;
}
Last edited on
tried it on visual c++ 6.0 error below:
Compiling...
char_hex.cpp
d:\temp1\char_hex.cpp(5) : error C2632: 'long' followed by 'long' is illegal
Error executing cl.exe.

char_hex.exe - 1 error(s), 0 warning(s)
If you can, you should replace VC6 with Visual C++ 2008 Express, which is free. VC6 is known to have poor standard compliance.

Anyway, I think you'll need to use something like "__int64" instead of long long int. (that is: two underlines followed by int64)
#include<stdio.h>
int main()
{
char char_num[17];
__int64 int_num;
char new_val[]={NULL};

printf("Enter a number (Max 16 digits) : ");
scanf("%16s",char_num);

sscanf(char_num,"%lld",&int_num);

sprintf(new_val,"%llX",int_num);
printf("\n%s new_value\n",new_val);
printf("\n%lld %llX\n",int_num,int_num);
return 0;
}


output:
Enter a number (Max 16 digits) : 999999999999999

A4C67FFF new_value

1179010615 CCCCCC00
Press any key to continue

---new_val should be "38D7EA4C67FFF" ... this is what i'm referring it cuts the output value...
closed account (z05DSL3A)
__int64 is available in VC6 but I don't think it will like the << >> operators, as ropez said poor standard compliance.

To clarify, That is the stringstream >> operator I don’t think there is a 64bit implementation of it and the cout <<gets confused with a 64bit int.
Last edited on
got it working on bit64 system ... thanks really appreciate your support
Topic archived. No new replies allowed.