c++ 数字转为对应ASCII字符--reinterpret_cast

本文通过一个C++示例代码,展示了如何使用reinterpret_cast进行类型转换,从而直接访问整型变量的内存地址,并将其视为字符型指针。这揭示了C++中类型安全的动态类型转换机制,以及对内存的底层操作。

reinterpret_cat

void main() {
	int A = 74;
	int *sh = &A;
	char* pc = reinterpret_cast<char*>(sh);
	cout << *sh << endl;
	cout << pc << endl;
	system("pause");
}

在这里插入图片描述

/*---------------------------------------------------------------------- | AP4_BytesToDoubleBE +---------------------------------------------------------------------*/ double AP4_BytesToDoubleBE(const unsigned char* bytes) { AP4_UI64 i_value = AP4_BytesToUInt64BE(bytes); void* v_value = reinterpret_cast<void*>(&i_value); double* d_value = reinterpret_cast<double*>(v_value); return *d_value; } /*---------------------------------------------------------------------- | AP4_BytesToUInt64BE +---------------------------------------------------------------------*/ AP4_UI64 AP4_BytesToUInt64BE(const unsigned char* bytes) { return ( ((AP4_UI64)bytes[0])<<56 ) | ( ((AP4_UI64)bytes[1])<<48 ) | ( ((AP4_UI64)bytes[2])<<40 ) | ( ((AP4_UI64)bytes[3])<<32 ) | ( ((AP4_UI64)bytes[4])<<24 ) | ( ((AP4_UI64)bytes[5])<<16 ) | ( ((AP4_UI64)bytes[6])<<8 ) | ( ((AP4_UI64)bytes[7]) ); } /*---------------------------------------------------------------------- | AP4_BytesFromDoubleBE +---------------------------------------------------------------------*/ void AP4_BytesFromDoubleBE(unsigned char* bytes, double value) { void* v_value = reinterpret_cast<void*>(&value); AP4_UI64* i_value = reinterpret_cast<AP4_UI64*>(v_value); AP4_BytesFromUInt64BE(bytes, *i_value); } /*---------------------------------------------------------------------- | AP4_BytesFromUInt64BE +---------------------------------------------------------------------*/ void AP4_BytesFromUInt64BE(unsigned char* bytes, AP4_UI64 value) { bytes[0] = (unsigned char)((value >> 56) & 0xFF); bytes[1] = (unsigned char)((value >> 48) & 0xFF); bytes[2] = (unsigned char)((value >> 40) & 0xFF); bytes[3] = (unsigned char)((value >> 32) & 0xFF); bytes[4] = (unsigned char)((value >> 24) & 0xFF); bytes[5] = (unsigned char)((value >> 16) & 0xFF); bytes[6] = (unsigned char)((value >> 8) & 0xFF); bytes[7] = (unsigned char)((value ) & 0xFF); } /*---------------------------------------------------------------------- | AP4_DurationMsFromUnits +---------------------------------------------------------------------*/ AP4_UI32 AP4_DurationMsFromUnits(AP4_UI64 units, AP4_UI32 units_per_second) { if (units_per_second == 0) return 0; return (AP4_UI32)(((double)units*1000.0)/(double)units_per_second); } /*---------------------------------------------------------------------- | AP4_ConvertTime +---------------------------------------------------------------------*/ AP4_UI64 AP4_ConvertTime(AP4_UI64 time_value, AP4_UI32 from_time_scale, AP4_UI32 to_time_scale) { if (from_time_scale == 0) return 0; double ratio = (double)to_time_scale/(double)from_time_scale; return ((AP4_UI64)(0.5+(double)time_value*ratio)); } /*---------------------------------------------------------------------- | AP4_FormatFourChars +---------------------------------------------------------------------*/ void AP4_FormatFourChars(char* str, AP4_UI32 value) { str[0] = (value >> 24) & 0xFF; str[1] = (value >> 16) & 0xFF; str[2] = (value >> 8) & 0xFF; str[3] = (value ) & 0xFF; str[4] = '\0'; } /*---------------------------------------------------------------------- | AP4_FormatFourCharsPrintable +---------------------------------------------------------------------*/ void AP4_FormatFourCharsPrintable(char* str, AP4_UI32 value) { AP4_FormatFourChars(str, value); for (int i=0; i<4; i++) { if (str[i]<' ' || str[i] >= 127) { str[i] = '.'; } } } /*---------------------------------------------------------------------- | AP4_SplitArgs +---------------------------------------------------------------------*/ AP4_Result AP4_SplitArgs(char* arg, char*& arg0, char*& arg1) { arg0 = arg; char* c = arg; while (*c != 0 && *c != ':') { c++; } if (*c == ':') { *c++ = '\0'; arg1 = c; return AP4_SUCCESS; } else { return AP4_FAILURE; } } /*---------------------------------------------------------------------- | AP4_SplitArgs +---------------------------------------------------------------------*/ AP4_Result AP4_SplitArgs(char* arg, char*& arg0, char*& arg1, char*& arg2) { AP4_Result result = AP4_SplitArgs(arg, arg0, arg1); if (AP4_FAILED(result)) return result; return AP4_SplitArgs(arg1, arg1, arg2); } 注释上述代
09-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值