如何压缩一串数字 [英] how to compress a string of numbers
问题描述
大家好。
我一直试图想出一种方法来压缩(压缩)只包含数字的大字符串
是否有可能将每个字符串组合起来几个数字并用8位代表它们而不是让每个角色占用整个八位字节
这是我的输入样本:
543216548798654321320000654897986321
每两个数字应该占用8位
谢谢你
如果你真的确定它只是十进制数字你可以像这样简单:
char digit1 = sample [i];
int value1 = digit1 - ' 0' 跨度>; // 介于0到9之间
char digit0 = sample [i + 1];
int value0 = digit0 - ' 0' 跨度>; // 介于0和9之间
int number = value1 * 10 + value0;
byteBuffer [n] =(字节)数;
确保数值低于128(7位),因此您可以省略1位并将值保存为7位。但这是高级的东西,你应该像来自微软这样理解它。
unsigned int i;
const char * s = 123465789;
vector< char> NUM;
for (i = 0 ; i< strlen(s); i + = 2 )
{
num.push_back((s [i] - ' 0')+(s [i + 1] - ' 0' )* 10 );
}
就像这样的工作?
将向量num的大小是字符串s的一半大小? ?
hello everyone
I've been trying to come up with a way to condense (compress) a large string that only contains number
is it possible to combining each couple of numbers and represent them in 8 bits instead of having each character take up the entire octet
Here is my input sample:
"543216548798654321320000654897986321"
each two numbers should take up 8 bits
thank you
if you are REALLY SURE, that it will only be decimal numbers you can go as easy as this:
char digit1 = sample[i]; int value1 = digit1 - '0'; //is between 0 and 9 char digit0 = sample[i+1]; int value0 = digit0 - '0'; //is between 0 and 9 int number = value1 * 10 + value0; byteBuffer[n] = (byte) number;
The value number is ensured to be below 128 (7 bits), so you can spare out 1 bit and save the value in 7 bits. But that is advanced stuff and you should understand it like here from Microsoft fine explained.
unsigned int i; const char *s = "123465789"; vector<char> num; for (i=0;i<strlen(s);i+=2) { num.push_back((s[i]-'0') + (s[i+1]-'0')*10); }
would something like this work ?
will the size of the vector num be half the size of the string s ??
这篇关于如何压缩一串数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!