首页 > 解决方案 > 谷歌脚本转换为十六进制

问题描述

使用 Google 表格,我想将值作为十六进制数字存储在单元格中。

但是 usingUtilities.formatString("0x%2x", byte)将文字“0x%2x”存储在单元格中。

按预期使用Utilities.formatString("0x%2i", byte)商店“0x [数字]”。

知道我做错了什么吗?

最好的问候斯特凡

标签: google-apps-scriptgoogle-sheets

解决方案


这是一个可以做你想做的功能:

// function to convert decimal to hexadecimal 
function DecToHex(value) {    
  var result = "";
  while( value != 0 ) {
    var temp = value % 16;
    Logger.log(temp);
    var hex = temp < 10 ? String.fromCharCode(temp+48) : String.fromCharCode(temp+55);
    result = hex.concat(result);
    value = Math.floor(value/16);
  }
  if( ( result.length %2 ) != 0 ) result = "0"+result;
  result = "0x"+result;
  return result;
} 

推荐阅读