LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 715|回复: 5

刚入门,帮我看看下面的代码

[复制链接]
发表于 2004-12-8 10:03:08 | 显示全部楼层 |阅读模式
public static int constructInt(byte[] in, int offset) {

        int ret = ((int) in[offset + 3] & 0xff);

        ret = (ret << 8) | ((int) in[offset + 2] & 0xff);

        ret = (ret << 8) | ((int) in[offset + 1] & 0xff);

        ret = (ret << 8) | ((int) in[offset + 0] & 0xff);

        return (ret);
这里的 byte[] in和in[offset+3]是啥意思啊?
发表于 2004-12-8 11:41:39 | 显示全部楼层
就是用4个byte(8位)组合成一个int(32位就是4个byte)
0 +1 +2 +3分别是对应的4个byte
 楼主| 发表于 2004-12-8 17:19:46 | 显示全部楼层
in是啥东西啊?
发表于 2004-12-8 18:24:56 | 显示全部楼层
byte[] in
 楼主| 发表于 2004-12-8 18:53:04 | 显示全部楼层
我想将下面一段转成C,该如何?
public static int constructInt(byte[] in, int offset) {

        int ret = ((int) in[offset + 3] & 0xff);

        ret = (ret << 8) | ((int) in[offset + 2] & 0xff);

        ret = (ret << 8) | ((int) in[offset + 1] & 0xff);

        ret = (ret << 8) | ((int) in[offset + 0] & 0xff);

        return (ret);

    }
 楼主| 发表于 2004-12-8 21:47:59 | 显示全部楼层
我想从BMP的数据区的的654字节的地方读取一个字节数据到一个整型变量中:
int a1;
rewind(fp1);
                        fseek(fp1,654L,1);
                        fread(&a1,sizeof(char),1,fp1);
                        printf("%d ", a1.R_Data);
                       
为什么总是得到的数据大于>256呢,这是不可能的啊!一个字节怎么会大于256呢?请教!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表