将char [4](二进制)转换为unsigned long [英] convert a char[4] (binary) to an unsigned long

查看:189
本文介绍了将char [4](二进制)转换为unsigned long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想将char [4](二进制)转换为unsigned long。我该怎么办?

这个?


谢谢,

Vincent

Hi all,

I want to convert a char[4] (binary) to an unsigned long. How can I do
this?

Thanks,
Vincent

推荐答案

Vincent sade:
Vincent sade:
大家好,

我想将char [4](二进制)转换为unsigned long 。我该怎么办?

谢谢,
Vincent
Hi all,

I want to convert a char[4] (binary) to an unsigned long. How can I do
this?

Thanks,
Vincent




断言(sizeof(long)== 4 );

char b [4] = {0x01,0x02,0x03,0x04};

unsigned long a = 0;

a | = (b [0]<< 24);

a | =(b [1]<< 16);

a | =(b [2]< ;< 8);

a | = b [3];


但如果您在b [3]中有MSB,那么您应该撤销订单。

小心大端和小端。


Tobias

-

重要提示:此电子邮件和附件的内容是保密的

并且可能受到法律特权和/或受版权保护。

禁止将其任何部分复制或传播给他人。可能

是非法的。



assert(sizeof(long) == 4);
char b[4] = {0x01,0x02,0x03,0x04};
unsigned long a = 0;
a |= (b[0] << 24);
a |= (b[1] << 16);
a |= (b[2] << 8);
a |= b[3];

But if you have MSB in b[3] then you should reverse the order.
Beware of big endian and little endian.

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.


Vincent写道:
Vincent wrote:
大家好,

我想将char [4](二进制)转换为unsigned long。我怎么能这样做?

谢谢,
Vincent
Hi all,

I want to convert a char[4] (binary) to an unsigned long. How can I do
this?

Thanks,
Vincent




我认为这不可能不知道

机器的字节顺序。也许有人会纠正我。


如果char [4]来自机器,那么你可能会做一个

reinterpret_cast,但我是几乎是积极的,它不会是便携式的。


//假设这有你的二进制无符号长

extern char ul_bin [4];

unsigned long ul = * reinterpret_cast< unsigned long *>(ul_bin);


- John Ratliff



I don''t think this is possible without knowing the endian-ness of the
machine. Maybe someone will correct me.

If the char[4] came from the machine, then you could probably do a
reinterpret_cast, but I''m almost positive it won''t be portable.

// assume this has your binary unsigned long
extern char ul_bin[4];
unsigned long ul = *reinterpret_cast<unsigned long *>(ul_bin);

--John Ratliff


该程序必须在MS Windows 2000上运行。字符[4]是一组

字符,从文件中读取。


我希望这将有助于您回答我的问题。

The program will have to work on MS Windows 2000. The char[4] is a set
of characters, read from a file.

I hope this will help you answering my question.


这篇关于将char [4](二进制)转换为unsigned long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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