这个问题与另一个问题类似;但是,我想了解为什么会这样。
下面的代码:
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x000000ff', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x0000ff00', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x00ff0000', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0xff000000', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x000000ff', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x0000ffff', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x00ffffff', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0xffffffff', 16)).toString(16));
Run Code Online (Sandbox Code Playgroud)
返回:
ef
be00
ad0000
-22000000
ef
beef
adbeef
-21524111
Run Code Online (Sandbox Code Playgroud)
当我对 .string(16) 的期望是:
ef
be00
ad0000
de000000
ef
beef
adbeef
deadbeef
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?
预先感谢您的帮助。
感谢以下回复者和评论者,以及以下来源:
下面是一个解决方案,它通过提供实用函数来将 radix-16 32 位数字与有符号 32 位整数相互转换:
// Convert 'x' to …Run Code Online (Sandbox Code Playgroud) 晚上好,
我正在研究Kernighan和Ritchie的经典"C编程语言"中的练习.
在几个地方,练习可以创建自己的函数版本,该函数复制标准库中函数的名称.而不是为我的版本创建一个替代名称,我真的想告诉编译器我宁愿使用我的函数版本,然后使用标准库函数.
具体来说,如果我尝试编译一个解决方案来练习1-18,它从每行输入中删除尾随空格和制表符,我使用函数'getline'来读取stdin中的行.不幸的是,这会产生编译器错误,因为getline是在stdio.h中定义的.
我曾尝试使用#undef,但似乎无法使用它.
我已经搜索过其他类似的问题并找到了[这一个] [1]; 然而,它似乎需要黑客标准的库标题,我宁愿不这样做.
提前谢谢你的帮助.
这是代码(为了简短而删除了我的评论):
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 1000
static size_t getline(char s[], size_t lim) {
char c;
size_t i = 0;
while (--lim > 0 && (c = (char)getchar()) != (char)EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
int main(void) {
char line[MAXLINE] = "";
size_t len = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (len …Run Code Online (Sandbox Code Playgroud) 在 Go 中,我尝试将数据写入临时文件,然后转身读取该文件,但没有成功。下面是一个精简的测试程序。我已通过检查临时文件验证数据是否已写入文件。所以,至少我知道数据正在进入文件。我只是无法读出它。
提前谢谢你的帮助
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
)
func main() {
tmpFile, err := ioutil.TempFile("", fmt.Sprintf("%s-", filepath.Base(os.Args[0])))
if err != nil {
log.Fatal("Could not create temporary file", err)
}
fmt.Println("Created temp file: ", tmpFile.Name())
// defer os.Remove(tmpFile.Name())
fmt.Println("Writing some data to the temp file")
if _, err = tmpFile.WriteString("test data"); err != nil {
log.Fatal("Unable to write to temporary file", err)
} else {
fmt.Println("data should have been written")
}
fmt.Println("Trying to read …Run Code Online (Sandbox Code Playgroud)