I got a file full with hexadecimal addresses, I've managed to parse the file and fetch them yet I must convert them back to unsigned long, what's the best algorithm or fast way to do so considering the file is very long (a few megs) ?
我有一个十六进制地址的文件,我已经设法解析文件并获取它们但我必须将它们转换回无符号长,什么是最好的算法或快速方法这样做,考虑到文件很长(几个megs )?
Thanks in advance.
提前致谢。
3 个解决方案
#1
You can start with strtoul
. It's part of the standard C library, but I include a link here from the C++ reference.
你可以从strtoul开始。它是标准C库的一部分,但我在C ++参考中包含了一个链接。
char buffer[5];
unsigned long val;
... /* read another 4 valid characters from the file into buffer */
buffer[4] = '\0';
val = strtoul(buffer, NULL, 16);
...
#2
a few meg is not big ! You should do this with a script language, e.g. in python
几兆不大!您应该使用脚本语言执行此操作,例如在python中
out = open("outfile", "w")
try:
for line in open(myfile, "r"):
out.write(int(line, 16))
finally:
out.close()
Assuming it is one hex/line. Even if your hex are not consistent, and you need a regex to parse them, it will almost certainly take less than a few seconds on a decent computer. I would not bother with C unless you file is several GB :)
假设它是一个十六进制/行。即使你的十六进制不一致,你需要一个正则表达式来解析它们,它几乎肯定会在不错的计算机上花费不到几秒钟。我不打扰C,除非你的文件是几GB :)
Now, if you need to do this in C, writing the integers is very easy once you have parsed them. What's your problem exactly ? To write to a file ? Or you do have the hex numbers as strings in a file ? In that later case, you could use fscanf with the x format. Or str* functions, as suggested by Jared.
现在,如果你需要在C中执行此操作,一旦解析了它们,就很容易编写整数。你的问题到底是什么?要写入文件?或者您确实将十六进制数字作为文件中的字符串?在后一种情况下,您可以使用带有x格式的fscanf。或者像Jared所建议的那样str *函数。
#3
If you already have your input parsed then you might as well just use:
如果您已经解析了输入,那么您可以使用:
unsigned long num;
sscanf(your_hex_string, "%lx", &num);
#1
You can start with strtoul
. It's part of the standard C library, but I include a link here from the C++ reference.
你可以从strtoul开始。它是标准C库的一部分,但我在C ++参考中包含了一个链接。
char buffer[5];
unsigned long val;
... /* read another 4 valid characters from the file into buffer */
buffer[4] = '\0';
val = strtoul(buffer, NULL, 16);
...
#2
a few meg is not big ! You should do this with a script language, e.g. in python
几兆不大!您应该使用脚本语言执行此操作,例如在python中
out = open("outfile", "w")
try:
for line in open(myfile, "r"):
out.write(int(line, 16))
finally:
out.close()
Assuming it is one hex/line. Even if your hex are not consistent, and you need a regex to parse them, it will almost certainly take less than a few seconds on a decent computer. I would not bother with C unless you file is several GB :)
假设它是一个十六进制/行。即使你的十六进制不一致,你需要一个正则表达式来解析它们,它几乎肯定会在不错的计算机上花费不到几秒钟。我不打扰C,除非你的文件是几GB :)
Now, if you need to do this in C, writing the integers is very easy once you have parsed them. What's your problem exactly ? To write to a file ? Or you do have the hex numbers as strings in a file ? In that later case, you could use fscanf with the x format. Or str* functions, as suggested by Jared.
现在,如果你需要在C中执行此操作,一旦解析了它们,就很容易编写整数。你的问题到底是什么?要写入文件?或者您确实将十六进制数字作为文件中的字符串?在后一种情况下,您可以使用带有x格式的fscanf。或者像Jared所建议的那样str *函数。
#3
If you already have your input parsed then you might as well just use:
如果您已经解析了输入,那么您可以使用:
unsigned long num;
sscanf(your_hex_string, "%lx", &num);