SetUChar Function

Action

Copies a one-byte unsigned character into a data structure at a specified position. You must declare a BDL string before using this function; see Data Structure Functions for further information.

This function can also be used to convert an ASCII representation of a hexadecimal string to a hexadecimal string by using a custom function. See the example below.

Include file

Kernel.bdh

Syntax

SetUChar( inout sString : string,
          in    nPos    : number,
          in    nChar   : number ) : boolean;

Return value

  • true if successful

  • false otherwise

Parameter Description
sString BDL string corresponding to the data structure
nPos Insert position in the BDL string, where the first position is 1 (not 0)
nChar Unsigned character

Example

dcltrans
  transaction TSetUChar
  const    SIZE_STRUCT := 32;
  var
    myBDLstring : string(SIZE_STRUCT);
    bOk         : boolean;
  begin
    // Set a one-byte unsigned character at position 32
    // in the BDL string myBDLstring and thereby (through mapping) set
    // the element myUChar in Struct1
    bOk := SetUChar(myBDLstring, 32, 255);
    if bOk then
      write("myUChar successfully set to 255"); writeln;
    else
      write("error setting myUChar"); writeln;
    end;
  end TSetUChar;

Output

myUChar successfully set to 255

Converting ASCII Representation of Hex String to Hex String Example

benchmark MyBenchmarkName

use "Kernel.bdh" 

dcluser  
  user
    VUser
  transactions
    TMain        : 1;

//converts each char to decimal value

dclfunc
  function GetDecFromAsciiHexStr(sAsciiHex: string): number
  var
    nDecPart : number;
    sTemp    : string;
    i, j     : number;
    nDec     : number;
    nFactor  : number;
  begin
   
    nDec := 0;
    for i:=1 to 2 do         
      sTemp[1]:=sAsciiHex[i];    
      
      if sTemp="0" then
        nDecPart := 0;
      elseif sTemp="1" then
        nDecPart := 1;
      elseif sTemp="2" then
        nDecPart := 2;
      elseif sTemp="3" then
        nDecPart := 3;
      elseif sTemp="4" then
        nDecPart := 4;
      elseif sTemp="5" then
        nDecPart := 5;
      elseif sTemp="6" then
        nDecPart := 6;
      elseif sTemp="7" then
        nDecPart := 7;
      elseif sTemp="8" then
        nDecPart := 8;
      elseif sTemp="9" then
        nDecPart := 9;
      elseif sTemp="A" then
        nDecPart := 10;
      elseif sTemp="B" then
        nDecPart := 11;
      elseif sTemp="C" then
        nDecPart := 12;
      elseif sTemp="D" then
        nDecPart := 13;
      elseif sTemp="E" then
        nDecPart := 14;
      elseif sTemp="F" then
        nDecPart := 15;
      end;       
      
      if i=1 then
        nDecPart := nDecPart*16;
      end;

      nDec := nDec + nDecPart;            
    end;
    
    GetDecFromAsciiHexStr := nDec;       
  end GetDecFromAsciiHexStr;
    
  

dcltrans


  transaction TMain
  var
    sStringWithHexVal: string;
    nLengthS         : number;
    sAsciiHex        : string;
    sHex             : string;
    nDec             : number;
    i, j             : number;
  begin
    
    //example of string to convert
    sStringWithHexVal := "03CA00000041";
    
    print("sStringWithHexVal: "+ sStringWithHexVal);    
    
    nLengthS := strlen(sStringWithHexVal);
    print("nLengthS: "+string(nLengthS));
    
    i := 1;
    j := 1;
    while i < nLengthS do   //rebuild string in hex using decimal values       
      sAsciiHex := sStringWithHexVal[i]+sStringWithHexVal[i+1];      
      nDec := GetDecFromAsciiHexStr(sAsciiHex);
      print(string(i)+": " + sAsciiHex + " -> " + string(nDec));
      SetUChar(sHex, j, nDec); 
      j:= j + 1;
      i:= i + 2;
    end;  
    
    writedata(sHex, BinLen(sHex), OPT_WRITEDATA_HEX); //result string
      
    
  end TMain;