我得到一个可能很大的数字(UInt.MaxValue:18446744073709551615)作为正常的base10数字。这个数字最终会成为一个文件名:12345678945768.txt
由于 Windows 上的文件名不仅限于数字,我想将其“压缩”为更短的字符串,但需要确保字符串可以映射回数字。
对于较小的数字:0001365555,十六进制比其他数字短得多。到目前为止我发现的所有内容都表明 Base64 是最短的,但事实并非如此。
到目前为止我已经尝试过这个:
//18446744073709551615 - 20
UInt64 i = UInt64.MaxValue; // 0001365555
//"//////////8=" - 12
string encoded = Convert.ToBase64String(BitConverter.GetBytes(i));
//"FFFFFFFFFFFFFFFF" - 16
string hexed = i.ToString("X");
//"MTg0NDY3NDQwNzM3MDk1NTE2MTU=" - 28
string utf = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(i.ToString()));
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来“压缩”整数以类似于十六进制进行转换,但使用 00-zz 而不仅仅是 00-FF?
提前致谢!
下面的代码片段演示了我最近从最近的ISO映像文件重新安装的Delphi XE2中的文本IO和UInt64类型变量的问题 - 编译失败,并显示与缺少Text.ReadUInt64函数或过程相关的错误消息.如果我用更换失败的线路
ReadLn(F,A);
Run Code Online (Sandbox Code Playgroud)
然后程序编译,正确写入
-1
18446744073709551615
Run Code Online (Sandbox Code Playgroud)
到文本文件,然后(如预期的那样)在第二次读取时失败,出现EInOutError:"无效的数字输入".我是否有损坏的安装或有人写入ReadUInt64函数失败?我可以在帮助中找到的对ReadUInt64的唯一引用是以下定义:
function ReadUInt64: UInt64; virtual;
Run Code Online (Sandbox Code Playgroud)
在System.Classes.TBinaryReader.ReadUInt64中.我不确定这是否是"相同"的功能,或者,如果是这样,为什么它是虚拟的......
我也对Help对UInt64的引用感到困惑.它将其定义为:
type UInt64 = Int64;
Run Code Online (Sandbox Code Playgroud)
如果这是正确的,编译器如何知道将UInt64与Int64变量区别对待?
procedure TForm1.Button1Click(Sender: TObject);
var
F : TextFile;
A : Int64;
B : Uint64;
begin
{
Compiler warns on following line with message:
[DCC Warning] Unit1.pas(32): W1012 Constant expression violates subrange bounds
}
A := $FFFFFFFFFFFFFFFF;
B := $FFFFFFFFFFFFFFFF;
AssignFile(F,'test.txt');
ReWrite(F);
Writeln(F,A);
Writeln(F,B);
CloseFile(F);
AssignFile(F,'test.txt');
ReSet(F);
ReadLn(F,A);
{
Fails to compile on following line with message:
[DCC Fatal Error] Unit1.pas(42): E2158 …Run Code Online (Sandbox Code Playgroud) 我想返回一个,uint64_t但结果似乎被截断:
在lib.c:
uint64_t function()
{
uint64_t timestamp = 1422028920000;
return timestamp;
}
Run Code Online (Sandbox Code Playgroud)
在main.c:
uint64_t result = function();
printf("%llu = %llu\n", result, function());
Run Code Online (Sandbox Code Playgroud)
结果:
394745024 = 394745024
Run Code Online (Sandbox Code Playgroud)
在编译时,我收到一个警告:
warning: format '%llu' expects argument of type 'long long unsigned int', but argument 2 has type 'uint64_t' [-Wformat]
warning: format '%llu' expects argument of type 'long long unsigned int', but argument 3 has type 'int' [-Wformat]
Run Code Online (Sandbox Code Playgroud)
为什么编译器认为我的函数的返回类型是int?我们怎样才能解释打印的reslut与函数发送的值不同function()?
你怎么转换NSString成UInt64?
例如,如果UInt64Value存在辅助方法,则类似这样的事情:
NSString *value = @"1234567";
UInt64 convertedValue = [value UInt64Value];
Run Code Online (Sandbox Code Playgroud)
我想在iOS项目中这样做.
我有一个uint64_t,我想将其存储在其中,作为它最右边的 32 位, a float。
基本上,我想要的是,给定一个 float f:
|--------------------------------|--------------------------------|
| 32 bits set to 0 | the 32 bits of our float |
|================================|================================|
^ ^
^ 64-bit unsigned integer ^
|-----------------------------------------------------------------|
Run Code Online (Sandbox Code Playgroud)
我试过铸造它(uint_64t)f,但结果有点奇怪。
有任何想法吗?
我的理解是 UInt64 可以是从 0 到 18446744073709551615 的任何值
我需要将 UInt64 标识符保存到 CoreData,但是我看到的值是:
我最初尝试过 Integer 64,但现在我知道它的范围是:-9223372036854775808 到 9223372036854775807
开发人员通常是否将 UInt64 存储为字符串并在两种类型之间进行转换?这是最佳实践吗?
我们在工作中遇到了一个问题,我们无法使用<inttypes.h>(PRIx64) 中的“可移植”类型来正确打印十六进制格式的 64 位无符号整数。
uint64_t serial = 0x12345678;
usb_printf("Serial: %"PRIx64"\n", serial);
Run Code Online (Sandbox Code Playgroud)
在哪里
void usb_printf(const char *fmt, ...)
{
char string[512];
uint16_t string_length = 0;
va_list va;
va_start(va, fmt);
string_length = vsnprintf(string, ARRAY_SIZE(string), fmt, va);
bsp_usb_cdc_send_buf((uint8_t *)string, string_length);
va_end(va);
}
Run Code Online (Sandbox Code Playgroud)
给出
> Serial: lx
Run Code Online (Sandbox Code Playgroud)
它在 LPC55S28 (Cortex-M33) 上运行,并-specs=nosys.specs -specs=nano.specs使用 GCC 链接。
通过查看 PRIx64 定义,它似乎被定义为:
#define __INT64 "ll"
#define __PRI64(x) __INT64 __STRINGIFY(x)
#define PRIx64 __PRI64(x)
Run Code Online (Sandbox Code Playgroud)
为什么可PRIx64移植类型定义在此平台上的行为不符合人们的预期?
我可以猜测它与使用unsigned long long int有关.
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
typedef unsigned long long int uint64;
int main(int argc, char *argv[])
{
uint64 number_in_question = 600851475143LL;
long double sqrt_in_question = sqrt(number_in_question);
bool primes_array[number_in_question+1];
for (uint64 i = 0; i <= number_in_question; i++) {
primes_array[i] = true;
}
for (uint64 i = 2; i <= sqrt_in_question; i++) {
if(primes_array[i] == true) {
// for every multiple of this prime, mark it as not prime
for (uint64 ii = …Run Code Online (Sandbox Code Playgroud) 我收到了一个数据集合,我需要将它打包成一个uint64_t值,在下面的示例中采用"weatherlog_t"类型的形式
我不允许使用算术运算符(+,++, - , - ,*,%,/,...),但我可以使用按位运算符(&,|,^,<<,>> ,〜)和逻辑运算符(!,=, - ,!=,&&和||)
但是我确实有预定义的add()和sub()函数来处理按位加法和减法,它们在下面的例子中使用.这些已经过测试,我很确定它们的工作达到了这里所需的程度.
根据说明,64位值必须按如下方式排列:
/* - year :: 6 bits -- stored as the number of years since the year 2000.
- month :: 4 bits
- day :: 5 bits
- zip_code :: 16 bits
- high_temp :: in degrees Fahrenheit, stored as an 8-bit signed integer
- low_temp :: in degrees Fahrenheit, stored as 8-bit signed integer
- precipitation :: in mm. stored as a 10-bit unsigned integer.
- average_wind_speed :: …Run Code Online (Sandbox Code Playgroud) 在uint64_t和long的混合上使用算术会在arm(C ++编译器)上产生不需要的结果。相同的代码可以在x86上正常工作。
如果将long替换为uint64_t,则它将按预期工作。
Armv7编译器是C ++(Debian 6.3.0-18 + deb9u1)6.3.0 20170516
这里的代码也:http : //cpp.sh/2xrnu
int main()
{
uint64_t x = 1000UL * 60 * 60 * 24 * 31;
int i1 = 31;
long l2 = 1000 * 60 * 60 * 24;
uint64_t u2 = 1000 * 60 * 60 * 24;
std::cout << "x : " << x << std::endl;
std::cout << "i1 : " << i1 << std::endl;
std::cout << "l2 : " << l2 << std::endl;
std::cout …Run Code Online (Sandbox Code Playgroud)