字符串是如何嵌入到二进制文件中的?

发布于 2024-08-04 18:14:59 字数 72 浏览 5 评论 0原文

我正在编写自己的字节码和虚拟机(在.NET 上),我不明白的一件事是如何将字符串嵌入到我的字节码中。现在有什么想法我应该怎么做吗?

I'm writing my own bytecode and virtual machine (on .NET) and one thing i can't figure out is how to embed strings into my bytecode. Any ideas now how i should do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半山落雨半山空 2024-08-11 18:14:59

显然你正在定义你自己的字节码。这与 .NET CIL 的语法/语法无关,对吗?

如果是这样,并且您关心的是如何对字符串进行编码(而不是其他指令,例如跳转、循环等),您可以为其发明自己的“指令”。

例如,十六进制代码“01xx”可能表示包含 xx 字节 (0 -255) 的字符串。然后,您的语言解释器将被教导将此字符串存储在堆栈(或任何位置)上,并解码位于字节码流下方 xx 字节的以下字节码。

如果您关心的是如何在字节码的任何存储中混合字符数据和数字数据,请提供具体信息,也许有人可以提供帮助......

Apparently you're defining your very own byte code. this has nothing to do with the syntax/grammar of .NET CIL, right ?

If so, and if you concern is how to encode strings (as opposed to other instructions such as jumps, loops, etc.), you can just invent your own "instruction" for it.

For example, hex code "01xx" could be for a string containing xx bytes (0 -255). Your language interpreter would then be taught to store this string on the stack (or whereever) and move to decode the following byte code located xx bytes further down the bytecode stream.

If you concern is how to mix character data and numeric data in whatever storage you have for the bytecode, please provide specifics and maybe someone can help...

波浪屿的海角声 2024-08-11 18:14:59

如果可以在数组中存储数字,那么就可以在同一个数组中存储 ASCII 数据。忽略字符串作为类的概念,简单的字符串无论如何都只是一个字符数组——在 C 中,值为 0 的字节表示字符串的结尾。

作为 C 中的简单概念验证:

int main()
{
    putchar(104); // h
    putchar(101); // e
    putchar(108); // l
    putchar(108); // l
    putchar(111); // o
    putchar(10);  // \n
    return 0;
}

输出:

$ ./a.out
hello

也许是 将字符数组作为字符串引用会有帮助吗?

If you can store numbers in an array, then you can store ASCII data in the same array. Ignoring the idea of a string as a class, a simple string is just a character array anyway -- and in C, a byte with a value of 0 indicates the end of the string.

As a simple proof-of-concept in C:

int main()
{
    putchar(104); // h
    putchar(101); // e
    putchar(108); // l
    putchar(108); // l
    putchar(111); // o
    putchar(10);  // \n
    return 0;
}

Output:

$ ./a.out
hello

Maybe a reference on character arrays as strings would help?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文