16进制的string转unsigned long long
16进制的string转unsigned long long
(尽可能使用int或long long)。 windows下int long longlong 4 4 8 linux下int long longlong 4 8 8
#include<bits/stdc++.h>
using namespace std;
unsigned long long h2ll(std::string h)
{
unsigned long long ans = 0;
// remove 0x
if(h.size()>=2&&h[0]=='0'&&h[1]=='x')
{
std::string t="";
for(int i=2;i!=h.size();i++)
{
t+=h[i];
}
h=t;
}
unsigned long long radix = 1;
char index[17]="0123456789abcdef";
bool flag=1;
for(int i=h.size()-1;i>=0;i--)
{
int j=0;
for(j=0;j!=16;j++)
{
if(h[i]==index[j])
{
ans+=j*radix;
break;
}
if(j>9&&h[i]==(index[j]-32))
{
ans+=j*radix;
break;
}
}
if(j==16)
{
flag=0;
break;
}
radix<<=4;
}
if(!flag)
{
ans=0;
}
return ans;
}
int main()
{
char a[100]="0x7fba905cc010";
string str = a;
cout<<str<<endl;
unsigned long long addr=h2ll(str);
printf("%p\n", addr);
return 0;
}