将十六进制流转换为 GIF
如何从以下十六进制流创建 .gif 文件:
0d 0a 0d 0a 47 49 46 38 39 61 01 00 01 00 80 ff 00 ff ff ff 00 00 00 2c 00 00 00 00 01 00 01 00 00 02 02 44 01 00 3b
3b 是 GIF 文件终止符
我正在尝试按照 http: //en.wikipedia.org/wiki/Graphics_Interchange_Format#Example_GIF_file
我想用 Perl 或C/C++。但任何语言都可以。
非常感谢,
谢谢大家的回复。我删除了前导的“0d 0a 0d 0a”...
这是我到目前为止所拥有的:
#!/usr/bin/perl
use strict;
use warnings;
open(IN,"<test.txt");
open(OUT,">test.gif");
my @lines=<IN>;
foreach my $line (@lines){
$line=~s/\n//g;
my @bytes=split(/ /,$line);
foreach my $byte (@bytes){
print OUT $byte;
}
}
close(OUT);
close(IN);
How do I create a .gif file from the following HEX stream:
0d 0a 0d 0a 47 49 46 38 39 61 01 00 01 00 80 ff 00 ff ff ff 00 00 00 2c 00 00 00 00 01 00 01 00 00 02 02 44 01 00 3b
3b is the GIF file terminator
I'm trying to do it following the guide at http://en.wikipedia.org/wiki/Graphics_Interchange_Format#Example_GIF_file
I'd like to implement this in either Perl or C/C++. Any language will do though.
Many thanks in advance,
Thanks guys for all the replies. I removed the leading '0d 0a 0d 0a'...
Here's what I have sofar:
#!/usr/bin/perl
use strict;
use warnings;
open(IN,"<test.txt");
open(OUT,">test.gif");
my @lines=<IN>;
foreach my $line (@lines){
$line=~s/\n//g;
my @bytes=split(/ /,$line);
foreach my $byte (@bytes){
print OUT $byte;
}
}
close(OUT);
close(IN);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 shell 中使用 GNU
echo 来完成此操作
:您还可以使用
xxd
命令将“进行十六进制转储或执行相反的操作” 。然而,令人烦恼的是,它似乎对输入格式非常挑剔。下面是一个使用xxd
的示例:gvim
有一个xxd
接口。使用“工具 → 转换为十六进制”菜单选项(键盘::%!xxd
),然后使用“工具 → 转换回”(:%!xxd -r
)。EMACS 还有一个内置的十六进制编辑器,可通过 Mx hexl-mode 访问(请参阅 编辑二进制文件(手册中)。这也有点烦人,因为在按十六进制代码输入字符之前,您必须输入
CMx
(即 Ctrl-Meta-X):当然,编写一个简单的 C 程序来进行转换是非常容易的:
用法:
另外: 此代码高尔夫问题中有许多答案。
You can do it in the shell, using GNU
echo
:You can also use the
xxd
command which will "make a hexdump or do the reverse". Annoyingly, however, it seems very picky about the input format. Here's an example usingxxd
:gvim
has an interface toxxd
. Use the "Tools → Convert To Hex" menu option (keyboard::%!xxd
) and then "Tools → Convert Back" (:%!xxd -r
).EMACS also has a built-in hex editor, which is accessed by
M-x hexl-mode
(see Editing Binary Files in the manual). It's also a little bit annoying, because you have to typeC-M-x
(i.e. Ctrl-Meta-X) before entering a character by its hex code:Of course, it is very easy to write a simple C program to do the conversion:
usage:
Also: many answers here in this code golf question.
.gif
。如果十六进制数据表示 GIF 图像,则文件应包含它。
.gif
extension.If the hex data represents a GIF image, the file should contain it.
Perl 5.14:(
如果您想要,
-n
和print
可以替换为-p
和$_ =
高尔夫(缩短节目长度。)Perl 5.14:
(
-n
andprint
can be replaced with-p
and$_ =
if you want to golf (shorten the length of the program.)您可能需要阅读
unpack
(或 < a href="http://perldoc.perl.org/functions/hex.html" rel="nofollow">hex
) 和pack
。您还可能会发现perlio
文档对于创建原始文件句柄很有用(所以perl
不会尝试帮助您处理诸如编码或行结尾之类的事情)。You may want to read the documentation for
unpack
(orhex
) andpack
. You may also find theperlio
documentation useful for creating a raw file handle (soperl
doesn't try to help you with things like encodings or line endings).