将数据输入和输出字符串 [英] getting data in and out of strings

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

问题描述

我正在尝试做的事情是我以前从未真正搞过的事情,这就是内存的实际分配和访问方式.


what i''m trying to do is something i havn''t really messed around with before, and that''s how memory is actually allocated and accessed.


#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    short short1;
    char * pointsToShort1;
    char s1[100] = "";
    short1 = 2;
    pointsToShort1 = (char *) &short1;   // cast referenced short as char *
    s1[0] = pointsToShort1[0];           // store both bytes in the string
    s1[1] = pointsToShort1[1];
    s1[2] = '\0';                        // null terminate for strcat
    strcat(s1, "oh hai hello wassup?");
    cout << s1 << "\n";                  //print string with short at front
                                         // gonna print out funky,  but that's fine
    
    //now i try and retrieve it from the string

    short short2;
    char * s2;
    short2 =  *((short *) s1); /*cast the char pointer pointing to the front of the string as a short pointer and store it's dereferenced value*/

    s2 = &s1[2];      /* have a new pointer, pointing 2 bytes up the first string
                         to omit the 2 bytes in short. */

    cout << short2 << "   " << s2 << "\n";
    return 0;
}



我做错了,因为当我第一次打印s1时,它只在前面打印一个额外的字符,并且它是一张笑脸(不必担心实际的字符,只是它只是一个字符而已) ),然后正常其余字符串.然后,当我尝试获取short值时,short2最终输出28418,而s2最终为"h hai hello wassup?". .短是两个字节吗?



i''m doing something wrong, because when i first print out s1, it only prints one extra character at the front, and it''s a smiley face( not worried about the actual character, just that it''s only one character), and then the rest of the string normally. Then when i try and get the short value, short2 ends up printing out 28418, and s2 ends up being "h hai hello wassup?" . short is two bytes right? What''s going on here?

推荐答案

由于short1等于2,这意味着它由低字节2和高字节0组成.这使您的s1等于{2,0,0}.
ascii值为2是一张笑脸,您只能打印1个字符,因为空字符标记了字符串结尾.

这也解释了s2是"h hai hello wassup?",因为s1的第三个字符确实是"h".

short2由s1的前两个字符组成,分别是2和"o",即111.
2 + 111 * 256 = 28418.
Since short1 equals 2, that means it consists of low byte 2 and the high byte 0. This makes your s1 equal to {2, 0, 0}.
The ascii value of 2 is a smiley face and you only get 1 character printed because the null-character marks the string end.

This also explains s2 being "h hai hello wassup?", since the third character of s1 is indeed the ''h''.

The short2 consists of the first two characters of s1, which are 2 and ''o'', which is 111.
2 + 111* 256 = 28418.


此外,请注意混合使用两个概念"char"和"string".从技术上讲,在直线C中,不存在字符串"之类的东西.哦,我们大多数人都使用惯例,将"char"序列等同为字符串",并且有许多使用字符串"的库例程,例如strcat(). >
但是,"char"只是8位,理论上讲,它代表8位ansii字符集中的某些内容(有些可打印,有些则不行).

按照惯例,字符串"是这一系列的8位字符",由特定的8个字符NULL(0x00)终止.

因此,通过将指向short的指针的类型转换为指向char的指针的方式,您可以摆弄8位数据字节,即"char",但是一旦决定使用strcat()将数据视为字符串",就可以遇到"NULL字节在哪里"的问题.尽管您确实意识到了这一点并在s1 [2]处终止了s1"string",但您的short的高字节恰好是0,因此它也在s1 [1]处终止了s1"string".

因此,strcat()开始按照定义的那样在s1 [1]处覆盖s1(覆盖终止NULL).

请注意,"short1"的其他值将具有不同的结果.对于任何值> ;,您在s [2]处的NULL都是必需的. 255

-----
编辑以解决我对s [1]和s [2]的困惑.困惑自己:)
Also, be care mixing two concepts, "char" and "string". Technically, in straight C, there is no such thing as a "string". Oh, there are conventions that most of us use that equate a sequence of "char"s to be viewed as a "string" and there are a number of library routines that work with "strings", like strcat().

However, "char" is just 8 bits that, theory goes, represents something in the 8-bit ansii character set (some printable, some not).

By convention, "string" is a series of these 8 bit "char"s that is termined by a specific 8-but character, NULL (0x00).

So, by typcasting a pointer to a short to a pointer to char, you get to fiddle with the 8 bit data bytes, the "char" but once you decided to use strcat() to treat that data as a "string", you ran into the "where is the NULL byte" problem. And while you did recognize this and terminated the s1 "string" at s1[2], the high byte of your short just happened to be 0 so it also terminated the s1 "string" at s1[1].

So strcat() started overwriting s1 at s1[1] (overwriting the terminating NULL) as it is defined to do.

Note that a different value for "short1" would have different results. Your NULL at s[2] would be necessary for any value > 255

-----
edit to fix my confusion with s[1] and s[2]. Confused myself :)


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

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