Apr 17, 2008 at 8:41am UTC
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...
Apr 17, 2008 at 10:22am UTC
Last edited on Apr 17, 2008 at 10:23am UTC
Apr 17, 2008 at 10:39am UTC
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 Apr 17, 2008 at 10:42am UTC
Apr 17, 2008 at 10:45am UTC
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)
Apr 17, 2008 at 10:53am UTC
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)
Apr 17, 2008 at 11:01am UTC
#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...
Apr 17, 2008 at 11:04am UTC
__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 Apr 17, 2008 at 11:10am UTC
Apr 17, 2008 at 11:06am UTC
got it working on bit64 system ... thanks really appreciate your support