小编yin*_*uge的帖子

Java:如何在Java中转换HTML字符实体?

基本上我想解码给定的Html文档,并替换所有特殊字符,例如" "- > " ",">"- > ">".

在.NET中我们可以使用HttpUtility.HtmlDecode.

什么是Java中的等效函数?

html java eclipse string decode

133
推荐指数
6
解决办法
22万
查看次数

如何在Java中呈现可空的原始类型int?

我正在设计一个实体类,它有一个名为"documentYear"的字段,可能有无符号整数值,如1999,2006等.同时,这个字段也可能是"未知",也就是说,不确定文档是哪一年创建.

因此,C#中的可空int类型将非常适合.但是,Java没有像C#那样可以为空的特性.

我有两个选择,但我不喜欢它们:

  1. 使用java.lang.Integer而不是原始类型int;
  2. 使用-1表示"未知"值

有没有人有更好的选择或想法?

更新:我的实体类将有数万个实例; 因此,java.lang.Integer的开销可能对系统的整体性能来说太重了.

java nullable

32
推荐指数
3
解决办法
4万
查看次数

在整数溢出的情况下,(unsigned int)*(int)的结果是什么?unsigned还是int?

在整数溢出的情况下,结果是(unsigned int) * (int)什么?unsigned还是int?什么类型确实数组索引运算符(operator[])承担char*:int,unsigned int或其他什么东西?

我正在审核以下功能,突然出现了这个问题.该功能在第17行有一个漏洞.

// Create a character array and initialize it with init[] 
// repeatedly. The size of this character array is specified by 
// w*h.
char *function4(unsigned int w, unsigned int h, char *init)
{
    char *buf;
    int i;

    if (w*h > 4096)
        return (NULL);

    buf = (char *)malloc(4096+1);
    if (!buf)
        return (NULL);

    for (i=0; i<h; i++)
        memcpy(&buf[i*w], init, w);  // line …
Run Code Online (Sandbox Code Playgroud)

c c++ buffer overflow

8
推荐指数
1
解决办法
2584
查看次数

如何在VC中输入main()例程之前执行一些代码?

我正在阅读Microsoft的CRT源代码,我可以提出以下代码,其中函数__initstdio1将在main()例程之前执行.

问题是,如何在VC(不是VC++代码)中输入main()例程之前执行一些代码?

#include <stdio.h>

#pragma section(".CRT$XIC",long,read)

int __cdecl __initstdio1(void);

#define _CRTALLOC(x) __declspec(allocate(x))

_CRTALLOC(".CRT$XIC") static pinit = __initstdio1;

int z = 1;

int __cdecl __initstdio1(void) {
    z = 10;
    return 0;
}

int main(void) {
    printf("Some code before main!\n");
    printf("z = %d\n", z);
    printf("End!\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出将是:

Some code before main!
z = 10
End!
Run Code Online (Sandbox Code Playgroud)

但是,我无法理解代码.

我在.CRT $ XIC上做了一些谷歌,但没有找到运气.有些专家可以向我解释上面的代码段,尤其是以下内容:

  1. 这条线_CRTALLOC(".CRT$XIC") static pinit = __initstdio1;是什么意思?变量pinit有什么意义?
  2. 在编译期间,编译器(cl.exe)会发出如下警告:

Microsoft(R)32位C/C++优化编译器版本15.00.30729.01 for 80x86版权所有(C)Microsoft Corporation.版权所有.

stdmacro.c
stdmacro.c(9) : warning C4047: 'initializing' : 'int' differs …
Run Code Online (Sandbox Code Playgroud)

c crt msvcrt

8
推荐指数
2
解决办法
5601
查看次数

为什么Microsoft的C编译器需要函数开头的变量?

我目前正在写一个C(不是C++).似乎Microsoft的C编译器要求在函数之上声明所有变量.

例如,以下代码不会通过编译:

int foo(int x) {
    assert(x != 0);
    int y = 2 * x;
    return y;
}
Run Code Online (Sandbox Code Playgroud)

编译器在第三行报告错误,说

error C2143: syntax error : missing ';' before 'type'
Run Code Online (Sandbox Code Playgroud)

如果代码更改为如下所示,它将通过编译:

int foo(int x) {
    int y;
    assert(x != 0);
    y = 2 * x;
    return y;
}
Run Code Online (Sandbox Code Playgroud)

如果我将源文件名更改.c.cpp,则编译也将通过.

我怀疑有一个选项可以关闭编译器的严格性,但我还没有找到它.任何人都可以帮忙吗?

提前致谢.

我正在使用Visual Studio 2008 SP1附带的cl.exe.

添加:

谢谢大家回答!看来我必须使用微软的cl.exe生活在C89中.

c c++

7
推荐指数
2
解决办法
3430
查看次数

在托管堆上分配Int32 ^ i = gcnew Int32()吗?

基本上,我想知道其中的差别之间
Int32^ i = gcnew Int32();

Int32* i2 = new Int32();

我写了以下代码:

#include <stdio.h>
#using <mscorlib.dll>

using namespace System;

int main(void) {

    Int32^ i = gcnew Int32();
    Int32* i2 = new Int32();

    printf("%p %d\n", i2, *i2);
    printf("%p %d\n", i, *i);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它给出了以下输出:

004158B8 0
00E1002C 0
Run Code Online (Sandbox Code Playgroud)

似乎两个整数分配在两个不同的内存位置.

是否在托管堆中分配了gcnew Int32()?还是直接在堆栈上?

.net c++ new-operator

3
推荐指数
1
解决办法
1240
查看次数

如何将任意大整数从基数10转换为基数16?

该程序需要输入任意大的无符号整数,该整数在基数10中表示为一个字符串.输出是表示基数16中的整数的另一个字符串.

例如,输入为"1234567890987654321234567890987654321234567890987654321",输出为"CE3B5A137DD015278E09864703E4FF9952FF6B62C1CB1"

算法越快越好.

如果输入限制在32位或64位整数范围内,那将非常容易; 例如,以下代码可以进行转换:

#define MAX_BUFFER 16
char hex[] = "0123456789ABCDEF";

char* dec2hex(unsigned input) {
    char buff[MAX_BUFFER];
    int i = 0, j = 0;
    char* output;

    if (input == 0) {
        buff[0] = hex[0];
        i = 1;
    } else {
        while (input) {
            buff[i++] = hex[input % 16];
            input = input / 16;
        }
    }

    output = malloc((i + 1) * sizeof(char));
    if (!output) 
        return NULL;

    while (i > 0) {
        output[j++] = buff[--i];        
    }
    output[j] = '\0';

    return …
Run Code Online (Sandbox Code Playgroud)

c base

3
推荐指数
1
解决办法
8501
查看次数

为什么short在C中的结构中存储为4个字节?

我有以下两种结构:

问题是sizeof(内容)返回160.结构由11个短路,6个整数,76个字符,7个浮点数,1个双精度数组成,总共增加到158个字节.我已计数三次,仍有2个字节的差异.

typedef struct TIME_T { 
    short year,mon,day; 
    short hour,min,sec; 
} TIME;

typedef struct { 
    int no; 
    char name[20]; 
    char Code[10]; 
    char DASType[10]; 
    short wlen; 
    float VLtd; 
    int samp; 
    int comp; 
    int locationID; 
    short TranMode; 
    char TranIns[12]; 
    short TimerMode; 
    char ClkType[12]; 
    float ClkErr; 
    float lat; 
    float lon; 
    float alt; 
    float azimuth,incident; 
    short weight; 
    short veloc; 
    int oritype; 
    char seismometer[12]; 
    double sens; 
    TIME start_time; 
    int record_samples; 
} Content;
Run Code Online (Sandbox Code Playgroud)

我写了一小段代码来打印结构中每个变量的位置,然后突然发现float wlen需要4个字节.我的代码如下:

int main(void)
{   
    Content content;
    printf("Sizeof Content: %d\n", sizeof(content)); …
Run Code Online (Sandbox Code Playgroud)

c struct sizeof

2
推荐指数
2
解决办法
3861
查看次数

标签 统计

c ×5

c++ ×3

java ×2

.net ×1

base ×1

buffer ×1

crt ×1

decode ×1

eclipse ×1

html ×1

msvcrt ×1

new-operator ×1

nullable ×1

overflow ×1

sizeof ×1

string ×1

struct ×1