show_bytes代码分析

/* show-bytes - prints byte representation of data */
/* $begin show-bytes */
#include <stdio.h>
/* $end show-bytes */
#include <stdlib.h>
#include <string.h>
/* $begin show-bytes */

typedef unsigned char *byte_pointer;
//typedef char *byte_pointer;
//typedef int *byte_pointer;

void show_bytes(byte_pointer start, size_t len) {
    size_t i;
    for (i = 0; i < len; i++)
	printf("%p\t0x%.2x\n", &start[i], start[i]); 
    printf("\n");
}

void show_int(int x) {
    show_bytes((byte_pointer) &x, sizeof(int)); 
}

void show_float(float x) {
    show_bytes((byte_pointer) &x, sizeof(float));
}

void show_pointer(void *x) {
    show_bytes((byte_pointer) &x, sizeof(void *));
}
/* $end show-bytes */


/* $begin test-show-bytes */
void test_show_bytes(int val) {
    int ival = val;
    //float fval = (float) ival;
	double fval = (double) ival;
    int *pval = &ival;
    printf("Stack variable ival = %d\n", ival);
    printf("(int)ival:\n");
    show_int(ival);
    printf("(float)ival:\n");
    show_float(fval);
    printf("&ival:\n");
    show_pointer(pval);
}
/* $end test-show-bytes */

void simple_show_a() {
/* $begin simple-show-a */
int val = 0x87654321;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
/* $end simple-show-a */
}

void simple_show_b() {
/* $begin simple-show-b */
int val = 0x12345678;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
/* $end simple-show-b */
}

void float_eg() {
  int x = 3490593;
  float f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);

  x = 3510593;
  f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);

}

void string_ueg() {
/* $begin show-ustring */
const char *s = "ABCDEF";
show_bytes((byte_pointer) s, strlen(s)); 
/* $end show-ustring */
}

void string_leg() {
/* $begin show-lstring */
const char *s = "abcdef";
show_bytes((byte_pointer) s, strlen(s)); 
/* $end show-lstring */
}

void show_twocomp() 
{
/* $begin show-twocomp */
    short x = 12345; 
    short mx = -x; 
    
    show_bytes((byte_pointer) &x, sizeof(short)); 
    show_bytes((byte_pointer) &mx, sizeof(short)); 
/* $end show-twocomp */
}

int main(int argc, char *argv[])
{
    int val = 12345;
    if (argc > 1) {
        val = strtol(argv[1], NULL, 0);
	printf("calling test_show_bytes\n");
	test_show_bytes(val);
    } else {
	printf("calling show_twocomp\n");
	show_twocomp();
	printf("Calling simple_show_a\n");
	simple_show_a();
	printf("Calling simple_show_b\n");
	simple_show_b();
	printf("Calling float_eg\n");
	float_eg();
	printf("Calling string_ueg\n");
	string_ueg();
	printf("Calling string_leg\n");
	string_leg();
    }
    return 0;
}

一.代码作用.
输出数的地址及其所对应的值.
此代码测试数据是以大端还是以小段模式存放
二.代码分析.
核心函数是show_bytes函数,该函数输出地址以及内容,主函数则是调用多个函数来验证不同类型数据的存放模式
三.输入输出实例
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
四.个人总结
各类数据在内存中都有其储存方式,通过此代码,能更加了解数据储存的方式和形式.

### 实现H5页面跳转至荣耀手机应用商店的方法 对于希望从H5页面直接引导用户前往荣耀手机上的应用市场安装特定应用程序的情况,通常有两种主要的技术方案可以选择:URL Scheme 和 Intent 方式。 #### 使用 URL Scheme 方式 当目标设备支持通过预定义的 scheme 来访问第三方应用时,可以构建如下形式的链接: ```html <a href="market://details?id=com.example.package">打开应用市场</a> ``` 此链接尝试利用 `market://` 协议来触发系统默认的应用商店程序,并传递参数 `id` 指定要查找的应用包名。这种方式适用于大多数基于Android系统的智能手机品牌,包括荣耀系列机型[^1]。 然而需要注意的是,在实际开发过程中应当考虑到不同厂商可能对标准协议有所修改或扩展;因此建议开发者查阅官方文档确认是否存在针对荣耀品牌的特殊配置需求。 #### 使用 Intent 方式 另一种更为推荐的方式是采用 Android 平台特有的 intent 机制来进行跨应用交互。这不仅限于启动其他已安装的应用,还可以用来打开应用商店并定位到某款软件的位置。下面是一个具体的 HTML/JavaScript 示例代码片段展示如何实现这一点: ```javascript function openHuaweiAppStore(packageName) { var url = 'intent://' + packageName + '#Intent;scheme=market;action=android.intent.action.VIEW;end'; window.location.href = encodeURI(url); } // 调用函数传入想要推广的应用包名作为参数 openHuaweiAppStore('com.example.package'); ``` 上述 JavaScript 函数构造了一个符合 Android intent URI 规范的字符串,并将其赋值给当前窗口位置属性以发起请求。这里特别指定了 `scheme=market` 表明意图访问华为自家的应用商店服务[^3]。 为了确保最佳用户体验以及兼容性考虑,可以在网页加载初期检测用户的终端环境(如判断是否为荣耀/Huawei 设备),再决定采取哪种策略最为合适。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值