整数和字符操作 [英] Integer and character manipulation

查看:62
本文介绍了整数和字符操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我需要以下帮助:


1)我需要将16位INT分成两个8位字符,然后是

能够加入这两个字符再次形成原始的16位

整数。


例如有值以二进制表示:


int n = 000000001111111;

- > char a = 00000000;

- > char b = 11111111;


然后能够加入a和b来形成后n。

2)最后,我需要转换一个整数一个字符串。

例如,


int n = 3.142 ---> char string [5] = [''3'',''。'',''1'',''4'',''2'']

非常感谢! !我已经尝试过施法,但就我而言,b $ b知道他们似乎不是答案。我还搜索了许多

教程和网页,但没什么高效的。任何帮助或

建议将受到高度赞赏。非常感谢你!

Hello,

I need some help with the following:

1) I need to split a 16bit INT into two 8bit characters, and then be
able to join these two characters to form again the original 16bit
integer.

For example with values expressed in binary:

int n= 000000001111111;
--> char a=00000000;
--> char b=11111111;

and then be able to join a and b to form back n.
2) And last, I need to convert an integer into a string.
For example,

int n=3.142 ---> char string[5] = [''3'',''.'',''1'',''4'',''2'']
Thank you very much!! I''ve tried working with casting but as far as I
know they don''t seem to be the answer. I''ve also searched numerous
tutorials and web pages, but nothing productive. Any help or
suggestion will be highly appreciated. Thank you very much!!

推荐答案


ru **** @ gmail.com 写道:
您好,

我需要以下方面的帮助:

1)我需要将16位INT分成两个8位字符,然后才能将这两个字符连接起来再形成原始的16位整数。

值以二进制表示的示例:

int n = 000000001111111;
- > char a = 00000000;
- > char b = 11111111;

然后能够加入a和b来形成背面。


你可以使用面具。


a = n& 0xff00;

b = n& 0x00ff;


并使用''unsigned char''代替''char''。 ''char''可以是

相当于''unsigned char''或''signed char''。如果你依赖签名的

char并使用一些无法用signed char表示的值,那么你可以调用实现定义的行为或者提出一个

实现定义信号。

2)最后,我需要将整数转换为字符串。
例如,

int n = 3.142 - - > char string [5] = [''3'',''。'',''1'',''4'',''2'']


' 'int''是标准整数类型的一部分,不能用于

代表有理数。所以你应该使用浮点数的对象类型

或double。要从float类型的给定值生成一个字符串或

double,请使用sprintf / snprintf

非常感谢!!我曾尝试过铸造,但据我所知,他们似乎不是答案。我也搜索了很多
教程和网页,但没什么高效的。任何帮助或
建议将受到高度赞赏。非常感谢!!
Hello,

I need some help with the following:

1) I need to split a 16bit INT into two 8bit characters, and then be
able to join these two characters to form again the original 16bit
integer.

For example with values expressed in binary:

int n= 000000001111111;
--> char a=00000000;
--> char b=11111111;

and then be able to join a and b to form back n.

You can use a mask for that.

a = n & 0xff00;
b = n & 0x00ff;

And use ''unsigned char'' instead of ''char''. ''char'' can be either
equivalent to ''unsigned char'' or ''signed char''. If you rely on signed
char and use some value that cannot be represented with signed char,
then you may invoke implementation defined behavior or raise an
implementation defined signal.

2) And last, I need to convert an integer into a string.
For example,

int n=3.142 ---> char string[5] = [''3'',''.'',''1'',''4'',''2'']

''int'' is a part of the standard integer types and can''t be used to
represent rationals. So you should either use an object type of float
or double. To produce a string from a given value of type float or
double, use sprintf/snprintf

Thank you very much!! I''ve tried working with casting but as far as I
know they don''t seem to be the answer. I''ve also searched numerous
tutorials and web pages, but nothing productive. Any help or
suggestion will be highly appreciated. Thank you very much!!




施放不是一件神奇的事情。铸造存在于

语言中的原因是针对一些极端情况,即:%p期望带有* printf系列函数的(void *)

。在发生错误时将mktime

的返回值与(time_t)-1进行比较。对于*,到*函数/宏。


目前可能还有一两个其他地方让我逃避但是

除了那。它没有产生字符串的原因是

,因为在给定值的情况下,强制转换会产生给定类型的值。由于那里没有
在C中不存在字符串类型,那么你不能指望

将是一个由演员生成的字符串。

-

aegis



casting is not a magical thing. The reason casting exists in the
language is for a few corner cases, namely: %p expecting a (void *)
with *printf family of functions. To compare the return value of mktime
to (time_t)-1 in case of error. For the is*, to* functions/macros.

There may be one or two other places that escape me at the moment but
not much else beyond that. The reason it doesn''t produce a string is
because a cast yields a value of a given type, given a value. As there
does not exist a string type in C, then you can''t expect that there
will be a string produced by a cast.
--
aegis


2005年9月5日12:03:12 -0700, ru **** @ gmail.com 写道:
On 5 Sep 2005 12:03:12 -0700, ru****@gmail.com wrote:
你好,

我需要以下方面的帮助:

1)我需要将16位INT分成两个8位字符,然后才能连接这两个字符再次形成原始的16位
整数。

例如,值以二进制表示:

int n = 000000001111111;
- > char a = 00000000;
- > char b = 11111111;

然后能够加入a和b来形成后面的n。


查看按位运算符,尤其是和转移。

2)最后,我需要将整数转换为字符串。
例如,

int n = 3.142 - - > char string [5] = [''3'',''。'',''1'',''4'',''2'']


3.142不是任何类型的整数值。你的数组不是

字符串。也许你想告诉我们你真正需要的东西而不是提供一个不正确的例子。


非常感谢你!我曾尝试过铸造,但据我所知,他们似乎不是答案。我也搜索了很多
教程和网页,但没什么高效的。任何帮助或
建议将受到高度赞赏。非常感谢!!
Hello,

I need some help with the following:

1) I need to split a 16bit INT into two 8bit characters, and then be
able to join these two characters to form again the original 16bit
integer.

For example with values expressed in binary:

int n= 000000001111111;
--> char a=00000000;
--> char b=11111111;

and then be able to join a and b to form back n.
Look at the bitwise operators, especially "and" and shift.


2) And last, I need to convert an integer into a string.
For example,

int n=3.142 ---> char string[5] = [''3'',''.'',''1'',''4'',''2'']
3.142 is not an integer value of any kind. Your array is not a
string. Perhaps you''d like to tell us what you really need rather
than provide an incorrect example.


Thank you very much!! I''ve tried working with casting but as far as I
know they don''t seem to be the answer. I''ve also searched numerous
tutorials and web pages, but nothing productive. Any help or
suggestion will be highly appreciated. Thank you very much!!




如果您发布代码,您将获得更多帮助。我们不会给你

为你做家庭作业。

<<删除电子邮件的del>>



You will get a lot more help if you post your code. We won''t do you
homework for you.
<<Remove the del for email>>


2005年9月5日13:49:26 -0700,aegis < AE *** @ mad.scientist.com>写道:
On 5 Sep 2005 13:49:26 -0700, "aegis" <ae***@mad.scientist.com> wrote:

ru****@gmail.com写道:

ru****@gmail.com wrote:
你好,

我需要一些帮助以下内容:

1)我需要将16位INT分成两个8位字符,然后才能加入这两个字符再次组成原始16位
整数。

例如,值以二进制表示:

int n = 000000001111111;
- > char a = 00000000;
- > char b = 11111111;

然后能够加入a和b来形成背面。

你可以使用面具。
a = n& 0xff00;
Hello,

I need some help with the following:

1) I need to split a 16bit INT into two 8bit characters, and then be
able to join these two characters to form again the original 16bit
integer.

For example with values expressed in binary:

int n= 000000001111111;
--> char a=00000000;
--> char b=11111111;

and then be able to join a and b to form back n.

You can use a mask for that.

a = n & 0xff00;




你错过了班次。在大多数系统中,

表达式的结果将不适合char。

b = n& 0x00ff;

并使用''unsigned char''代替''char''。 ''char''可以等同于''unsigned char''或''signed char''。如果你依赖签名的
char并使用一些无法用signed char表示的值,那么你可以调用实现定义的行为或者提出一个
实现定义的信号。



You are missing a shift. On most systems, the result of your
expression will not fit in a char.
b = n & 0x00ff;

And use ''unsigned char'' instead of ''char''. ''char'' can be either
equivalent to ''unsigned char'' or ''signed char''. If you rely on signed
char and use some value that cannot be represented with signed char,
then you may invoke implementation defined behavior or raise an
implementation defined signal.


2)最后,我需要将整数转换为字符串。
例如,

int n = 3.142 ---> char string [5] = [''3'',''。'',''1'',''4'',''2'']

2) And last, I need to convert an integer into a string.
For example,

int n=3.142 ---> char string[5] = [''3'',''.'',''1'',''4'',''2'']



' 'int''是标准整数类型的一部分,不能用于表示有理数。所以你应该使用float的对象类型
或double。要从float或
double类型的给定值生成一个字符串,请使用sprintf / snprintf



''int'' is a part of the standard integer types and can''t be used to
represent rationals. So you should either use an object type of float
or double. To produce a string from a given value of type float or
double, use sprintf/snprintf


非常感谢!!我曾尝试过铸造,但据我所知,他们似乎不是答案。我也搜索了很多
教程和网页,但没什么高效的。任何帮助或
建议将受到高度赞赏。非常感谢!!

Thank you very much!! I''ve tried working with casting but as far as I
know they don''t seem to be the answer. I''ve also searched numerous
tutorials and web pages, but nothing productive. Any help or
suggestion will be highly appreciated. Thank you very much!!



施法不是一件神奇的事情。
语言中存在转换的原因是针对一些极端情况,即:%p期望与* printf系列函数一起使用(void *)
。在发生错误时将mktime的返回值与(time_t)-1进行比较。对于is *,to * functions / macros。

目前可能还有一两个其他地方让我逃避,但除此之外别无其他。它没有产生字符串的原因是
因为在给定值的情况下,强制转换会产生给定类型的值。因为在C中不存在字符串类型,所以你不能指望
将是由演员生成的字符串。



casting is not a magical thing. The reason casting exists in the
language is for a few corner cases, namely: %p expecting a (void *)
with *printf family of functions. To compare the return value of mktime
to (time_t)-1 in case of error. For the is*, to* functions/macros.

There may be one or two other places that escape me at the moment but
not much else beyond that. The reason it doesn''t produce a string is
because a cast yields a value of a given type, given a value. As there
does not exist a string type in C, then you can''t expect that there
will be a string produced by a cast.



< ;<删除电子邮件的del>>


<<Remove the del for email>>


这篇关于整数和字符操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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