Javascript - 将字节数组转换为GUID标准格式 [英] Javascript - convert byte array to GUID standard format

查看:246
本文介绍了Javascript - 将字节数组转换为GUID标准格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器(c#)在发送到客户端之前已将GUID转换为字节数组。我设法将json反序列化为字节数组( base64 string to bytearray [ ^ ])并将字节数组转换为guid(将阵列变为十六进制 [ ^ ])。我现在的问题是转换的GUID不是我想要的格式。我收到

 D6708F733AD4064F87B546DBB6C291A4 

而不是

 738f70d6-d43a-4f06-87b5-46dbb6c291a4 

如果参考在线GUID转换器( GUID转换器 [ ^ ]),GUID采用oracle RAW(16)格式,我需要的格式为标准格式。



我尝试过:



这个我如何从字符串转换为数组

 stringToByteArray = function(str){
var decoding = atob(str);
var i,il = decoding.length;
var array = new Uint8Array(il);

for(i = 0; i< il; ++ i){
array [i] = decoding.charCodeAt(i);
}

返回数组;
}





这是我从Array转换为GUID的方式



 arrayToGuidString = function(byteArray){
return Array.from(byteArray,function(byte){
return('0'+(byte& 0xFF).toString (16))。slice(-2);
})。join('')。replace(/(。{8})(。{4})(。{4})(。{4} )(。{12})/,$ 1- $ 2- $ 3- $ 4- $ 5);
}

解决方案

1 -


2 -


3 -

The server (c#) have convert the GUID to byte array before send to the client. I manage to deserialize the json to byte array (base64 string to bytearray[^]) and convert the byte array to guid (Byte array to hexadecimal[^]). My problem now is the converted GUID is not in the format that I want. I received

D6708F733AD4064F87B546DBB6C291A4

instead of

738f70d6-d43a-4f06-87b5-46dbb6c291a4

If refer to the Online GUID converter (GUID Converter[^]), the GUID is in oracle RAW(16) format and the format I need is in the standard format.

What I have tried:

This how I convert from string to Array

stringToByteArray = function (str) {
        var decoded = atob(str);
        var i, il = decoded.length;
        var array = new Uint8Array(il);

        for (i = 0; i < il; ++i) {
            array[i] = decoded.charCodeAt(i);
        }

        return array;
    }



This is how I convert from Array to GUID

arrayToGuidString = function (byteArray) {
        return Array.from(byteArray, function (byte) {
            return ('0' + (byte & 0xFF).toString(16)).slice(-2);
        }).join('').replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, "$1-$2-$3-$4-$5");
    }

解决方案

1-


2-


3-


这篇关于Javascript - 将字节数组转换为GUID标准格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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