多字节字符常量和位图文件头类型常量

多字节字符常量和位图文件头类型常量

本文介绍了多字节字符常量和位图文件头类型常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些现有的代码,用于将图像写入位图文件.其中一行代码如下所示:

I have some existing code that I've used to write out an image to a bitmap file. One of the lines of code looks like this:

bfh.bfType='MB';

我想我可能是从某个地方复制的.另一位开发人员对我说这看起来不对,它不应该是'BM'吗?"无论如何它似乎工作正常,但在代码审查中它被重构为:

I think I probably copied that from somewhere. One of the other devs says to me "that doesn't look right, isn't it supposed to be 'BM'?" Anyway it does seem to work ok, but on code review it gets refactored to this:

bfh.bfType=*(WORD*)"BM";

谷歌搜索表明,大多数时候,第一行似乎被使用,而有些时候人们会这样做:

A google search indicates that most of the time, the first line seems to be used, while some of the time people will do this:

bfh.bfType=0x4D42;

那么有什么区别呢?他们怎么能给出正确的结果呢?多字节字符常量到底是什么意思?它们真的一样吗?

So what is the difference? How can they all give the correct result? What does the multi-byte character constant mean anyway? Are they the same really?

推荐答案

这三者(可能)是等价的,但原因不同.

All three are (probably) equivalent, but for different reasons.

bfh.bfType=0x4D42;

这是最容易理解的,它只是用一个数字加载 bfType,该数字恰好在位 8-15 中表示 ASCII 'M',在位 0-7 中表示 ASCII 'B'.如果您以小端格式将其写入流,则该流将包含B"、M".

This is the simplest to understand, it just loads bfType with a number that happens to represent ASCII 'M' in bits 8-15 and ASCII 'B' in bits 0-7. If you write this to a stream in little-endian format, then the stream will contain 'B', 'M'.

bfh.bfType='MB';

这本质上等同于第一条语句——它只是一种表达整数常量的不同方式.它可能完全取决于编译器如何处理它,但它可能会根据您编译的机器的字节序生成一个值.如果您在具有相同字节序的机器上编译和执行,那么当您在流上写出值时,您应该得到B"、M".

This is essentially equivalent to the first statement -- it's just a different way of expressing an integer constant. It probably depends on the compiler exactly what it does with it, but it will probably generate a value according to the endian-ness of the machine you compile on. If you compile and execute on a machine of the same endian-ness, then when you write the value out on the stream you should get 'B', 'M'.

bfh.bfType=*(WORD*)"BM";

这里,BM"使编译器创建一个看起来像 'B'、'M'、'\0' 的数据块,并得到一个指向它的 char*.然后将其强制转换为 WORD*,以便在取消引用时将内存读取为 WORD.因此,它以机器具有的任何字节序将B"、M"读入 bfType.使用相同的字节顺序将其写出来显然会在您的流中放置B"、M".只要你只使用 bfType 写出到流,这是最便携的版本.但是,如果您要与 bfType 进行任何比较/等,那么最好为它选择一个字节序并在读取或写入值时根据需要进行转换.

Here, the "BM" causes the compiler to create a block of data that looks like 'B', 'M', '\0' and get a char* pointing to it. This is then cast to WORD* so that when it's dereferenced it will read the memory as a WORD. Hence it reads the 'B', 'M' into bfType in whatever endian-ness the machine has. Writing it out using the same endian-ness will obviously put 'B', 'M' on your stream. So long as you only use bfType to write out to the stream this is the most portable version. However, if you're doing any comparisons/etc with bfType then it's probably best to pick an endian-ness for it and convert as necessary when reading or writing the value.

这篇关于多字节字符常量和位图文件头类型常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 07:44