转换... [英] conversion...

查看:82
本文介绍了转换...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

unsigned short int i;

char c [] = {0x00,0x04,0x00,0x0C,''A'',''B'',''C'',0x00} ;

^^^^^^^^^^^

第3和第4个字节代表短值12。

问:将这两个字节从c复制到i的最简单方法是什么?

Herwig

unsigned short int i;
char c[] = { 0x00, 0x04, 0x00, 0x0C, ''A'', ''B'', ''C'', 0x00 };
^^^^^^^^^^^
The 3rd and 4th byte represent the short value "12".
Q: what is the easiest way to copy these 2 bytes from c to i?
Herwig

推荐答案

Herwig写道:
Herwig wrote:
unsigned short int i;
char c [] = {0x00,0x04,0x00,0x0C,''A'',''B'',''C '',0x00};
^^^^^^^^^^^
第3和第4个字节代表短值12。
问:什么是最简单的方法将这2个字节从c复制到i?
Herwig
unsigned short int i;
char c[] = { 0x00, 0x04, 0x00, 0x0C, ''A'', ''B'', ''C'', 0x00 };
^^^^^^^^^^^
The 3rd and 4th byte represent the short value "12".
Q: what is the easiest way to copy these 2 bytes from c to i?
Herwig



memcpy(& i,& c [2],2);


也就是说,你有一些可移植性问题要考虑


1)字节顺序(你可能需要反转字节),沿着

unsigned char * p = i;

p [0] = c [3];

p [1] = c [2];


2)char和short的相对大小假设为是一个字节为

char(由标准尺寸(char)== 1)保证,并且

简称2字节(不保证)。


3)可能有些人我不知道但是你很快就会听到

...


-David



memcpy(&i, &c[2], 2);

That said, you have some portability issues to consider

1) endianness (you may have to reverse the bytes), along the lines of
unsigned char *p = i;
p[0] = c[3];
p[1] = c[2];

2) relative size of char and short is assumed to be 1 byte for
char (guaranteed by standard that sizeof(char) == 1), and
2 bytes for short (not guaranteed).

3) Probably some others I''m ignorant of but you''ll no doubt hear
of shortly...

-David


Herwig写道:
Herwig wrote:
unsigned short int i;
char c [] = {0x00, 0x04,0x00,0x0C,''A'',''B'',''C'',0x00};
^^^^^^^^^^^
第3和第4 byte表示短值12。
问:将这2个字节从c复制到i的最简单方法是什么?
unsigned short int i;
char c[] = { 0x00, 0x04, 0x00, 0x0C, ''A'', ''B'', ''C'', 0x00 };
^^^^^^^^^^^
The 3rd and 4th byte represent the short value "12".
Q: what is the easiest way to copy these 2 bytes from c to i?




不知道关于*最简单*;我想要有一个可移动的战斗机会




我会去

i =((unsigned char)c [2]<< CHAR_BIT)+(unsigned char)c [3];


我遇到什么问题看?


-

Chris" electric hedgehog" Dollin



Dunno about the *easiest*; I''d like to have a fighting chance of
being portable, as well.

I''d go for

i = ((unsigned char) c[2] << CHAR_BIT) + (unsigned char) c[3];

What problems didn''t I see?

--
Chris "electric hedgehog" Dollin


文章< d4 ************************** @发布。 google.com>,

Herwig< in ** @ bitart.at>写道:

:unsigned short int i;

:char c [] = {0x00,0x04,0x00,0x0C,''A'',''B'' ,''C'',0x00};


:第3和第4个字节代表短值12。

:问:什么是将这2个字节从c复制到i的最简单方法是什么?


i =(无符号短整数)c [2]<< 8 || (unsigned short)c [3];

-

Ceci,ce n''est pasuneidée。
In article <d4**************************@posting.google.com >,
Herwig <in**@bitart.at> wrote:
:unsigned short int i;
:char c[] = { 0x00, 0x04, 0x00, 0x0C, ''A'', ''B'', ''C'', 0x00 };

:The 3rd and 4th byte represent the short value "12".
:Q: what is the easiest way to copy these 2 bytes from c to i?

i = (unsigned short) c[2] <<8 || (unsigned short) c[3];
--
Ceci, ce n''est pas une idée.


这篇关于转换...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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