将UTF-16LE Elixir位串转换为Elixir字符串

时间:2016-09-29 14:42:05

标签: utf-8 elixir utf-16 utf-16le

给定以UTF-16LE编码的Elixir位串:

<<68, 0, 101, 0, 118, 0, 97, 0, 115, 0, 116, 0, 97, 0, 116, 0, 111, 0, 114, 0, 0, 0>>

如何将其转换为可读的Elixir String(它拼写出“Devastator”)?我最接近的是将上面的内容转换为Unicode代码点列表(["0044", "0065", ...])并尝试将\u转义序列添加到它们之前,但Elixir会抛出错误,因为它是一个无效的序列。我没有想法。

2 个答案:

答案 0 :(得分:4)

最简单的方法是使用:unicode模块中的函数:

/// <reference path="../jquery-3.1.0.intellisense.js" />


	var ProjectModel = function (project) {
		var self = this;
		self.Id = ko.observable(project ? project.Id : 0);

		self.CustumerCompany = ko.observable(project ? project.CustumerCompany : "");

		self.CustomerRepresentative = ko.observable(project ? project.CustomerRepresentative : "");

		self.ProjectTitle = ko.observable(project ? project.ProjectTitle : "");

		self.WWSNumber = ko.observable(project ? project.WWSNumber : "");

		self.AqStatus = ko.observable(project ? project.AqStatus : "");

		self.Completed = ko.observable(project ? project.Completed : "");

		self.StartDate = ko.observable(project ? project.StartDate : "");

		self.EndDate = ko.observable(project ? project.EndDate : "");

		self.ProjectLeader = ko.observable(project ? project.ProjectLeader : "");

		self.Deputy = ko.observable(project ? project.Deputy : "");

		self.SalesConsultant = ko.observable(project ? project.SalesConsultant : "");

		self.Service = ko.observableArray(project ? project.Service : []);

	};


	// #endregion

	// #region Project Service Model

	var ProjectServiceModel = function (projectService) {
		var self = this;
		self.Id = ko.observable(projectService ? projectService.Id : 0);

		self.Number = ko.observable(projectService ? projectService.Number : "");

		self.Name = ko.observable(projectService ? projectService.Name : "");

		self.Positions = ko.observableArray(projectService ? projectService.Positions : []);

		self.isEditing = ko.observable(true);

		self.isActive = ko.observable(false);

		self.countSelf = ko.computed(function () {
			if (self.Positions().length > 0) {
				return true;
			}
			else {
				return false;
			}

		},
	 self);

	};


	// #endregion

	// #region Position Model
	var ServicePositionModel = function (servicePosition) {
		var self = this;
		self.Id = ko.observable(servicePosition ? servicePosition.Id : 0);

		self.Number = ko.observable(servicePosition ? servicePosition.Number : "");

		self.Name = ko.observable(servicePosition ? servicePosition.Name : "");

		self.PerformanceGroup = ko.observable(servicePosition ? servicePosition.PerformanceGroup : "");

		self.PerformanceGroupPrice = ko.observable(servicePosition ? servicePosition.PerformanceGroupPrice : "");

		self.Remarks = ko.observable(servicePosition ? servicePosition.Remarks : "");

		self.isEditing = ko.observable(servicePosition ? servicePosition.isEditing : false);

		self.isActive = ko.observable(false);

	};

	// #endregion


	var ProjectViewModel = function () {
		var self = this;

		self.project = ko.observable(new ProjectModel());

		self.projectServices = ko.observableArray([]);

		// #region InitData

		

		self.selectedPerformanceGroup = ko.observableArray([]);
		self.AqStatusTypeData = ko.observableArray([]);
		self.PerformanceGroupTypeData = ko.observableArray([]);
		$.ajax({
			url: "/LeistungManager/InitializeData",
			dataType: "json",
			success: function (json) {
				self.AqStatusTypeData(json.AqStatusType);
				self.PerformanceGroupTypeData(json.PerformanceGroupType);
			}
		});


		self.testOptions = function () {
			
			self.selectedPerformanceGroup;
			debugger;
		}

		// #endregion

		// #region Service Functions
		self.addService = function () {
			var object = new Object();

			object.isEditing = true;
			self.projectServices.push(new ProjectServiceModel(object));

			self.isServiceSelected(false);

		};


		self.saveService = function (projectService) {
			projectService.isEditing(false);

		};

		self.editService = function (projectService) {
			projectService.isEditing(true);

		};

		self.removeService = function (projectService) {
			self.projectServices.remove(projectService);

		};


		self.isServiceSelected = ko.observable(false);

		self.selectedServiceData = ko.observable(new Object());

		self.selectService = function (serviceRowData) {
			if (!serviceRowData.isEditing()) {
				self.isServiceSelected(true);

				serviceRowData.isActive(true);

				self.selectedServiceData(serviceRowData);

			}

		};

		// #endregion


		// #region Position Functions
		self.addPosition = function () {
			var object = new Object();

			object.isEditing = true;
			self.selectedServiceData().Positions.push(new ServicePositionModel(object));

		};


		self.savePosition = function (servicePosition) {
			servicePosition.isEditing(false);

		};

		self.editPosition = function (servicePosition) {
			servicePosition.isEditing(true);

		};

		self.removePosition = function (servicePosition) {
			self.selectedServiceData().Positions.remove(servicePosition);

		};


		self.isPositionSelected = ko.observable(false);

		self.selectedPositionData = ko.observable(ServicePositionModel(new Object()));

		self.selectPosition = function (positionRowData) {
			if (!positionRowData.isEditing()) {
				self.isPositionSelected(true);

				positionRowData.isActive(true);

				self.selectedPositionData(positionRowData);


			}

		};

		// #endregion

		self.saveProject = function () {
			self.project().Service = self.projectServices;
			self.project().Service().Positions = self.servicePositions;

			var token = $('[name=__RequestVerificationToken]').val();


			$.ajax({
				type: "POST",
				url: "/LeistungManager/CreateProject",
				data: { __RequestVerificationToken: token, model: ko.toJS(self.project()) },

				dataType: "json",
				cache: false,
				async: true,
				success: function (response) {

				},

				complete: function (response) {
					console.log(response);

				}

			});

		};


	};

ko.applyBindings(new ProjectViewModel());

例如

:unicode.characters_to_binary(utf16binary, {:utf16, :little})

(最后有一个空字节,因此二进制显示而不是字符串将在shell中使用,并且根据操作系统,它可能会为空字节打印一些额外的表示)

答案 1 :(得分:1)

您可以使用Elixir的模式匹配,特别是<<codepoint::utf16-little>>

defmodule Convert do
  def utf16le_to_utf8(binary), do: utf16le_to_utf8(binary, "")

  defp utf16le_to_utf8(<<codepoint::utf16-little, rest::binary>>, acc) do
    utf16le_to_utf8(rest, <<acc::binary, codepoint::utf8>>)
  end
  defp utf16le_to_utf8("", acc), do: acc
end

<<68, 0, 101, 0, 118, 0, 97, 0, 115, 0, 116, 0, 97, 0, 116, 0, 111, 0, 114, 0, 0, 0>>
|> Convert.utf16le_to_utf8
|> IO.puts

<<192, 3, 114, 0, 178, 0>>
|> Convert.utf16le_to_utf8
|> IO.puts

输出:

Devastator
πr²