将十六进制值附加到字符串 [英] Appending hex value to string

查看:86
本文介绍了将十六进制值附加到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$temp = 'abc'.0x12;
print $temp; # prints abc18

在将十六进制值附加到字符串时,它将转换为十进制值,最终结果是字符串.我想要的是将十六进制的ascii值附加到字符串中.

While appending the hex value to string it is getting converted to decimal value and final result is string. what I want is that the ascii value of the hex should get appended to the string.

例如ASCII中的0x12是DC2(设备控制2).

for e.g. 0x12 in ascii is DC2 (device control 2).

推荐答案

我认为Sly Raskal处在正确的轨道上,但使用%c代替了%x,所以:

I think Sly Raskal was on the right track but instead of %x use %c, so:

my $hex = 0x12;
my $ascii = sprintf "%c", $hex;
my $temp = 'abc' . $ascii;

我认为这可以为您提供所需的结果.我是从 http://www.perlmonks.org/?node_id=191039 获得的.

I think this gives the result you were looking for. I got it from http://www.perlmonks.org/?node_id=191039 .

这篇关于将十六进制值附加到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆