使用西里尔字母创建了一个 Qt 项目。打开项目后,文件以损坏的编码打开,尽管如此,所有内容都在应用程序本身中正确显示。它是:
ui->label->setText("хай");
重新打开项目后,它变成了:
ui->label->setText("Допустим привет");
一般来说,情况是这样的:我的电报机器人记录任何传入的消息,并将日志发送到特殊组时,使用 Markdown。我遇到这样的问题,其中一个用户的昵称中有下划线(_),MD将其定义为要删除的行的开头,当找不到第二个这样的字符时会导致错误。 Python 中是否有函数或方法可以将常规 str 转换为 raw?
bot.send_message(groupId, username + '\n' + msg, parse_mode = 'markdown')
# username - str, содержащий специальный символ для markdown
我对一个公式感兴趣,如果父块的宽度小于我们设置的子块的宽度,则该公式可以计算哪个块应占用父块内的多少空间。
为什么MAIN块的宽度为423px,而ASIDE块的宽度为375px。如果父类减去100px,那么MAIN的宽度已经是393px,ASIDE的宽度是305px。您如何计算块的宽度?
/* Ширина parent 800px */
* {
box-sizing: border-box;
}
body {
width: 1000px;
border: 1px solid red;
height: 600px;
padding: 50px;
}
.parent {
width: 800px;
margin: 0 auto;
border: 1px solid green;
height: 100%;
display: flex;
align-items: start;
}
.aside-block {
width: min(50%, 450px);
border: 1px solid blue;
}
.main-block {
width: max(50%, 450px);
border: 1px solid black;
}
<div class="parent">
<div class="aside-block">ASIDE</div>
<div class="main-block">MAIN</div>
<div>
/* Ширина parent 700px */
* {
box-sizing: border-box;
}
body {
width: 1000px;
border: 1px solid red;
height: 600px;
padding: 50px;
}
.parent {
width: 700px;
margin: 0 auto;
border: 1px solid green;
height: 100%;
display: flex;
align-items: start;
}
.aside-block {
width: min(50%, 450px);
border: 1px solid blue;
}
.main-block {
width: max(50%, 450px);
border: 1px solid black;
}
<div class="parent">
<div class="aside-block">ASIDE</div>
<div class="main-block">MAIN</div>
<div>
请帮帮我,我已经为这个问题绞尽脑汁三天了。
我正在尝试编写一个带有内部数据库(SQLite)的WinForms应用程序,该数据库使用EntityFramework进行管理。问题是,当我尝试向表中添加记录时,出现以下异常:
“找不到具有固定名称“System.Data.SQLite”的 ADO.NET 提供程序的实体框架提供程序。请确保该提供程序已在应用程序配置文件的“entityFramework”部分中注册。请参阅http://go.microsoft。 com/fwlink/?LinkId=260882了解更多信息。”
这是 App.config 文件。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<entityFramework>
<providers>
<!-- Провайдер для SQL Server -->
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<!-- Провайдер для SQLite -->
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider (Entity Framework 6)"
invariant="System.Data.SQLite.EF6"
description=".NET Framework Data Provider for SQLite (Entity Framework 6)"
type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider"
invariant="System.Data.SQLite"
description=".NET Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="FinanceHelperConnection"
connectionString="Data Source=|DataDirectory|\FinanceHelperDB.sqlite;"
providerName="System.Data.SQLite.EF6"/>
</connectionStrings>
</configuration>
以下是应用程序中断的方法的代码:
private void createItemBtn_Click(object sender, EventArgs e)
{
if (itemNameField.Texts != "")
{
using (AppDbContext db = new AppDbContext())
{
Item newItem = new Item() { Name = itemNameField.Texts };
db.Items.Add(newItem);
db.SaveChanges();
reloadData();
}
}
else
{
MessageBox.Show("Стой, ты забыл заполнить поле 'Название'");
}
}
上下文类也是“经典”:
public class AppDbContext : DbContext
{
public AppDbContext() : base("name=FinanceHelperConnection")
{
}
public DbSet<Item> Items { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Transaction> Transactions { get; set; }
}
我已经通过管理器和控制台重新安装了库 100,500 次 - 它没有帮助(将它们更新到最新版本也没有帮助)。
这不是玩笑,但ChatGpt也挠头,以勤劳的啄木鸟的方式重复:“一切似乎都是正确的。检查你是否已经安装了库,如果需要,请重新安装它们。”请告诉我问题是什么以及如何解决?
我想制作一个 Base64 编码器和解码器。我们得到了 3 个文件:
base64.c:
#include "base64.h"
// ------- hidden header
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <limits.h>
#include <stdlib.h>
typedef bool bin;
// -------
// ------- hidden logic
unsigned short power(unsigned short base, unsigned short exponent)
{
if (exponent == 0)
return 1;
int out = base;
for (int i=0; i<exponent-1; i++)
{
out *= base;
}
return out;
}
// place bits pointer to first bit
void decToBin(unsigned short dec, bin *bits, short bits_count)
{
short i=bits_count;
while (dec != 0)
{
i--;
bits[i] = dec % 2;
dec /= 2;
}
}
// place bits pointer to first bit
unsigned short binToDec(bin *bits, short bits_count)
{
unsigned short dec=0;
for (short i=bits_count-1, j=0; i >= 0; i--, j++)
{
dec += bits[i] * power(2, j);
}
return dec;
}
unsigned short strfind(const char *str, char sub)
{
char *str_pos = strchr(str, sub);
return str_pos - str;
}
// -------
void b64_encode(const char *str, char *base64)
{
// ------- part 0: preparation
const size_t BYTES_COUNT = strlen(str),
BITS_COUNT = BYTES_COUNT * 8,
BASE_BYTES_COUNT = BITS_COUNT / 6;
const unsigned short BASE_BYTES_MOD_COUNT = 6 - BITS_COUNT % 6;
bin *bits = calloc(BITS_COUNT, sizeof(bin));
if (bits == NULL)
{
printf("Failed memory allocation\n");
return;
}
// ------- part 1: converting bytes to bits
for (size_t i=0, j=0; i<BYTES_COUNT; i++, j+=8)
{
decToBin(str[i], &bits[j], 8);
}
// ------- part 2: dividing bits to base-bytes & encoding to base64
size_t i=0, j=0;
for (; j<BASE_BYTES_COUNT; i+=6, j++)
{
const unsigned short dec = binToDec(&bits[i], 6);
base64[j] = BASE64_ALPHABET[dec];
}
if (BASE_BYTES_MOD_COUNT == 6)
{
free(bits);
return;
}
// ------- part X: encoding mod bits to base64
bin mod_base_byte[6] = {};
for (unsigned short k=0; k< BASE_BYTES_MOD_COUNT/2; k++, i++)
mod_base_byte[k] = bits[i];
unsigned short dec = binToDec(&mod_base_byte[0], 6);
base64[j] = BASE64_ALPHABET[dec];
j++;
for (unsigned short k=0; k< BASE_BYTES_MOD_COUNT/2; k++, j++)
{
base64[j] = '=';
}
free(bits);
}
void b64_decode(const char *base64, char *str)
{
// ------- part 0: preparation
const size_t BASE64_LEN = strlen(base64);
unsigned short BASE64_MOD_COUNT = 6;
if (base64[BASE64_LEN-1] == '=') BASE64_MOD_COUNT -= 2;
if (base64[BASE64_LEN-2] == '=') BASE64_MOD_COUNT -= 2;
if (BASE64_MOD_COUNT == 6) BASE64_MOD_COUNT = 0;
const size_t BASE64_LEN_NO_MOD = BASE64_LEN - (6-BASE64_MOD_COUNT)/2,
BITS_COUNT = BASE64_LEN_NO_MOD * 6 - (6-BASE64_MOD_COUNT),
BYTES_COUNT = BITS_COUNT / 8;
bin *bits = calloc(BITS_COUNT, sizeof(bin));
if (bits == NULL)
{
printf("Failed memory allocation\n");
return;
}
// ------- part 1: decoding base64 to bits
for (size_t i=0, j=0; i<BASE64_LEN_NO_MOD; i++, j+=6)
{
unsigned short dec = strfind(BASE64_ALPHABET, base64[i]);
if ((j > BITS_COUNT) && (BASE64_MOD_COUNT == 2))
{
decToBin(
dec & 0b110000,
&bits[j-4],
2
);
continue;
}
else if ((j > BITS_COUNT) && (BASE64_MOD_COUNT == 4))
{
decToBin(
dec & 0b111100,
&bits[j-2],
4
);
continue;
}
decToBin(dec, &bits[j], 6);
}
// ------- part 2: dividing bits to bytes
for (size_t i=0, j=0; i<BYTES_COUNT; i++, j+=8)
{
str[i] = (char) binToDec(&bits[j], 8);
}
free(bits);
}
base64.h:
#ifndef BASE64_H_
#define BASE64_H_
#define BASE64_ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
void b64_encode(const char *str, char *base64);
void b64_decode(const char *base64, char *str);
#endif
test.c:
#include <stdio.h>
#include "base64.h"
int main()
{
char str[14] = "Hello, World!";
char base64[100] = "";
char str_out[14] = "";
b64_encode(str, base64);
printf("%s\n", base64);
b64_decode(base64, str_out);
printf("%s\n", str_out);
char str2[14] = "Man";
char base64_2[100] = "";
char str2_out[14] = "";
b64_encode(str2, base64_2);
printf("%s\n", base64_2);
b64_decode(base64_2, str2_out);
printf("%s\n", str2_out);
return 0;
}
运行时显示两个printf,并出现两行错误:
SGVsbG8sIFdvcmxkIQ==
Hello, World!
base64: malloc.c:2617: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)
通过其他打印,我发现问题出在该行中的b64_encode :
bin *bits = calloc(BITS_COUNT, sizeof(bin));
有趣的是,第一个函数调用工作正常,但第二个函数调用创建了一个错误而不返回 NULL(它只是创建了一个严重错误)。
我试图了解发生了什么,但无法通过调试来做到这一点。
谁知道问题出在哪里?
添加。信息
- 在Linux上编程
- 使用以下命令编译并启动:
# ↓ Получилось без предупреждений
gcc -Wall base64.c test.c -o base64
./base64
