最有效的方法为无符号的16位整数存储到文件中 [英] Most efficient way to store an unsigned 16-bit Integer to a file

查看:293
本文介绍了最有效的方法为无符号的16位整数存储到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一本字典COM pressor用C与正因为如此字典最大大小64000,我存储我的条目,16位整数。

I'm making a dictionary compressor in C with dictionary max size 64000. Because of this, I'm storing my entries as 16-bit integers.

目前,我正在做什么:
 要带code'一',我得到它的ASCII值,97,然后这个数字转换为字符串重新97. 16位整数的presentation所以我最终编码0000000001100001的一个,这显然不是在短期内节省了很大的空间。

What I'm currently doing: To encode 'a', I get its ASCII value, 97, and then convert this number into a string representation of the 16-bit integer of 97. So I end up encoding '0000000001100001' for 'a', which obviously isn't saving much space in the short run.

我知道,该算法的更有效的版本会(直到我们需要更多的存储空间更少位)与较小的整数大小,但是我不知道是否有更好的方法为

I'm aware that more efficient versions of this algorithm would start with smaller integer sizes (less bits of storage until we need more), but I'm wondering if there's a better way to either


  1. 我的转换整数'97'成固定长度的ASCII字符串,可以存储16位数据(97会为x位数,46347还将为x位)

  1. Convert my integer '97' into an ASCII string of fixed length that can store 16 bits of data (97 would be x digits, 46347 would also be x digits)

写的只能存储1和0的文件。因为它是,它似乎像我写16个ASCII字符到一个文本文件,每个为8位......所以这不是真正帮助的原因很多,是吗?

writing to a file that can ONLY store 1s and 0s. Because as it is, it seems like I'm writing 16 ascii characters to a text file, each of which is 8 bits...so that's not really helping the cause much, is it?

请让我知道如果我能以任何方式更加清晰。我是pretty新来这个网站。谢谢!

Please let me know if I can be more clear in any way. I'm pretty new to this site. Thank you!

编辑:我如何保存我的字典是完全取决于我,据我所知。我只知道,我需要能够方便地阅读EN codeD文件回来,从它那里得到的整数。

How I store my dictionary is entirely up to me as far as I know. I just know that I need to be able to easily read the encoded file back and get the integers from it.

另外,我可以的包含了stdio.h,stdlib.h中,string.h中,和头文件我写的程序。

Also, I can only include stdio.h, stdlib.h, string.h, and header files I wrote for the program.

推荐答案

请,不要忽略这些人谁是建议你直接写入文件。有若干与该问题,最终落入整数重新presentation的类别。

Please, do ignore these people who are suggesting that you "write directly to the file". There are a number of issues with that, which ultimately fall into the category of "integer representation".

假设你使用它采用32位的整数和大端重新presentation X系统写 unsigned int类型取值直接到一个文件,你会结束问题阅读系统Y,它采用16位的整数和little endian重新presentation,或System z上它采用64位整数混合端重新presentation和32位填充该文件。

Suppose you write unsigned ints directly to a file using system X which uses 32-bit ints and big endian representation, you'll end up with issues reading that file on system Y which uses 16-bit ints and little endian representation, or system Z which uses 64-bit ints with mixed endian representation and 32 padding bits.

下在考虑抽象,让你的前preSS你的算法可移植的,这样的您不必编写不同的code为每个操作系统!的下面是一个发展例如阅读和改装4十六进制数字到 unsigned int类型价值,可移植的:

C was developed with abstractions in mind that allow you to express your algorithm portably, so that you don't have to write different code for each OS! Here's an example of reading and converting four hex digits to an unsigned int value, portably:

unsigned int value;
assert(fscanf(fd, "%04x", &value) == 1);

下面就是我会写这个值,可移植:

Here's how I'd write that value, portably:

value &= 0xFFFF;
fprintf(fd, "%04x", value);

假设你的 unsigned int类型值占两个字节,这里就是我读到这两个字节,可移植的,使用大端再presentation:

Supposing your unsigned int values occupy two bytes, here's how I'd read those two bytes, portably, using big endian representation:

int hi = fgetc(fd);
int lo = fgetc(fd);
unsigned int value = 0;
assert(hi >= 0 && lo >= 0);
value += hi & 0xFF; value <<= 8;
value += lo & 0xFF;

...这是我怎么会写这两个字节,其大端顺序:

... and here's how I'd write those two bytes, in their big endian order:

fputc((value >> 8) & 0xFF, fd);
fputc(value & 0xFF, fd);

也许你更感兴趣的是小尾数。整洁的事情就是,code真的是没有什么不同。下面是输入:

Perhaps you're more interested in little endian. The neat thing is, the code really isn't that different. Here's input:

int lo = fgetc(fd);
int hi = fgetc(fd);
unsigned int value = 0;
assert(hi >= 0 && lo >= 0);
value += hi & 0xFF; value <<= 8;
value += lo & 0xFF;

...这是输出:

... and here's output:

fputc(value & 0xFF, fd);
fputc((value >> 8) & 0xFF, fd);

这篇关于最有效的方法为无符号的16位整数存储到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆