如何在二进制字符串上串起来? [英] How to mid string on a binary string?

查看:92
本文介绍了如何在二进制字符串上串起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从某个位置开始二进制字符串并从该位置返回一个

指针到二进制混音结束。


这样的事情:


char bstr [2048];

char * pos;


pos =中期(BSTR,35); / *返回二进制字符串其余部分的指针

从元素35开始* /


任何帮助显示我如何做到这一点?我不知道怎么做一个字母

* mid(bstr,ele)


谢谢,

jt

I''m needing to take a binary string start at a certain position and return a
pointer from that postion to the end of the binary stirng.

something like this:

char bstr[2048];
char *pos;

pos=mid(bstr,35); / *return a pointer of the rest of the binary string
starting at element 35 */

Any help showing me how I can do this? I don''t know how I can do a char
*mid(bstr,ele)

Thanks,
jt

推荐答案

" jt" < JT **** @ hotmail.com>在消息中写道

新闻:lP ******************* @ tornado.tampabay.rr.com ...
"jt" <jt****@hotmail.com> wrote in message
news:lP*******************@tornado.tampabay.rr.com ...
我需要带二进制字符串


我不熟悉二进制字符串的概念。就C / /
编程语言而言,只有字符串,更严格的是,它们的数组和指向它们的指针。你可以自由地定义

的某些东西,但是我们都必须知道说明

相同的语言。

start在某个位置并从该位置返回一个
指针到二进制搅拌的末尾。


没有像指针这样的东西。 C中的指针总是指向

(或者如果你愿意的话),而且永远不会来自。

这样的东西:

char bstr [2048 ];
char * pos;

pos = mid(bstr,35); / *返回二进制字符串其余部分的指针
从元素35开始* /
I''m needing to take a binary string
I''m not familiar with the concept of a binary string. As far as the C
programming language goes, there are only character strings, more strictly,
arrays of thereof and pointers to them. You are free to define something in
terms of something, but then we both must know that difinition to speak the
same language.
start at a certain position and return a
pointer from that postion to the end of the binary stirng.
There''s no such thing as pointer from to. A pointer in C always points to
(or at if you will) and never from.
something like this:

char bstr[2048];
char *pos;

pos=mid(bstr,35); / *return a pointer of the rest of the binary string
starting at element 35 */




你需要类似的东西:

pos = bstr + 35;

???


请注意,如果一个字符包含超过1位(你从未告诉过多少

你的二进制文件在char,1或CHAR_BIT中),那么你就不能有一个指针

到一点。内存的最小可寻址(可指向)单位是char(字节)。

你可以改变它的位,但为此你不仅需要知道

char'的地址(即ptr到这个字符),还有里面的位位置,

因此它不是一个单一的指向物,而是一个复合指针...

也许你想用一个整数而不是一对

指针+整数...

但我不知道,你是模糊的。 ..


Alex



Do you need something like:
pos = bstr + 35;
???

Mind you, if a char contains more than 1 bit (you never told how much of
your binaries are in a char, 1 or CHAR_BIT), then you can''t have a pointer
to a bit. The minimum addressable(pointable) unit of memory is char (byte).
You may alter its bits though, but for that you need to know not just the
char''s address (i.e. ptr to this char) but also the bit position inside,
hence it''s not really a single pointing thing, rather a compound pointer...
Maybe you would like to use an integer instead of the pair
pointer+integer...
But I don''t know, you''re vague...

Alex


" jt" < JT **** @ hotmail.com>写道:
"jt" <jt****@hotmail.com> writes:
我需要从某个位置开始一个二进制字符串,然后从该位置返回指针到二进制混音的结尾。

char bstr [2048];
char * pos;

pos = mid(bstr,35); / *返回二进制字符串其余部分的指针
从元素35开始* /

任何帮助显示我如何做到这一点?我不知道怎么做一个字母
* mid(bstr,ele)
I''m needing to take a binary string start at a certain position and
return a pointer from that postion to the end of the binary stirng.

something like this:

char bstr[2048];
char *pos;

pos=mid(bstr,35); / *return a pointer of the rest of the binary string
starting at element 35 */

Any help showing me how I can do this? I don''t know how I can do a char
*mid(bstr,ele)




不清楚(对我来说,无论如何)你所说的二进制字符串是什么意思。


AC字符串是一个连续的字符序列终止于和

包括第一个空字符 ;。


大概是在二进制字符串中你想在字符串中允许空字符

- 这意味着你必须有其他的方式

指定它的长度。你似乎没有这样做过。


另外,如果你想存储任意位模式,你会更好

off使用unsigned char而不是char。


如果你只是在谈论普通字符串,你可以忽略上面的大部分




操作字符串的常用方法是通过指针(类型为

char *)到其第一个元素。指针只指定

第一个字符的位置;它作为指向整个字符串的指针,因为

结尾用空字符标记。


因此,忽略确定结束位置的问题,这里是如何获得指向元素35的指针(实际上是第36个元素)



bstr:


char bstr [2048];

char * pos = bstr + 35;


如果你对C'的指针运算感到不舒服,你也可以这样做:
这个:


char bstr [2048];

char * pos =& bstr [35] ;


这是等价的,因为索引算子是用指针算术的术语定义的:x [y]定义相当于*(x + y) ),和

一元*和&运营商互相取消。 (通常x是

a指针,y是整数。)


这是一个例子:


#include< stdio.h>

int main(无效)

{

char * s =" hello,world" ;;

printf(" s = \"%s \" \ n",s);

printf(" s + 7 = \ "%s \" \ n",s + 7);

printf("& s [7] = \"%s \" \ n" ;,& s [7]);

返回0;

}


输出:


s =" hello,world"

s + 7 =" world"

& s [7] =" world"


阅读C FAQ中的第6节数组和指针(谷歌C常见问题解答)。

然后阅读剩下的部分。


-

Keith Thompson(The_Other_Keith) ks *** @ mib。组织< http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< * GT; < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



It''s not clear (to me, anyway) just what you mean by "binary string".

A C string is "a contiguous sequence of characters terminated by and
including the first null character".

Presumably in a "binary string" you want to allow null characters
within the string -- which means you have to have some other way to
specify how long it is. You don''t seem to have done so here.

Also, if you want to store arbitrary bit patterns, you''ll be better
off using unsigned char rather than char.

If you''re just talking about ordinary strings, you can ignore most of
the above.

The usual way of manipulating a string is via a pointer (of type
char*) to its first element. The pointer only specifies where the
first character is; it works as a pointer to the whole string because
the end is marked by the null character.

So, ignoring the question of determining where it ends, here''s how
to get a pointer to element 35 (which is actually the 36th element)
of bstr:

char bstr[2048];
char *pos = bstr + 35;

If you''re uncomfortable with C''s pointer arithmetic, you can also do
this as:

char bstr[2048];
char *pos = &bstr[35];

This is equivalent because the indexing operator is defined in terms
of pointer arithmetic: x[y] is by definition equivalent to *(x+y), and
the unary "*" and "&" operators cancel each other out. (Usually x is
a pointer and y is an integer.)

Here''s an example:

#include <stdio.h>
int main(void)
{
char *s = "hello, world";
printf("s = \"%s\"\n", s);
printf("s+7 = \"%s\"\n", s+7);
printf("&s[7] = \"%s\"\n", &s[7]);
return 0;
}

Output:

s = "hello, world"
s+7 = "world"
&s[7] = "world"

Read section 6, "Arrays and Pointers", of the C FAQ (google "C FAQ").
Then read the rest of it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


你是对的。该字符串中包含空字符。它是我读过的二进制文件

。我不应该提到二进制字符串。


jt

"凯斯汤普森 < KS *** @ mib.org>在消息中写道

news:ln ************ @ nuthaus.mib.org ...
You are correct. The string has null characters in it. Its a binary file
that I read in. I shouldn''t of mentioned binary string.

jt
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
" jt" < JT **** @ hotmail.com>写道:
"jt" <jt****@hotmail.com> writes:
我需要从某个位置开始一个二进制字符串,然后从该位置返回指针到二进制混音的结尾。

char bstr [2048];
char * pos;

pos = mid(bstr,35); / *返回二进制字符串其余部分的指针
从元素35开始* /

任何帮助显示我如何做到这一点?我不知道怎么做一个字母
* mid(bstr,ele)
I''m needing to take a binary string start at a certain position and
return a pointer from that postion to the end of the binary stirng.

something like this:

char bstr[2048];
char *pos;

pos=mid(bstr,35); / *return a pointer of the rest of the binary string
starting at element 35 */

Any help showing me how I can do this? I don''t know how I can do a char
*mid(bstr,ele)



我不清楚(对我而言)就是你通过二进制字符串表示。

AC字符串是一个连续的字符序列,以#
结尾,包括第一个空字符。

大概是二进制字符串你想在字符串中允许空字符
- 这意味着你必须有其他方法来指定它的长度。你似乎没有这样做过。

另外,如果你想存储任意位模式,你最好使用unsigned char而不是char。<如果你只是在讨论普通的字符串,你可以忽略上面的大部分内容。

操作字符串的常用方法是通过指针(类型
char *)到它的第一个元素。指针只指定
第一个字符的位置;它作为指向整个字符串的指针,因为结尾是由空字符标记的。

所以,忽略了确定它结束的问题,这里是如何
获取指向元素35的指针(实际上是第36个元素)
bstr:

char bstr [2048];
char * pos = bstr + 35; <如果你对C'的指针运算感到不舒服,你也可以这样做:

char bstr [2048];
char * pos =& bstr [35];

这是等价的,因为索引算子是用指针算术的术语定义的:x [y]的定义相当于*(x + y),和/或一元*和&运营商互相取消。 (通常x是指针,y是整数。)

这是一个例子:

#include< stdio.h>
int main(void)
{
char * s =" hello,world" ;;
printf(" s = \"%s \" \ n" ;,s);
printf(" s + 7 = \"%s \" \ n",s + 7);
printf("& s [7 ] = \"%s \" \ n",& s [7]);
返回0;
}

输出:

s =" hello,world"
s + 7 =" world"
& s [7] =" world"

阅读部分6,C语言常见问题解答中的数组和指针(谷歌C常见问题解答)。
然后阅读其余部分。

-
基思汤普森( The_Other_Keith) ks***@mib.org
< http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *>
< http://users.sdsc.edu/~kst>
我们必须做点什么G。这是事情。因此,我们必须这样做。



It''s not clear (to me, anyway) just what you mean by "binary string".

A C string is "a contiguous sequence of characters terminated by and
including the first null character".

Presumably in a "binary string" you want to allow null characters
within the string -- which means you have to have some other way to
specify how long it is. You don''t seem to have done so here.

Also, if you want to store arbitrary bit patterns, you''ll be better
off using unsigned char rather than char.

If you''re just talking about ordinary strings, you can ignore most of
the above.

The usual way of manipulating a string is via a pointer (of type
char*) to its first element. The pointer only specifies where the
first character is; it works as a pointer to the whole string because
the end is marked by the null character.

So, ignoring the question of determining where it ends, here''s how
to get a pointer to element 35 (which is actually the 36th element)
of bstr:

char bstr[2048];
char *pos = bstr + 35;

If you''re uncomfortable with C''s pointer arithmetic, you can also do
this as:

char bstr[2048];
char *pos = &bstr[35];

This is equivalent because the indexing operator is defined in terms
of pointer arithmetic: x[y] is by definition equivalent to *(x+y), and
the unary "*" and "&" operators cancel each other out. (Usually x is
a pointer and y is an integer.)

Here''s an example:

#include <stdio.h>
int main(void)
{
char *s = "hello, world";
printf("s = \"%s\"\n", s);
printf("s+7 = \"%s\"\n", s+7);
printf("&s[7] = \"%s\"\n", &s[7]);
return 0;
}

Output:

s = "hello, world"
s+7 = "world"
&s[7] = "world"

Read section 6, "Arrays and Pointers", of the C FAQ (google "C FAQ").
Then read the rest of it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org
<http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*>
<http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.



这篇关于如何在二进制字符串上串起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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