Import a .dat file from CDC mortality 2003 into Rstudio - r
Greeting stackoverflow R community;
I have downloaded 10 data sets from CDC that correspond to mortality from 2003 to 2013. Each year is a separate data set. Some data sets have an extension of .DUSMCPUB and others have an extension of .dat. I have found a python script on github to parse 2013 .DUSMCPUB file and export it to .csv. However, I have not found anything to read the .dat files. I'm playing around and changing parameters with the data <- read.table(file = 'Mort03us.dat', header =TRUE, sep = ','). When I open the .dat file in a text editor I get this outcome.
I'm expecting headers on this data. However, I did read the documentation to see if the position of the values make any sense with the documentation, and doesn't.
I search the CDC website hoping to find documentation on how to read the file, or from what software this file was exported, or some information that might help to read the file, but no documentation I found.
Here I found a similar question, however, my .dat is not text and the script is not working for me. Also, I played around with the script without success.
Anyone of you have worked with CDC data sets in .dat format that can give me some guidance on how to import this data into RStudio?
July 1st, 2016:
Solution to this question:
I want to make an update to my question. I found a solution and I want to share with the community. Finally, I tweak a python script from github. I was able to parse the .dat file and import the data set into R. I want to thank you all!!
fileObj = open('Mort03us.dat', 'r')
fileOutObj = open('mort_2003.csv', 'a')
fileOutObj.write('Resident_Status, Education, Month_Of_Death, Sex, Age_Key, Age_Value, Age_Sub_Flag, Age_Recode_52, ' +
'Age_Recode_27, Age_Recode_12, Infant_Age_Recode_22, Place_Of_Death, Marital_Status, DOW_of_Death, ' +
'Data_Year, Injured_At_Work, Manner_Of_Death, Method_Of_Disposition, Autopsy, Activity_Code, ' +
'Place_Of_Causal_Injury, ICD10, Cause_Recode_358, Cause_Recode_113, Infant_Cause_Recode_130, ' +
'Cause_Recode_39, Entity_Axis_Conditions, EAC1, EAC2, EAC3, EAC4, EAC5, EAC6, EAC7, EAC8, EAC9, ' +
'EAC10, EAC11, EAC12, EAC13, EAC14, EAC15, EAC16, EAC17, EAC18, EAC19, EAC20, '
'Record_Axis_Conditions, RA1, RA2, RA3, RA4, RA5, RA6, RA7, RA8, RA9, RA10, RA11, RA12, RA13, RA14, ' +
'RA15, RA16, RA17, RA18, RA19, RA20, Race, Race_Bridged, Race_Imputation, Race_Recode_3, ' +
'Race_Recode_5, Hispanic_Origin, Hispanic_Origin_Recode\n')
outStr = ''
for line in fileObj:
Resident_Status = line[19].strip()
Education = line[60:62].strip()
Month_Of_Death = line[64:66].strip()
Sex = line[68].strip()
Age_Key = line[69].strip()
Age_Value = line[70:73].strip()
Age_Sub_Flag = line[73].strip()
Age_Recode_52 = line[74:76].strip()
Age_Recode_27 = line[76:78].strip()
Age_Recode_12 = line[78:80].strip()
Infant_Age_Recode_22 = line[80:82].strip()
Place_Of_Death = line[82].strip()
Marital_Status = line[83].strip()
DOW_of_Death = line[84].strip()
Data_Year = line[101:105].strip()
Injured_At_Work = line[105].strip()
Manner_Of_Death = line[106].strip()
Method_Of_Disposition = line[107].strip()
Autopsy = line[108].strip()
Activity_Code = line[143].strip()
Place_Of_Causal_Injury = line[144].strip()
ICD10 = line[145:149].strip()
Cause_Recode_358 = line[149:152].strip()
Cause_Recode_113 = line[153:156].strip()
Infant_Cause_Recode_130 = line[156:159].strip()
Cause_Recode_39 = line[159:161].strip()
Entity_Axis_Conditions = line[162:164].strip()
EAC1 = line[164:171].strip()
EAC2 = line[171:178].strip()
EAC3 = line[178:185].strip()
EAC4 = line[185:192].strip()
EAC5 = line[192:199].strip()
EAC6 = line[199:206].strip()
EAC7 = line[206:213].strip()
EAC8 = line[213:220].strip()
EAC9 = line[220:227].strip()
EAC10 = line[227:234].strip()
EAC11 = line[234:241].strip()
EAC12 = line[241:248].strip()
EAC13 = line[248:255].strip()
EAC14 = line[255:262].strip()
EAC15 = line[262:269].strip()
EAC16 = line[269:276].strip()
EAC17 = line[276:283].strip()
EAC18 = line[283:290].strip()
EAC19 = line[290:297].strip()
EAC20 = line[297:304].strip()
Record_Axis_Conditions = line[340:342]
RA1 = line[343:348].strip()
RA2 = line[348:353].strip()
RA3 = line[353:358].strip()
RA4 = line[358:363].strip()
RA5 = line[363:368].strip()
RA6 = line[368:373].strip()
RA7 = line[373:378].strip()
RA8 = line[378:383].strip()
RA9 = line[383:388].strip()
RA10 = line[388:393].strip()
RA11 = line[393:398].strip()
RA12 = line[398:403].strip()
RA13 = line[403:408].strip()
RA14 = line[408:413].strip()
RA15 = line[413:418].strip()
RA16 = line[418:423].strip()
RA17 = line[423:428].strip()
RA18 = line[428:433].strip()
RA19 = line[433:438].strip()
RA20 = line[438:443].strip()
Race = line[444:446].strip()
Race_Bridged = line[446].strip()
Race_Imputation = line[447].strip()
Race_Recode_3 = line[448].strip()
Race_Recode_5 = line[449].strip()
Hispanic_Origin = line[483:486].strip()
Hispanic_Origin_Recode = line[487].strip()
outStr = (Resident_Status + ', ' + Education + ', ' + Month_Of_Death + ', ' + Sex +
', ' + Age_Key + ', ' + Age_Value + ', ' + Age_Sub_Flag + ', ' + Age_Recode_52 +
', ' + Age_Recode_27 + ', ' + Age_Recode_12 + ', ' + Infant_Age_Recode_22 + ', ' + Place_Of_Death +
', ' + Marital_Status + ', ' + DOW_of_Death + ', ' + Data_Year + ', ' + Injured_At_Work +
', ' + Manner_Of_Death + ', ' + Method_Of_Disposition + ', ' + Autopsy + ', ' + Activity_Code +
', ' + Place_Of_Causal_Injury + ', ' + ICD10 + ', ' + Cause_Recode_358 + ', ' + Cause_Recode_113 +
', ' + Infant_Cause_Recode_130 + ', ' + Cause_Recode_39 + ', ' + Entity_Axis_Conditions + ', ' + EAC1 +
', ' + EAC2 + ', ' + EAC3 + ', ' + EAC4 + ', ' + EAC5 +
', ' + EAC6 + ', ' + EAC7 + ', ' + EAC8 + ', ' + EAC9 +
', ' + EAC10 + ', ' + EAC11 + ', ' + EAC12 + ', ' + EAC13 +
', ' + EAC14 + ', ' + EAC15 + ', ' + EAC16 + ', ' + EAC17 +
', ' + EAC18 + ', ' + EAC19 + ', ' + EAC20 + ', ' + Record_Axis_Conditions +
', ' + RA1 + ', ' + RA2 + ', ' + RA3 + ', ' + RA4 +
', ' + RA5 + ', ' + RA6 + ', ' + RA7 + ', ' + RA8 +
', ' + RA9 + ', ' + RA10 + ', ' + RA11 + ', ' + RA12 +
', ' + RA13 + ', ' + RA14 + ', ' + RA15 + ', ' + RA16 +
', ' + RA17 + ', ' + RA18 + ', ' + RA19 + ', ' + RA20 +
', ' + Race + ', ' + Race_Bridged + ', ' + Race_Imputation + ', ' + Race_Recode_3 +
', ' + Race_Recode_5 + ', ' + Hispanic_Origin + ', ' + Hispanic_Origin_Recode + '\n')
fileOutObj.write(outStr)
print("Parse complete.")
fileOutObj.close()
fileObj.close()
This code will work with Python 3.
Regardless of the file extension, it's merely a text file (88MB, with 2.4M rows). On inspection, it looks fixed-width (at 430 chars wide, a bit wide to show here, though your picture does it justice).
The only "trick" to reading in FWF files is to find the column widths. In some "dat" files, this is well-defined/documented, but it can often be inferred by looking at the first few lines. In this case, I just counted characters. (I only grab the first 100 lines for brevity.)
Some of the fields appear to be space-delimited. That is, some (even most) of the rows had a space between two groupings but some rows had none. Since I don't know how to decode the logic, I keep those together (with the space). (Namely: V6, V8, and V9.)
mort <- read.fwf("~/Downloads/foo/Mort03us.dat",
widths = c(30, 26, 6, 4, 7, 12, 24, 43, 9, 178, 3, 100, 4, 4, 36, 2),
header = FALSE, n = 100, stringsAsFactors = FALSE)
str(mort)
# 'data.frame': 100 obs. of 16 variables:
# $ V1 : chr " 11AK999AK9AK" " 11AK999AK9AK" " 11AK999AK9AK" " 11AK999AK9AK" ...
# $ V2 : chr " AK9999999992AK00009900OR" " AK9999999992AK00009900NY" " AK9999999992AK00009900WA" " AK9999999992AK00009900TX" ...
# $ V3 : chr " OR14" " NY17" " WA15" " TX16" ...
# $ V4 : int 1 1 1 1 1 1 1 1 1 1 ...
# $ V5 : chr " F1079" " F1088" " F1059" " F1084" ...
# $ V6 : chr " 412110 4W4" " 432311 1W6" " 371708 6D7" " 422210 4W7" ...
# $ V7 : chr " 2003U7UN" " 2003U7UN" " 2003U7UN" " 2003U7UN" ...
# $ V8 : chr " I361226" " I499228" " C64 117" " C349093" ...
# $ V9 : chr " 068 22" " 068 22" " 034 12" " 027 08" ...
# $ V10: chr " 0711I500 21I361 31I10 61I679 62I739 63M199 64N289 "| __truncated__ " 0311R568 21I959 31I499 "| __truncated__ " 0111C64 "| __truncated__ " 0111C349 "| __truncated__ ...
# $ V11: int 7 3 1 1 2 3 4 4 1 1 ...
# $ V12: chr " I10 I361 I500 I679 I739 M199 N289 " " I499 I959 R568 " " C64 " " C349 " ...
# $ V13: int 1 1 1 3 3 1 1 1 1 1 ...
# $ V14: int 11 11 11 23 23 11 11 11 11 11 ...
# $ V15: int 100 100 100 100 100 100 100 100 100 100 ...
# $ V16: int 6 6 6 8 8 6 6 6 6 6 ...
There is a lot of white-space, and the read.fwf function doesn't automatically remove it for you, so we'll have to take care of that next.
# chars <- sapply(mort, is.character)
mort[,chars] <- sapply(mort[,chars], function(z) {
gsub("[[:space:]]+", " ", trimws(z))
}, simplify = FALSE)
str(mort)
# 'data.frame': 100 obs. of 16 variables:
# $ V1 : chr "11AK999AK9AK" "11AK999AK9AK" "11AK999AK9AK" "11AK999AK9AK" ...
# $ V2 : chr "AK9999999992AK00009900OR" "AK9999999992AK00009900NY" "AK9999999992AK00009900WA" "AK9999999992AK00009900TX" ...
# $ V3 : chr "OR14" "NY17" "WA15" "TX16" ...
# $ V4 : int 1 1 1 1 1 1 1 1 1 1 ...
# $ V5 : chr "F1079" "F1088" "F1059" "F1084" ...
# $ V6 : chr "412110 4W4" "432311 1W6" "371708 6D7" "422210 4W7" ...
# $ V7 : chr "2003U7UN" "2003U7UN" "2003U7UN" "2003U7UN" ...
# $ V8 : chr "I361226" "I499228" "C64 117" "C349093" ...
# $ V9 : chr "068 22" "068 22" "034 12" "027 08" ...
# $ V10: chr "0711I500 21I361 31I10 61I679 62I739 63M199 64N289" "0311R568 21I959 31I499" "0111C64" "0111C349" ...
# $ V11: int 7 3 1 1 2 3 4 4 1 1 ...
# $ V12: chr "I10 I361 I500 I679 I739 M199 N289" "I499 I959 R568" "C64" "C349" ...
# $ V13: int 1 1 1 3 3 1 1 1 1 1 ...
# $ V14: int 11 11 11 23 23 11 11 11 11 11 ...
# $ V15: int 100 100 100 100 100 100 100 100 100 100 ...
# $ V16: int 6 6 6 8 8 6 6 6 6 6 ...
The last bit to take into account is that some of the uber-wide fields (V10 and V12) appear to be variable lists of codes. Since I wasn't confident I knew exactly how many, etc, I chose to keep them together, too. (This would be a good time to learn something about some more of Hadley's packages, specifically broom and purrr. This youtube video is an hour of Hadley talking about nested dataframes using these packages.)
(For the record, it does take a while to load the whole file and do this processing, so be patient. My recommendation is to "practice" on a modest subset of the entire file to make sure your processing is doing what you want. When you have your steps mapped out and tested, then run them all and go get a coffee.)
Related
Dynamic Sort BY in a Query Prepare with a Join
I want to join pt_mstr and in_mstr to create my report screen for which I have written this code i want to sort my output screen based on either prod line or status so i have defined a variable lvc_sort sort by prod line if i give 1 and similarly 2 for status IF lvc_sort = 1 THEN DO: FOR EACH pt_mstr no-lock WHERE pt_domain = global_domain AND pt_part >= lvc_part AND pt_part <= lvc_part1 AND pt_part_type >= lvc_part_type AND pt_part_type <= lvc_part_type1 AND pt_prod_line >= lvc_prod_line AND pt_prod_line <= lvc_prod_line1 AND pt_status >= lvc_status AND pt_status <= lvc_status1, EACH in_mstr WHERE in_domain = pt_domain AND in_part = pt_part BREAK BY pt_prod_line: FIND FIRST tt NO-LOCK WHERE tt_part = pt_part AND tt_site = in_site NO-ERROR. Is this the correct approach or can it be done in some other way?
You can use a query then you have a section with a different by clause DEFINE VARIABLE cQuery AS CHARACTER NO-UNDO. DEFINE VARIABLE hQuery AS HANDLE NO-UNDO. cQuery = "FOR EACH pt_mstr no-lock " + " WHERE pt_domain = " + QUOTER(global_domain) + " AND pt_part >= " + QUOTER(lvc_part) + " AND pt_part <= " + QUOTER(lvc_part1) + " AND pt_part_type >= " + QUOTER(lvc_part_type) + " AND pt_part_type <= " + QUOTER(lvc_part_type1) + " AND pt_prod_line >= " + QUOTER(lvc_prod_line) + " AND pt_prod_line <= " + QUOTER(lvc_prod_line1) + " AND pt_status >= " + QUOTER(lvc_status) + " AND pt_status <= " + QUOTER(lvc_status1) + ", EACH in_mstr " + " WHERE in_domain = " + QUOTER(pt_domain) + " AND in_part = " + QUOTER(pt_part). CREATE QUERY hQuery. hQuery:SET-BUFFERS(BUFFER pt_mstr:HANDLE, BUFFER in_mstr:HANDLE). IF lvc_sort = 1 THEN cQuery = cQuery + " BREAK BY pt_prod_line ". ELSE cQuery = cQuery + " BREAK BY pt_status ". hQuery:QUERY-PREPARE(cQuery). hQuery:QUERY-OPEN. // alle durch DO WHILE hQuery:GET-NEXT() : FIND FIRST tt NO-LOCK WHERE tt_part = pt_part AND tt_site = in_site NO-ERROR. END. FINALLY: hQuery:QUERY-CLOSE () NO-ERROR. DELETE OBJECT hQuery NO-ERROR. END FINALLY.
This code yields `0` as the value no matter what roman numeral I enter. Why is this so?
import re d = { ' M ' :1000, ' CM ' :900, ' D ' :500, ' CD ' :400, ' C ' :100, ' XC ':90, ' L ' :50, ' XL ' :40, ' X ' :10, ' IX ' :9, ' V ' :5, ' IV ' :4, ' I ' :1} pattern = re.compile(r"""(?x) (M{0,3})(CM)? (CD)?(D)?(C{0,3}) (XC)?(XL)?(L)?(X{0,3}) (IX)?(IV)?(V)?(I{0,3})""") num = input( ' Enter Roman numeral: ' ).upper() m = pattern.match(num) sum = 0 for x in m.groups(): if x!=None and x!= '' : if x in [ ' CM ' , ' CD ' , ' XC ' , ' XL ' , ' IX ' , ' IV ' ]: sum+=d[x] elif x[0] in ' MDCLXVI ' : sum+=d[x[0]]* len(x) print(sum) When I run this code it gives 0 as the output no matter what Roman numeral I enter. What did I do wrong? Is white spaces the issue? For example: Enter Roman numeral I 0 Enter Roman numeral X 0
AX 2012 View computed column returning Null
I have a View in AX with a computed column: private static server str qty() { #define.THEN(" THEN ") #define.SINGLE_QUOTE("'") return 'CASE T2.ReturnStatus ' + ' WHEN ' + int2str(enum2int(ReturnStatusLine::None)) + #THEN + '-1 * T3.UnitValue' + ' WHEN ' + int2str(enum2int(ReturnStatusLine::Awaiting)) + #THEN + '-1 * T3.UnitValue' + ' WHEN ' + int2str(enum2int(ReturnStatusLine::Registered)) + #THEN + '-1 * T3.UnitValue' + ' ELSE ' + "(T3.UnitValue / T2.ExpectedRetQty * (SELECT TOP 1 SUM(cpst.Qty) as RcvQty from custPackingSlipTrans as cpst where cpst.InventTransId = T2.InventTransId and cpst.dataAreaId='" + curext() + "')) * -1" + ' END'; } It works great, except the past week or so the column is returning NULL when it should not be. This is fixed simply by going into the AOT and syncing this view, after that the column has a valid value. But we're having to do this almost daily. Any ideas?
moment.js difference between .weekday() and .format('d')
Can somebody explain to me the difference of .weekday() and .format('d') in moment.js? e.g. console.log('month: ' + month.format('DD/MM/YYYY')); let firstDay = moment(month).startOf('M'); console.log('/1 firstDay: ' + firstDay.format('DD/MM/YYYY')); console.log('/1 firstDay.weekday(): ' + firstDay.weekday()); console.log('/1 firstDay.format("d"): ' + firstDay.format('d')); firstDay = moment('01/08/2018', 'DD/MM/YYYY'); console.log('/2 firstDay: ' + firstDay.format('DD/MM/YYYY')); console.log('/2 firstDay.weekday(): ' + firstDay.weekday()); console.log('/2 firstDay.format("d"): ' + firstDay.format('d')); produces: month: 09/08/2018 /1 firstDay: 01/08/2018 /1 firstDay.weekday(): 2 /1 firstDay.format("d"): 3 /2 firstDay: 01/08/2018 /2 firstDay.weekday(): 3 /2 firstDay.format("d"): 3
Classic ASP code migration
I am trying to migrate some code from Classic ASP/VBScript. This code shown below generates a barcode. As it is known, some functions do not exist or are entirely different in .NET. So I figured I would thrown the old code, in the code-behind of my barcode.aspx page (as I call the barcode generation sequence like this: <img src="/barcode.aspx?code="<%: getID(sheet_id)%>" border="0" height="80"/> This is the Classic code: <% Option Explicit Response.CodePage = 1252 Response.CharSet = "windows-1252" Response.Contenttype = "image/bmp" ' code = bar code value ' height = height of barcode in pixels. ' width = width MULTIPLIER in pixels. ' mode = type of barcode (Currently supported barcode types: code39, code128b, UPC-A, EAN-13) ' dim code, origcode, height, width, mode, caching, FontKey, FontCN10, FontCN12 caching = False ' turn this on to cache barcodes in '10101010' format. Might speed things up on busy servers, although this script doesn't take many resources to begin with. An EAN-13 or UPC barcode will take less than 100 bytes of memory space. Other types will take more or less depending on the length of the barcode created. ' DO NOT EDIT BELOW THIS LINE! code = Request.QueryString("code") height = 80 'request.querystring("height") width = 2 'request.querystring("width") mode = "code39" 'request.querystring("mode") origcode = code if not IsNumeric(height) or height = "" then height = 1 else height = numeric(height) if not IsNumeric(width) or width = "" then width = 1 else width = numeric(width) if caching AND application("cache" & origcode & mode & height & width) <> "" then code = application("cache" & origcode & mode & height & width) else select case lcase(mode) case "raw" ' do nothing. non-0 chars are automatically 1s case "code39": code = code39(code) case "code128b": code = code128b(code) case "upc-a": code = codeean13("0" & code, "AAAAAA") case "ean-13": code = codeean13(code, eanflag(left(code, 1))) end select if caching then Application.Lock Application("cache" & origcode & mode & height & width) = code Application.UnLock end if end if Function stb(String) Dim I, B For I=1 to len(String) B = B & ChrB(Asc(Mid(String,I,1))) Next stb = B End Function function tstr(data, width) dim tchar, total, tpos, i, j, x tchar = 0 total = "" tpos = 8 for i = 1 to len(data) for j = 1 to width tpos = tpos - 1 if mid(data, i, 1) <> "0" then tchar = tchar + 2^tpos if tpos = 0 then total = total & chr(tchar) tpos = 8 tchar = 0 end if next next if tpos <> 8 then total = total & chr(tchar) end if x = len(total) mod 4 if x = 0 then x = 4 for i = x to 3 total = total & chr(0) next tstr = total end function function numeric(num) dim numb, valid, i numb = "" valid = "0123456789" for i = 1 to len(num) if InStr(valid, mid(num, i, 1)) > 0 then numb = numb & mid(num, i, 1) next num = left(num, 30) numeric = cint(num) end function function size(lngth) lngth = cdbl(lngth) if lngth > 255 then if lngth > 65535 then lngth = 65535 size = chr(lngth mod 256) & chr(int(lngth/256)) else size = chr(lngth) & chr(0) end if end function function code39(code) dim output, i, clet output = "" code = "*" & replace(code, "*", "") & "*" for i = 1 to len(code) clet = "" select case ucase(mid(code, i, 1)) case "1": clet = "111010001010111" case "2": clet = "101110001010111" case "3": clet = "111011100010101" case "4": clet = "101000111010111" case "5": clet = "111010001110101" case "6": clet = "101110001110101" case "7": clet = "101000101110111" case "8": clet = "111010001011101" case "9": clet = "101110001011101" case "0": clet = "101000111011101" case "A": clet = "111010100010111" case "B": clet = "101110100010111" case "C": clet = "111011101000101" case "D": clet = "101011100010111" case "E": clet = "111010111000101" case "F": clet = "101110111000101" case "G": clet = "101010001110111" case "H": clet = "111010100011101" case "I": clet = "101110100011101" case "J": clet = "101011100011101" case "K": clet = "111010101000111" case "L": clet = "101110101000111" case "M": clet = "111011101010001" case "N": clet = "101011101000111" case "O": clet = "111010111010001" case "P": clet = "101110111010001" case "Q": clet = "101010111000111" case "R": clet = "111010101110001" case "S": clet = "101110101110001" case "T": clet = "101011101110001" case "U": clet = "111000101010111" case "V": clet = "100011101010111" case "W": clet = "111000111010101" case "X": clet = "100010111010111" case "Y": clet = "111000101110101" case "Z": clet = "100011101110101" case "-": clet = "100010101110111" case ".": clet = "111000101011101" case " ": clet = "100011101011101" case "*": clet = "100010111011101" case "$": clet = "100010001000101" case "/": clet = "100010001010001" case "+": clet = "100010100010001" case "%": clet = "101000100010001" end select output = output & clet & "0" next code39 = left(output, len(output)-1) end function Function code128b(ByVal InputString) Const MinValidAscii = 32 Const MaxValidAscii = 126 Dim CharValue(255) Dim i for i = 0 to 94 CharValue(i+32) = i next for i = 95 to 106 CharValue(i+100) = i next ' Encode the input string InputString = Trim(InputString) Dim CheckDigitValue, CharPos, CharAscii, InvalidCharsFound InvalidCharsFound = false CheckDigitValue = CharValue(204) For CharPos = 1 To Len(InputString) CharAscii = Asc(Mid(InputString, CharPos, 1)) if (CharAscii < MinValidAscii) OR (CharAscii > MaxValidAscii) then CharAscii = Asc("?") InvalidCharsFound = true end if CheckDigitValue = CheckDigitValue + (CharValue(CharAscii) * CharPos) Next CheckDigitValue = (CheckDigitValue Mod 103) Dim CheckDigitAscii if CheckDigitValue < 95 then CheckDigitAscii = CheckDigitValue + 32 else CheckDigitAscii = CheckDigitValue + 100 end if Dim OutputString OutputString = Chr(204) & InputString & Chr(CheckDigitAscii) & Chr(206) Dim BarcodePattern(255) BarcodePattern(32) = "212222" ' <SPACE> BarcodePattern(33) = "222122" ' ! BarcodePattern(34) = "222221" ' " BarcodePattern(35) = "121223" ' # BarcodePattern(36) = "121322" ' $ BarcodePattern(37) = "131222" ' % BarcodePattern(38) = "122213" ' & BarcodePattern(39) = "122312" ' ' BarcodePattern(40) = "132212" ' ( BarcodePattern(41) = "221213" ' ) BarcodePattern(42) = "221312" ' * BarcodePattern(43) = "231212" ' + BarcodePattern(44) = "112232" ' , BarcodePattern(45) = "122132" ' - BarcodePattern(46) = "122231" ' . BarcodePattern(47) = "113222" ' / BarcodePattern(48) = "123122" ' 0 BarcodePattern(49) = "123221" ' 1 BarcodePattern(50) = "223211" ' 2 BarcodePattern(51) = "221132" ' 3 BarcodePattern(52) = "221231" ' 4 BarcodePattern(53) = "213212" ' 5 BarcodePattern(54) = "223112" ' 6 BarcodePattern(55) = "312131" ' 7 BarcodePattern(56) = "311222" ' 8 BarcodePattern(57) = "321122" ' 9 BarcodePattern(58) = "321221" ' : BarcodePattern(59) = "312212" ' ; BarcodePattern(60) = "322112" ' < BarcodePattern(61) = "322211" ' = BarcodePattern(62) = "212123" ' > BarcodePattern(63) = "212321" ' ? BarcodePattern(64) = "232121" ' # BarcodePattern(65) = "111323" ' A BarcodePattern(66) = "131123" ' B BarcodePattern(67) = "131321" ' C BarcodePattern(68) = "112313" ' D BarcodePattern(69) = "132113" ' E BarcodePattern(70) = "132311" ' F BarcodePattern(71) = "211313" ' G BarcodePattern(72) = "231113" ' H BarcodePattern(73) = "231311" ' I BarcodePattern(74) = "112133" ' J BarcodePattern(75) = "112331" ' K BarcodePattern(76) = "132131" ' L BarcodePattern(77) = "113123" ' M BarcodePattern(78) = "113321" ' N BarcodePattern(79) = "133121" ' O BarcodePattern(80) = "313121" ' P BarcodePattern(81) = "211331" ' Q BarcodePattern(82) = "231131" ' R BarcodePattern(83) = "213113" ' S BarcodePattern(84) = "213311" ' T BarcodePattern(85) = "213131" ' U BarcodePattern(86) = "311123" ' V BarcodePattern(87) = "311321" ' W BarcodePattern(88) = "331121" ' X BarcodePattern(89) = "312113" ' Y BarcodePattern(90) = "312311" ' Z BarcodePattern(91) = "332111" ' [ BarcodePattern(92) = "314111" ' / BarcodePattern(93) = "221411" ' ] BarcodePattern(94) = "431111" ' ^ BarcodePattern(95) = "111224" ' _ BarcodePattern(96) = "111422" ' ` BarcodePattern(97) = "121124" ' a BarcodePattern(98) = "121421" ' b BarcodePattern(99) = "141122" ' c BarcodePattern(100) = "141221" ' d BarcodePattern(101) = "112214" ' e BarcodePattern(102) = "112412" ' f BarcodePattern(103) = "122114" ' g BarcodePattern(104) = "122411" ' h BarcodePattern(105) = "142112" ' i BarcodePattern(106) = "142211" ' j BarcodePattern(107) = "241211" ' k BarcodePattern(108) = "221114" ' l BarcodePattern(109) = "413111" ' m BarcodePattern(110) = "241112" ' n BarcodePattern(111) = "134111" ' o BarcodePattern(112) = "111242" ' p BarcodePattern(113) = "121142" ' q BarcodePattern(114) = "121241" ' r BarcodePattern(115) = "114212" ' s BarcodePattern(116) = "124112" ' t BarcodePattern(117) = "124211" ' u BarcodePattern(118) = "411212" ' v BarcodePattern(119) = "421112" ' w BarcodePattern(120) = "421211" ' x BarcodePattern(121) = "212141" ' y BarcodePattern(122) = "214121" ' z BarcodePattern(123) = "412121" ' { BarcodePattern(124) = "111143" ' | BarcodePattern(125) = "111341" ' } BarcodePattern(126) = "131141" ' ~ BarcodePattern(195) = "114113" BarcodePattern(196) = "114311" BarcodePattern(197) = "411113" BarcodePattern(198) = "411311" BarcodePattern(199) = "113141" BarcodePattern(200) = "114131" BarcodePattern(201) = "311141" BarcodePattern(202) = "411131" BarcodePattern(203) = "211412" BarcodePattern(204) = "211214" BarcodePattern(205) = "211232" BarcodePattern(206) = "2331112" Dim OutputPattern, ThisPattern, thischar OutputPattern = "" for CharPos = 1 to Len(OutputString) ThisPattern = BarcodePattern(Asc(Mid(OutputString, CharPos, 1))) for i = 1 to len(ThisPattern) if i mod 2 = 1 then thischar = "1" else thischar = "0" OutputPattern = OutputPattern & replace(space(int(mid(ThisPattern, i, 1))), " ", thischar) next next code128b = OutputPattern End Function Function CodeEAN13(code, encoding) Dim leftA, leftB, rght, OutputPattern, i if len(code) = 13 then LeftA = Array("0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011") LeftB = Array("0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111") Rght = Array("1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100") OutputPattern = "101" for i = 1 to 6 if mid(ucase(encoding), i, 1) = "A" then OutputPattern = OutputPattern & LeftA(cint(mid(code, i+1, 1))) else OutputPattern = OutputPattern & LeftB(cint(mid(code, i+1, 1))) end if next OutputPattern = OutputPattern & "01010" for i = 1 to 6 OutputPattern = OutputPattern & Rght(cint(mid(code, i+7, 1))) next OutputPattern = OutputPattern & "101" CodeEAN13 = OutputPattern end if End Function Function eanflag(num) select case num case 0: eanflag = "AAAAAA" case 1: eanflag = "AABABB" case 2: eanflag = "AABBAB" case 3: eanflag = "AABBBA" case 4: eanflag = "ABAABB" case 5: eanflag = "ABBAAB" case 6: eanflag = "ABBBAA" case 7: eanflag = "ABABAB" case 8: eanflag = "ABABBA" case 9: eanflag = "ABBABA" end select End Function dim dataout, i if code <> "" then dataout = tstr(code, width) response.binarywrite stb(chr(66) & chr(77) & size(62+(len(dataout)*height)) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0) & chr(62) & chr(0) & chr(0) & chr(0) & chr(40) & chr(0) & chr(0) & chr(0) & size(len(code)*width) & chr(0) & chr(0) & size(height) & chr(0) & chr(0) & chr(1) & chr(0) & chr(1) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0) & chr(37) & chr(14) & chr(0) & chr(0) & chr(37) & chr(14) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0) & chr(255) & chr(255) & chr(255) & chr(0) & chr(0) & chr(0) & chr(0) & chr(0)) for i = 1 to height response.binarywrite stb(dataout) next end if %> The stuff the are "problematic" (based on VS underlining) are: For I = 1 To Len(text) B = B & ChrB(Asc(Mid(text, I, 1))) Next and Array in CodeEAN13 function, which I completely commented out since I am using Code39 I put this piece of code on code-behind of barcode.aspx's page load: Response.Charset = "windows-1252" Response.ContentType = "image/bmp" Dim code, origcode, height, width, mode, caching, FontKey, FontCN10, FontCN12 caching = False ' turn this on to cache barcodes in '10101010' format. Might speed things up on busy servers, although this script doesn't take many resources to begin with. An EAN-13 or UPC barcode will take less than 100 bytes of memory space. Other types will take more or less depending on the length of the barcode created. ' DO NOT EDIT BELOW THIS LINE! code = Request.QueryString("code") height = 80 'request.querystring("height") width = 2 'request.querystring("width") mode = "code39" 'request.querystring("mode") origcode = code If Not IsNumeric(height) Or height = "" Then height = 1 Else height = numeric(height) If Not IsNumeric(width) Or width = "" Then width = 1 Else width = numeric(width) If caching And Application("cache" & origcode & mode & height & width) <> "" Then code = Application("cache" & origcode & mode & height & width) Else Select Case LCase(mode) Case "raw" ' do nothing. non-0 chars are automatically 1s Case "code39" : code = code39(code) Case "code128b" : code = code128b(code) 'Case "upc-a" : code = codeean13("0" & code, "AAAAAA") 'Case "ean-13" : code = codeean13(code, eanflag(Left(code, 1))) End Select If caching Then Application.Lock() Application("cache" & origcode & mode & height & width) = code Application.UnLock() End If End If If code <> "" Then dataout = tstr(code, width) Response.BinaryWrite(stb(Chr(66) & Chr(77) & size(62 + (Len(dataout) * height)) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(62) & Chr(0) & Chr(0) & Chr(0) & Chr(40) & Chr(0) & Chr(0) & Chr(0) & size(Len(code) * width) & Chr(0) & Chr(0) & size(height) & Chr(0) & Chr(0) & Chr(1) & Chr(0) & Chr(1) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(37) & Chr(14) & Chr(0) & Chr(0) & Chr(37) & Chr(14) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(255) & Chr(255) & Chr(255) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0))) For i = 1 To height Response.BinaryWrite(stb(dataout)) Next End If Also, I tried changing Function stb(text) Dim I, B For I = 1 To Len(text) B = B & ChrB(Asc(Mid(text, I, 1))) Next stb = B End Function to Function stb(text) Dim I, B For I = 1 To Len(text) B = B & Convert.ToChar(Asc(Mid(text, I, 1))) Next stb = B End Function Well, needless to say, it does not work :)
So, after several hours of sweating and beating myself, I have used another solution.I will post it here for anyone that might have similar problems. What I did was basically using the Barcode Rendering Framework As no clear documentation exists for this library, I will explain the procedure below: Download the library from the link above. Add a reference to your project, browsing to Zen.Barcode.Core.dll Import zen.barcode in your code-behind The rest is in my code :) Dim sheet_id As String = Session("generated_id") Dim height As Integer = 80 Dim width As Integer = 80 'Dim mode As String = "code39" Dim bar As Code39BarcodeDraw = BarcodeDrawFactory.Code39WithoutChecksum Dim img As Image = bar.Draw(sheet_id, height, width) Dim bmp As Bitmap = img Dim stream As New System.IO.MemoryStream bmp.Save(stream, Imaging.ImageFormat.Bmp) stream.Position = 0 Dim size As Integer = stream.Length Dim data(stream.Length) As Byte stream.Read(data, 0, stream.Length) Response.BinaryWrite(data) The call is being made on the landing page's body like so: <img src="/barcode.aspx" border="0" height="80" width="250"/>